当前位置: 移动技术网 > IT编程>开发语言>Java > java定义二维数组的几种写法(小结)

java定义二维数组的几种写法(小结)

2019年07月22日  | 移动技术网IT编程  | 我要评论

如下所示:

//定义二维数组写法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.1
numthree[1][0]=1.2f;                 // 2行1列=1.2
numthree[2][0]=1.3f;                 // 3行1列=1.3
numthree[3][0]=1.4f;                 // 4行1列=1.4
numthree[4][0]=1.5f;                 // 5行1列=1.5
system.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列=10000000000
numfive[1][5]=20000000000l;     //2行6列=20000000000
numfive[2][6]=30000000000l;     //3行7列=30000000000
numfive[3][7]=40000000000l;     //4行8列=40000000000
numfive[4][8]=50000000000l;     //5行9列=50000000000
system.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]);
}
}

以上就是小编为大家带来的java定义二维数组的几种写法(小结)全部内容了,希望大家多多支持移动技术网~

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网