数组是一组同类型的数据或对象的集合。每个数组有一个名称,称为数组名,数组中的每个数据,称为数组元素。
数组的声明及其初始化
int a[]=new int[5]; //声明时必须声明数组容量
上面这种方式初始化时,数组的元素会有默认值,数字类型的默认值是0,对象的默认类型是null
int a[]=new int[]{1,2,3,4,5};
数组的一个重要属性——length
“length” 用于获取数组中元素的长度(元素的个数)。
数组一旦初始化,其长度是不可变的。
例如1:
int a[]=new int[10]; for(int i=0;i<a.length;++i){ //注意:a.length=10 a[i]=i*i; }
例如2:
int b[][]=new int[6][5]; for(int i=0;i<b.length;++i){ //注意:b.length=6 for(int j=0;j< b[i].length;++j){ //注意:b[i].length=5 a[i][j]=i*j; } }
利用数组求1~100的和
public class test { public static void main(String[] args) { int[] ia = new int[101]; //创建数组ia,数组的下标从0到100,共101个元素 for (int i = 0; i < ia.length; i++) { //利用循环语句,给数组元素一次赋值 ia[i] = i; } int sum = 0; for (int i = 0; i < ia.length; i++) { //利用循环,累加求出所有元素的和值 sum += ia[i]; } System.out.println(sum); //输出和值 } }
二维数组的创建
二维数组的创建与一维数组类似,不同的是,需要指定两个数值(行数的值,列数值),
二维数组的创建有两种方式:
(1)方法一:直接分配空间(new),
例如:
int a[][]; a= new int[2][3]; //该数组2行3列,共5个元素
(2)方法二: 从最高维开始,为每一维分配空间,
例如:
int c[][]; c = new int[2][]; //定义2行 c[0] = new int[4]; //定义第一行有4个元素 c[1] = new int[3]; //定义第二行有3个元素
该数组为2行,共有7个元素,其中第一行4个元素,第2行3个元素。
初始化二维数组
初始化二维数组与一维数组类似,不同的是按行给出每行的元素值。
例如:
int a[][] = {{1,2,3}, {3,4,5}}; //数组是2行3列
该语句所形的数组为:
a[0][0]=1 a[0][1]=2 a[0][2]=3
a[1][0]=3 a[1][1]=4 a[1][2]=5
再例如:
int b[][] = {{10,20,30}, {1,2,3,4,5}}; //数组是2行,第一行3列,第2行5列。
求数组里数组元素的加和
public class test { public static void main(String[] args) { int[][] a = new int[][] { {0,1,2},{3,4,5,6},{7,8,9,10} }; int res = 0; for(int i = 0; i < a.length; i++) { for(int j = 0; j < a[i].length; j++) { res +=a[i][j]; } } System.out.println(res); } }
分析程序,给出运行结果。注意:该程序,采用对每个元素单独进行赋值,未赋值的采用默认值。
public class test { public static void main(String[] args) { int a[][] = new int[3][3]; a[0][0] = 1; a[1][1] = 2; a[2][2] = 3; System.out.println("数组a: "); for (int i = 0; i < a.length; i++) { //i控制行数 for (int j = 0; j < a[i].length; j++) { //j控制列数 System.out.print(a[i][j] + " "); } System.out.println(); } } }
String[] strs = new String[5];
String[] strs = new String[]{"a","b","c","d","e"};
例:分析程序,理解字符串数组,并给出运行结果
public class test { public static void main(String[] args) { //利用字符串常量创建并初始化数组 String[] anArray1 = { "String One", "String Two", "String Three"}; //以下分别声明、创建、赋值字符串数组 String[] anArray2; //声明字符串数组 anArray2=new String[3]; //创建字符串数组 anArray2[0]="xxxxxxx"; //赋值字符串数组 anArray2[1]="yyyyyyy"; anArray2[2]="aaaaaaa"; System.out.println("第一个字符串数组中的3个串值:"); for (int i = 0; i < anArray1.length; i++){ System.out.println( anArray1[i]); } System.out.println("第二个字符串数组中的3个串值:"); for (int i = 0; i < anArray2.length; i++){ System.out.println( anArray2[i]); } } }
int [] x,y[]; //x是一维数组,y是二维数组
1、求最大值
public class test { public static void main(String[] args) { int[] a = new int [] {4,2,7,1,3,5}; int max = a[0]; //假设a[0]是最大值 for(int i = 0; i < a.length ; i++) { if(max < a[i]) { max = a[i]; //存放目前最大值 } } System.out.println("max: " + max); } }
2、求最小值 (同上)
public class test { public static void main(String[] args) { int[] a = new int [] {4,2,7,1,3,5}; int min = a[0]; //假设a[0]是最小值 for(int i = 0; i < a.length ; i++) { if(min > a[i]) { min = a[i]; //存放目前最小值 } } System.out.println("min: " + min); } }
3、总和、平均数
public class test { public static void main(String[] args) { int[] a = new int [] {4,2,7,1,3,5}; double res = 0; for(int i = 0; i < a.length ; i++) { res += a[i]; } System.out.println("总和: " + res); System.out.println("平均数:" + (res / a.length)); } }
4、 数组的复制
public class test { public static void main(String[] args) { int[] a = new int [] {4,2,7,1,3,5}; //声明一个与a长度一致的数组 int[] copy = new int[a.length]; for(int i = 0; i < a.length ; i++) { copy[i] = a[i]; } for(int i = 0; i < a.length ; i++) { System.out.print(copy[i]+" "); } } }
5、数组的反转
public class test { public static void main(String[] args) { int[] a = new int [] {4,2,7,1,3,5}; //声明一个与a长度一致的数组 int[] temp = new int[a.length]; int k = 0;//temp元素的下标 for(int i = a.length - 1; i >= 0; i--) { temp[k] = a[i]; k++; } for(int i = 0; i < a.length ; i++) { System.out.print(temp[i]+" "); } } }
6、数组排序
冒泡排序
冒泡排序动态图
例如4,7,3,1进行冒泡排序(从小到大)
第一次排序结果:4,3,1,7(得到一个最大的数字,放在倒数第一位)
第二次排序结果:3,1,4,7(得到除最后一个数字外的最大数字,放在倒数第二位)
第三次排序结果:1,3,4,7
public class test { public static void main(String[] args) { int[] a = new int [] {4,7,3,1}; for(int i = 0; i < a.length - 1 ; i++) { //外层循环是循环轮次,轮次循环的次数是数组长度-1 for(int j = 0; j < a.length-1-i; j++) { if(a[j] > a[j+1]) { int temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } System.out.println("从小到大排序后的结果是:"); for(int i = 0; i < a.length; i++) { System.out.print(a[i]+" "); } } }