Initialization and basic operation examples of two-dimensional arrays in Java programming _java

Source: Internet
Author: User

Declaration and initialization of Java two-dimensional arrays

1. A two-D array can be regarded as an array of elements of an array;
The Declaration and initialization of two-dimensional arrays in 2.Java should be carried out in order from high to low dimensions.


Instance:
Static initialization:
Array2.java:

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:

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 [instance]:

public class lesson{public static void Main (String [] args) {//two-D array declaration mode://data type [] [] Array name = new data type [ 
    Length] [length]; 
     
    The data type [] [] Array name = {{123},{456}}; /* int [] [] arr = {{123},{456}}; A two-dimensional array with two rows of three columns defined and assigned for (int x = 0; x<arr.length; x + +) {//Locate line for (int y = 0; y<arr[x].length; y++) {//Locate each line 
      Number of elements System.out.print (Arr[x][y]); 
    } System.out.println ("n"); } */int [] num = new int [3][3]; Two-dimensional array num[0][0 with three rows and three columns defined] = 1; Assign a value to the first element of the first line 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 a value to the first element of the second line num[1][1] = 5; Assign the second element to the second row num[1][2] = 6; Assign a value to the third element of the second line num[2][0] = 7; Assign a value to the first element of the third line num[2][1] = 8; Assign a value to the second element of the third row num[2][2] = 9;  
        Assign a third row to the third element for (int x = 0; x<num.length; x + +) {//Locate 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"); 
    }}//array value Arr[x][y] represents the value specified in column y of line X. When using a two-dimensional array object, note the length represented by length, and/or the array name, followed by a direct length (such as Arr.length), which refers to a few rows (row);//Specify the index followed by length (such as Arr[0].length), 

 Refers to the element that the row owns, that is, the number of columns (column).

Take another example: two two-dimensional arrays, incorporating them into a new two-dimensional array, with elements of two array corresponding elements. Input two array: {{1,5},{2,3},{6,5}},   {4,2},{2,6},{5,7}}
Output print: {{5,7},{4,9},{11,12}}}

 code: class arraysctrl{static void Arraysadd (int[][] a,int[][] b) {StringBuffer Sbre 
    Sult = 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); } 
} 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.