java defines several ways to write a two dimensional array

  • 2020-05-10 18:08:59
  • OfStack

As follows:

// define 2 Dimensional array notation 1 
class numthree
{
public static void main(String[] args)
{
float[][] numthree;       // define 1 a float The type of 2 Dimensional array 
numthree=new float[5][5];    // Allocated for it 5 line 5 The size of the column space 
numthree[0][0]=1.1f;      // Access by subscript index    1 line 1 column =1.1
numthree[1][0]=1.2f;                 // 2 line 1 column =1.2
numthree[2][0]=1.3f;                 // 3 line 1 column =1.3
numthree[3][0]=1.4f;                 // 4 line 1 column =1.4
numthree[4][0]=1.5f;                 // 5 line 1 column =1.5
System.out.println(numthree[0][0]); // Print the newline output 
System.out.println(numthree[1][0]);
System.out.println(numthree[2][0]);
System.out.println(numthree[3][0]);
System.out.println(numthree[4][0]);
}
}

// define 2 Dimensional array notation 2   Define and allocate the size of the space 
class numfour
{
public static void main(String[] args)
{
  short[][] numfour=new short[5][8]; // define 1 a short An array of type is assigned to it at the same time 5 line 8 The size of the column space 
  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]);
}
}

// define 2 Dimensional array notation 3     Irregular array 
class numfive
{
public static void main(String[] args)
{
long[][] numfive=new long[5][];   // define 1 a long An irregular array of type 
numfive[0]=new long[5];       // For the first 1 Line distribution 5 column 
numfive[1]=new long[6];       // For the first 2 Line distribution 6 column 
numfive[2]=new long[7];       // For the first 3 Line distribution 7 column 
numfive[3]=new long[8];       // For the first 4 Line distribution 8 column 
numfive[4]=new long[9];       // For the first 5 Line distribution 9 column 
numfive[0][4]=10000000000L;     //1 line 5 column =10000000000
numfive[1][5]=20000000000L;     //2 line 6 column =20000000000
numfive[2][6]=30000000000L;     //3 line 7 column =30000000000
numfive[3][7]=40000000000L;     //4 line 8 column =40000000000
numfive[4][8]=50000000000L;     //5 line 9 column =50000000000
System.out.println(numfive[0][4]); // Print the newline output 
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]); // A printout 1 An array that does not have an array element defined  Java It will automatically initialize it to zero 0
}
}

// define 2 Dimensional array notation 4   Define and assign initial values at the same time 
class numsix
{
public static void main(String[] args)
{
double[][] numsix={{1.111D,2.222D,3.333D},{4.444D,5.555D,6.666D}};// define double Type array allocation 3 line 3 The space of the column is assigned at the same time 
System.out.println(numsix[0][0]); // Print the newline output 1 line 1 column =1.111
System.out.println(numsix[1][1]); // Print the newline output 2 line 2 column =5.555
}
}

// define 2 Dimensional array notation 5   undefined 2 The dimension array is initialized at the same time 
class numseven
{
public static void main(String[] args)
{
int[][] numseven=new int[][]{{10,20,30},{40,50},{60}}; // There's nothing to say if you don't understand   Then stop learning! 
System.out.println(numseven[0][2]);
System.out.println(numseven[1][1]);
System.out.println(numseven[0][0]);
}
}

// define 2 Dimensional array notation 6  undefined 2 The initial value of the dimension array is assigned at the same time. 
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]);
}
}

Related articles: