数组
数组的操作
获取数组中的元素,赋值操作,通常会用到的操作
1、静态数组赋值
1 int [] a={5,4,1,2,6,8,4}
2、动态数组赋值
方法一
1 Student[] students = new Student[10]; 2 students[0] = new Student(100, "张三", 18); 3 students[1] = new Student(200, "李四", 28); 4 students[2] = new Student(300, "王五", 38);
方法二
遍历【遍历的功能不只是给动态数组赋值,只是说具备给动态数组赋值的功能】
1 public class Array{ 2 public static void main(String[] args){ 3 int arr[] = new int[3] 4 for(int i=0;i<3;i++){ 5 System.out.println("arr["+i+"]="+arr[i]+";"); 6 } //输出arr[0]=0 arr[1]=0 arr[2]=0 7 } 8 }
数组长度【length】
数组中有一个属性,可以直接获取数组长度 length
使用方式 数组名.length
1 public class Array{ 2 public static void main(String[] args){ 3 int arr[] ={5,6,8,4,5,6,9,8}; 4 System.out.println("length"+arr.length) //8 5 } 6 }
应用
用于打印数组中的元素,元素之间用逗号隔开
1 public class Array{ 2 public static void main(String[] args){ 3 int arr[] ={5,6,8,4,5,6,9,8}; 4 5 arrprint(arr); 6 } 7 public static void arrprint(int arr){ 8 for(int i=0;i<arr.length;i++){ 9 System.out.println(arr[i]+","); //5,6,8,4,5,6,9,8 10 } 11 }
输出细节
这种情况下输出的是数组的地址值
1 public class Array{ 2 public static void main(String[] args){ 3 int arr[] ={5,6,8,4,5,6,9,8}; 4 5 System.out.println(arr); //输出数组地制值 6 }
2、获取最值
获取数组中的最大值,最小值
注意:只适用于比较数值类型 int double等都可以比较 例如boolean类型不能比较
思路:
1.获取最值需要进行比较,每一次比较都会有一个较大的值,因为该值不确定,通过一个变量进行存储
2.让数组中的每一个元素都和变量的值进行比较。
如果大于变量中的值,就用该变量记录较大值
3.当所有元素都比较完成,那么该变量储存的值就是数组中的最大值
步骤:
1.定于变量,初始化为数组中的任意元素即可
2.通过循环语句对数组进行遍历
3.在变量过程中定义判断条件,如果遍历到的元素比变量中的元素大,就复制给该变量
需要定义一个功能来完成,以便提高复用性
1.明确结果,数组中的最大元素 int X
2.未知内容:一个数组 int[]
应用
获取最大值
代码一
max代表一个数组的元素
1 public class Exam { 2 public static void main(String[] args) { 3 int arr[] = { 2, 5, 4, 8, 4, 6, 9, 4, 1, 5 }; 4 5 int max1 = arrMax(arr); 6 System.out.println("max=" + max1); 7 } 8 9 public static int arrMax(int[] arr) { 10 int max = arr[0]; 11 for (int i = 0; i < arr.length; i++) { 12 if (arr[i] > max) { 13 max = arr[i]; 14 15 } 16 17 } 18 return max; 19 20 } 21 }
代码二
max代表最大值的数组元素的角标
1 public class Exam { 2 public static void main(String[] args) { 3 int arr[] = { 2, 5, 4, 8, 4, 6, 9, 4, 1, 5 }; 4 5 int max1 = arrMax(arr); 6 System.out.println("max=" + arr[max1]); 7 } 8 9 public static int arrMax(int[] arr) { 10 int max = 0; 11 for (int i = 0; i < arr.length; i++) { 12 if (arr[i] > arr[max]) { 13 max = i; 14 15 } 16 17 } 18 return max; 19 20 } 21 }
获取最大小值
代码一
min代表一个数组的元素
1 public class Exam { 2 public static void main(String[] args) { 3 int arr[] = { 2, 5, 4, 8, 4, 6, 9, 4, 1, 5 }; 4 5 int min1 = arrMax(arr); 6 System.out.println("min=" + min1); 7 } 8 9 public static int arrMax(int[] arr) { 10 int min = arr[0]; 11 for (int i = 0; i < arr.length; i++) { 12 if (arr[i] < min) { 13 min = arr[i]; 14 15 } 16 17 } 18 return min; 19 20 } 21 }
代码二
min代表最大值的数组元素的角标
1 package com.bjsxt.ly; 2 3 public class Exam { 4 public static void main(String[] args) { 5 int arr[] = { 2, 5, 4, 8, 4, 6, 9, 4, 1, 5 }; 6 7 int min1 = arrMax(arr); 8 System.out.println("min=" + arr[min1]); 9 } 10 11 public static int arrMax(int[] arr) { 12 int min = 0; 13 for (int i = 0; i < arr.length; i++) { 14 if (arr[i] < arr[min]) { 15 min = i; 16 17 } 18 19 } 20 return min; 21 22 } 23 }
3、排序
选择排序
每一个数和数组内所有元素进行比较
特点:内循环结束一次,最值出现头角表位置上
二者共用同一组数据,所以一者更改,所对应的数组就进行了更改
1 package com.bjsxt.ly; 2 3 4 public class Exam{ 5 public static void arrPaiXu1(int[] arr) { 6 for (int i = 0; i < arr.length - 1; i++) { //代表每一个数和数组里的每一个数比 7 for (int j = i + 1; j < arr.length; j++) { 代表着和arr[i]进行比较的元素 8 if (arr[i] > arr[j]) { 9 int temp = arr[i]; 10 arr[i] = arr[j]; 11 arr[j] = temp; 12 } 13 } 14 } 15 16 } 17 public static void main(String[] args) { 18 int[] arr={6,8,7,6,5,1,5}; 19 arrPaiXu1(arr); 20 print(arr); 21 22 } 23 24 public static void print(int[] arr) { 25 for(int i=0;i<arr.length;i++){ 26 System.out.println(arr[i]); 27 } 28 } 29 }
冒泡排序【面试中比较常见】
相邻两个元素进行比较,如果符合条件就换位置
1 package com.bjsxt.ly; 2 3 4 public class Exam{ 5 6 public static void main(String[] args) { 7 int[] arr={6,8,7,6,5,1,5}; 8 9 maopao(arr); 10 print(arr); 11 } 12 13 public static void maopao(int [] arr) { 14 for(int i=0;i<arr.length-1;i++){ 15 for (int j = 0; j < arr.length-i-1; j++) { //-x:让每一次比较的元素减少,-1:避免角标越级 16 if (arr[j] > arr[j+1]) { 17 int temp = arr[j]; 18 arr[j] = arr[j+1]; 19 arr[j+1] = temp; 20 } 21 } 22 23 } 24 } 25 26 public static void print(int[] arr) { 27 for(int i=0;i<arr.length;i++){ 28 System.out.println(arr[i]); 29 } 30 } 31 }
数组迁移
数组不适合做数据的迁移
删除指定位置数据
插入数据到指定位置
数组的最大优点在于可以进行数据的随机访问
应用
1 private static void deleteStudent(Student[] students) { 2 System.out.println("===============请输入要删除学生的学号"); 3 //显示所有的学生信息 4 showStudent(students); 5 //输入要删除学生的学号 6 Scanner scanner = new Scanner(System.in); 7 int studentNo = scanner.nextInt(); 8 //删除学生信息 9 for (int i = 0; i < students.length; i++) { 10 //开始判断并删除学生信息 11 if (students[i] != null && students[i].getStudentNo() == studentNo) { 12 //删除数据,就相当于用后面的数据覆盖前面的数据,开始做数据迁移 13 for (int j = i; j < students.length - 1; j++) { 14 //开始判断 15 if (students[j + 1] != null) { 16 //后面的同学覆盖前面的同学 17 students[j] = students[j + 1]; 18 } else { 19 //让最后一个同学的数据为null 20 students[j] = null; 21 break; 22 } 23 } 24 break; 25 } 26 //学生信息删除失败,找不到指定的学生 27 if (i + 1 == students.length) { 28 System.out.println("=========学生信息删除失败,找不到指定的学生"); 29 } 30 } 31 32 }
对j < students.length - 1的解释
例如要删除的元素是第3个元素,下标为2,然后进行覆盖 【3覆盖2】,【4覆盖3】,因为不存在5,所以要用length-1,因为如果没有length-1,就会有【5覆盖4】,因为5不存在,就会报错,出现【异常Exception , 数组下标越界】
多维数组
一维数组
1 public class Exam{ 2 public static void main(String[] args) { 3 //定义一个数组,数组长度5,一维数组 4 int []a=null; 5 a=new int[5]; 6 7 System.out.println(a.length); //5 8 9 } 10 }
二维数组
声明二维数组
一、
1 int[][] arrays02 = { { 1, 2 }, { 1, 2, 3 }, { 1, 2, 3, 4 } }; 2 for (int i = 0; i < arrays02.length; i++) { 3 System.out.println(arrays02[i].length); //2 3 4 4 }
二、
1 public class HelloArray { 2 public static void main(String[] args) { 3 String[][] arrays = null; 4 arrays = new String[5][3]; 5 for (int i = 0; i < arrays.length; i++) { 6 for (int j = 0; j < arrays[i].length; j++) { 7 arrays[i][j] = "" + i + j; 8 System.out.print(arrays[i][j] + "\t"); 9 10 } 11 System.out.println(); 12 System.out.println(); 13 } 14 } 15 }
结果:
图像分析
二维数组初始化的形式为
数据类型 数组名[整常量表达式][ 整常量表达式]={ 初始化数据 };
在{ }中给出各数组元素的初值,各初值之间用逗号分开。把{ }中的初值依次赋给各数组元素。
有如下几种初始化方式:
⑴ 分行进行初始化
1 int a[2][3]={{1,2,3},{4,5,6}};
在{ }内部再用{ }把各行分开,第一对{ }中的初值1,2,3是0行的3个元素的初值。第二对{ }中的初值4,5,6是1行的3个元素的初值。相当于执行如下语句:
1 int a[2][3]; 2 a[0][0]=1;a[0][1]=2;a[0][2]=3;a[1][0]=4;a[1][1]=5;a[1][2]=6;
注意,初始化的数据个数不能超过数组元素的个数,否则出错。
⑵ 不分行的初始化
1 int a[2][3]={ 1,2,3,4,5,6};
把{ }中的数据依次赋给a数组各元素(按行赋值)。即a[0][0]=1; a[0][1]=2;a[0][2]=3;a[1][0]=4;a[1][1]=5;a[1][2]=6;
⑶ 为部分数组元素初始化
1 static int a[2][3]={{1,2},{4}};
第一行只有2个初值,按顺序分别赋给a[0][0]和a[0][1];第二行的初值4赋给a[1][0]。由于存储类型是static,故其它数组元素的初值为0。注:某些C语言系统(如:Turbo C)中,存储类型不是static的变量或数组的初值也是0。
1 static int a[2][3]={ 1,2};
只有2个初值,即a[0][0]=1,a[0][1]=2,其余数组元素的初值均为0。
⑷ 可以省略第一维的定义,但不能省略第二维的定义。系统根据初始化的数据个数和第2维的长度可以确定第一维的长度。【前提是已知数组内容】
1 int a[ ][3]={ 1,2,3,4,5,6};
a数组的第一维的定义被省略,初始化数据共6个,第二维的长度为3,即每行3个数,所以a数组的第一维是2。
一般,省略第一维的定义时,第一维的大小按如下规则确定:
初值个数能被第二维整除,所得的商就是第一维的大小;若不能整除,则第一维的大小为商再加1。例如,int a[ ][3]={ 1,2,3,4};等价于:int a[2][3]={ 1,2,3,4};
若分行初始化,也可以省略第一维的定义。下列的数组定义中有两对{ },已经表示a数组有两行。
1 static int a[ ][3]={{1,2},{4}};
(5)数组的定义可以在一维处定义长度,第二维可省略,但是不能跨越定义,不能定义二维而不定义一维,也不可以一个都不定义
1 float [][]f = new float[6][];
数组的常用操作【待整理】