Examples of initialization and basic manipulation of two dimensional arrays in Java programming

  • 2020-04-01 04:20:05
  • OfStack

Declaration and initialization of Java two-dimensional arrays

1. A two-dimensional array can be regarded as an array with an array as an element;
2. Two-dimensional arrays in Java should be declared and initialized in the order from high to low dimensions.


Example:
Static initialization:
Array2. Java:

The program code


public class Array2 
{ 
  public static void main(String args[]) { 
    int a[][] = {{1,2},{3,4,5,6},{7,8,9}} ; 
    for(int i=0 ; i <a.length ; i++) { 
      for(int j=0 ; j<a[i].length ; j++) { 
        System.out.println("a[" + i + "][" + j + "]=" + a[i][j]) ; 
      } 
    } 
  } 
} 

Dynamic initialization:

The program code


public class Array2D 
{ 
  public static void main(String args[]) { 
    int i , j ; 
    String s[][] ; 
    s = new String[3][] ; 
    s[0] = new String[2] ; 
    s[1] = new String[3] ; 
    s[2] = new String[2] ; 
    for(i=0 ; i<s.length ; i++) { 
      for(j=0 ; j <s[i].length ; j++) { 
        s[i][j] = new String(" My position is: " + i + "," + j) ; 
      } 
    } 
    for(i=0 ; i<s.length ; i++) { 
      for(j=0 ; j<s[i].length ; j++) { 
        System.out.println(s[i][j]) ; 
      } 
    } 
  } 
} 

About two-dimensional array operations in Java [example] :


public class Lesson{ 
  public static void main(String [] args){ 
   
    //Declaration of two-dimensional array:
    //Data type [][] array name = new data type [length][length];
    //Data type [][] array name = {{123},{456}};
     
    /* 
    int [][] arr = {{123},{456}}; //Defines and assigns a two-dimensional array of two rows and three columns
    for(int x = 0; x<arr.length; x++){ //Positioning line
      for(int y = 0; y<arr[x].length; y++){ //Locate the number of elements per row
        System.out.print(arr[x][y]); 
      } 
      System.out.println("/n"); 
    } 
    */ 
    int [][] num = new int [3][3]; //Defines a two-dimensional array of three rows and three columns
    num[0][0] = 1; //Assign a value to the first element in the first row
    num[0][1] = 2; //Assign a value to the second element in the first row
    num[0][2] = 3; //Assign a value to the third element in the first row
     
    num[1][0] = 4; //Assign the first element in the second row
    num[1][1] = 5; //Assign a value to the second element in the second row
    num[1][2] = 6; //Assign a value to the third element in the second row
     
    num[2][0] = 7; //Assign the first element in the third row
    num[2][1] = 8; //Assign a value to the second element in the third row
    num[2][2] = 9; //Assign a value to the third element in the third row
    for(int x = 0; x<num.length; x++){ //Positioning line
      for(int y = 0; y<num[x].length; y++){ //Locate the number of elements per row
        System.out.print(num[x][y]); 
      } 
      System.out.println("/n"); 
    } 
  } 
} 
//The array value arr[x][y] indicates that the specified value is row x and column y.
//When using two-dimensional array objects, pay attention to the length represented by the length,
//After the array name, add the length(such as arr.length), which refers to the number of lines (Row).
//Specify the index followed by a length(such as arr[0].length), which refers to the number of elements that the row has, that is, the number of columns.

Let's take another example: two two-dimensional arrays, and combine them into a new two-dimensional array, with the elements being the sum of the corresponding elements of the two arrays. Enter two arrays: {{1,5},{2,3},{6,5}},& noon;   {{4, 2}, {2}, {5, 7}}
Output print: {{5,7},{4,9},{11,12}}


 Code:  
class arraysCtrl{ 
  static void arraysAdd(int[][] a,int[][] b) 
  { 
    StringBuffer sbResult = new StringBuffer("{"); 
    int[][] result = new int[a.length][b.length]; 
    for(int i=0;i<a.length;++i) 
    { 
      sbResult.append("{"); 
      for(int j=0;j<a[i].length;++j) 
      { 
         
        result[i][j] = a[i][j]+b[i][j]; 
        sbResult.append(result[i][j]); 
        if(j == a[i].length-1) 
        { 
          if(i == a.length-1) 
          { 
            sbResult.append("}"); 
          }else{ 
            sbResult.append("},"); 
          }           
          break; 
        } 
        sbResult.append(","); 
      } 
      if(i == a.length-1) 
      { 
        sbResult.append("}"); 
        break; 
      } 
         
    } 
    System.out.println(sbResult); 
  } 
  public static void main(String[] args) { 
 
    int[][] aTst1 = {{1,5},{2,3},{6,5}}, aTst2 = {{4,2},{2,6},{5,7}}; 
    arraysAdd(aTst1,aTst2); 
  } 
} 



Related articles: