声明数组的同时,根据指定的长度分配内存,但数组中元素值都为默认 的初始化值
int[] ary0 = new int[10];
声明数组并分配内存,同时将其初始化
int[] ary1 = new int[]{1, 2, 3, 4, 5};
与前一种方式相同,仅仅只是语法相对简略
int[] ary2 = {1,2,4,5,6};
数组还可以分为静态和动态两种
动态初始化数组`
import java.util.Scanner; public class test{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int []a = new int[10]; for (int i = 0; i < 10; i++) { a[i] = scanner.nextInt(); } } } // 这样就做到了对数组元素的赋值
静态初始化数组
int [] num = new int []{1,2,3,4,5}
num.length
数组元素的访问:
数组名字[索引] 例如: a[0],a[1];
数组的索引从0开始
索引的数据类型是整型
索引的最大值和数组长度始终差1
int [] b1 = new int[]{1,2,3,4,5,6,7}; for(int i = 0 ; i < b1.length; i++){ System.out.println(b1[i]); }
int [] b1 = new int []{1,2,3,4,5,6,7}; for(数组元素的类型 临时变量名字 :数组的名字){ System.out.println(临时变量名字 ); } 即: for(int x:b1){ System.out.println(x); }
package day4; import java.util.Arrays; public class Shuzu { public static void main(String[] args) { int [] a = {1,2,3,4,5,6,7}; float [] c = {1,2,4,5,6,7}; for (int i = 0; i <4 ; i++) { System.out.println(a[i]); } int [] b = new int[10]; char [] d = {'d','f'}; System.out.println(b); //如果数组为空数组,则输出数组会打印出数组的地址 System.out.println(Arrays.toString(b)); //用 Arrays类里面的toString方法 可以字符串化输出数组; System.out.println(Arrays.toString(a)); System.out.println(Arrays.toString(c)); System.out.println(Arrays.toSting(d)); } }
package day5; import java.util.Arrays; //冒泡排序 public class maopao{ public static void swap(int []arr , int a,int b){ //swap方法 int tmp = arr[a]; arr[a] = arr[b]; arr[b] = tmp; } public static void main(String[] args) { int [] a={3,5,2,1,8,7,5,8,9}; for (int i = 0; i < a.length-1; i++) { //外层循环控制判断的趟数 for (int j = 0; j < a.length-1-i; j++) { //内层循环控制相邻数的比较 if(a[j]>a[j+1]){ //判断大小,将小的数字放到数组的前面 swap(a, j, j+1); } } } System.out.println(Arrays.toString(a)); //输出数组 } }
package day5; import java.util.Arrays; public class Xuanze { //未优化版 /* 4 5 3 1 2 4 5 3 1 2 3 5 4 1 2 1 5 4 3 2 */ public static void main(String[] args) { int []a = {4,5,3,1,2,7,8,2,4,5}; for (int i = 0; i < a.length-1; i++) { for (int j = i+1; j<a.length-1; j++) { // j = 1 j < 4 i == 0; if(a[i]>a[j]){ //第一个数大于后面的数字 , i 每次向右挪一位,j从i挪动的那一位的下一位开始计算 int temp = a[i]; a[i] = a[j]; a[j] = temp; } } } System.out.println(Arrays.toString(a)); } }
二维数组中的每一个元素都是一个一维数组
例:int [][]a = {{1,2,3},{4,5,7},{9,10,11}};
二维数组的声明方式和一维数组相似
int [][]a = new int[][]{{1,2,3},{1,2,3},{1,2,3}};
int [] [] b = {{1,2,3},{1,2,3},{1,2,3}};
int [][] c = new int[3][5];
int[][] arr = new int[3][5];—定义了一个整型的二维数组 ,这个二维数组有3
个一维数组,每一个一维数组包含5个元素
package day6; import java.util.Arrays; public class shuzu2 { public static void main(String[] args) { //int [] a = new int[3]; // char [][] a = new char[][]; int [][] a = {{1,3,2},{3,5,4},{4,5,7}}; for (int i = 0; i < a.length;i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } }
代码示例2
package day6; public class Qipan { public static void main(String[] args) { char [][] a = new char[11][11]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { a[i][j] = '+'; } } for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } }