Java中的二维数组的定义与学习
编程技术  /  houtizong 发布于 3年前   46
//定义二维数组写法1 class numthree{public static void main(String[] args){float[][] numthree; //定义一个float类型的2维数组numthree=new float[5][5]; //为它分配5行5列的空间大小numthree[0][0]=1.1f; //通过下标索引去访问 1行1列=1.1numthree[1][0]=1.2f; // 2行1列=1.2numthree[2][0]=1.3f; // 3行1列=1.3numthree[3][0]=1.4f; // 4行1列=1.4numthree[4][0]=1.5f; // 5行1列=1.5System.out.println(numthree[0][0]); //打印换行输出喽 System.out.println(numthree[1][0]);System.out.println(numthree[2][0]);System.out.println(numthree[3][0]);System.out.println(numthree[4][0]);}}//定义二维数组写法2 定义的同时分配空间大小class numfour{public static void main(String[] args){ short[][] numfour=new short[5][8]; //定义一个short类型的数组同时为它分配5行8列的空间大小 numfour[0][7]=10; numfour[1][6]=20; numfour[2][5]=30; numfour[3][4]=40; numfour[4][3]=50; System.out.println(numfour[0][7]); System.out.println(numfour[1][6]); System.out.println(numfour[2][5]); System.out.println(numfour[3][4]); System.out.println(numfour[4][3]);}}//定义二维数组写法3 不规则数组 class numfive{public static void main(String[] args){long[][] numfive=new long[5][]; //定义一个long类型的不规则数组 numfive[0]=new long[5]; //为第1行分配5列numfive[1]=new long[6]; //为第2行分配6列numfive[2]=new long[7]; //为第3行分配7列numfive[3]=new long[8]; //为第4行分配8列numfive[4]=new long[9]; //为第5行分配9列numfive[0][4]=10000000000L; //1行5列=10000000000numfive[1][5]=20000000000L; //2行6列=20000000000numfive[2][6]=30000000000L; //3行7列=30000000000numfive[3][7]=40000000000L; //4行8列=40000000000numfive[4][8]=50000000000L; //5行9列=50000000000System.out.println(numfive[0][4]); //打印换行输出喽 System.out.println(numfive[1][5]);System.out.println(numfive[2][6]);System.out.println(numfive[3][7]);System.out.println(numfive[4][8]);System.out.println(numfive[4][7]);//打印输出一个没有定义数组元素的数组 java会自动将他初始化值为0 } }
//定义2维数组写法4 定义的同时赋初始值 class numsix { public static void main(String[] args) { double[][] numsix={{1.111D,2.222D,3.333D},{4.444D,5.555D,6.666D}};//定义double型的数组分配3行3列的空间同时赋值 System.out.println(numsix[0][0]); //打印换行输出1行1列=1.111 System.out.println(numsix[1][1]); //打印换行输出2行2列=5.555 } } //定义2维数组写法5 定义不规则的2维数组同时赋初始值 class numseven{public static void main(String[] args){int[][] numseven=new int[][]{{10,20,30},{40,50},{60}};System.out.println(numseven[0][2]);System.out.println(numseven[1][1]);System.out.println(numseven[0][0]);}}//定义2维数组写法6 定义不规则的2维数组同时赋初始值; class numeight{public static void main(String[] args){int[][] numeight={{100,200,300,400},{500,600,700,800},{900,1000,1100,1200,1300}};System.out.println(numeight[0][2]);System.out.println(numeight[1][2]);System.out.println(numeight[2][1]);}}
//一维数组定义与输出 class less02{ public static void main(String[] args) { int stu[]=new int[]{1,2,3}; //方法一 //int stu[]={1,2,3}; 方法二 //int []stu=new int[]{1,2,3}; 方法三 //int[] stu={1,2,3}; 方法四 for(int i=0;i<stu.length;i++) System.out.println(stu[i]); }}//二维数组定义与输出 class less02 { public static void main(String args[]) { //int [][] num=new int[][]{{1,2,3},{4,5,6}}; 方法一 int [][] num={{1,2,3},{4,5,6}}; //方法二 for (int i=0;i<num.length ;i++ ) { for (int j=0;j<num[i].length ;j++ ) { System.out.print(num[i][j]+" "); } System.out.println(); } } }
//定义包含三个元素的一维数组 int[] a = new int[3]; a = {1,2,3};//方法1,先new对象,然后赋值 int[] b = {1,2,3}; //方法2,直接赋值 int[] c = new int[3]{1,2,3}; //方法3,new后直接赋值
//定义一个3行5列的二维数组 int[][] a = new int[3][5]; a = {{"a","a","a","a","a"}, {"b","b","b","b","b"}, {"c","c","c","c","c"} }; //方法1 int[][] b = { {"a","a","a","a","a"}, {"b","b","b","b","b"}, {"c","c","c","c","c"} };//方法2 int[][] c = new int[3][5]{{"a","a","a","a","a"}, {"b","b","b","b","b"}, {"c","c","c","c","c"} };
这样定义是正确的:int[][] d = new int[3][]; 这样定义是错误的:int[][] d = new int[][4]; int[][] d = new int[][]; 也可以定义不规则数组: arr = new int[2][]; arr[0] = new int[3]; arr[1] = new int[5];
int[][] arr = new int[2][3];
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接