The concept and usage of Java two-dimensional arrays

Source: Internet
Author: User

Two-dimensional arrays

An array of arrays---each element of a two-dimensional array is a one-dimensional array

Defining formats

data type [] Array name = new data type [length of two-dimensional array/number of one-dimensional array included] [length of each one-dimensional array];

int[][] arr = new INT[3][5];---defines a two-dimensional array of integers that contains 3 one-dimensional arrays, each one-dimensional array can store 5 integers

ARR[0]---a one-dimensional array at a location labeled 0

ARR[1][3]---If you want to get a specific element requires two subscript

data type [] Array name = new data type [length of two-dimensional array/number of contained one-dimensional array] [];

int[][] arr = new int[3][];----represents a two-dimensional array of one-dimensional arrays that contain three integers (-------------this sentence is well understood------------------------)

data type [] Array name = {{element},{element 1, Element 2},......};

Int[][] arr = {{2,5},{1},{3,2,4},{1,7,5,9}};

Note: [] before the variable name, it is the data type, and if [] after that, it belongs to the current variable name.

Application of two-dimensional array

Length of two-dimensional array: array name. length---each one-dimensional array: array name [subscript].length

Two-dimensional array traversal---dual for loop

For (// traversal of a two-dimensional array, each element traversed is a one-dimensional array for (// traversal of a one-dimensional array at the corresponding location system.  out. println (Arr[i][j]);}}      

Inversion of two-dimensional arrays---kinsoku

for (0, end = arr.length-1; start < end; start++,end--) {int[] temp = Arr[start];arr[start] =
          
            Arr[end];arr[end] =
            temp;} 
              

Enter the number of rows from the console to print the corresponding Yang Hui triangle

//Get the number of rows from the console scanner s =New Scanner (System.Inch);int row =S.nextint ();//A two-dimensional array is defined according to the number of rows, because the number of elements in each row is different, so the number of each row is undefinedint[][] arr =NewInt[Row] [];//Traversing a two-dimensional arrayForint i =0; i < row; i++){//Initialize this one-dimensional array of each row arr[i] =NewInt[i +1];//Iterate through this one-dimensional array and add elementsForInt J =0; J <= I; J + +){// The beginning and ending elements of each column are 1, at the beginning, j=0, at the end, j=i if (j = = 0 | | J == i) {Arr[i][j] = 1;} else {// Each element is the sum of the elements of its previous line and the diagonal elements arr[i][j] = arr[i-1][j] + arr[i-1][ J-1];} System. out.print (Arr[i][j] +  "\t  ");} System. out.println ();}       

Results:

1

1 1

1 2 1

1 3 3 1

1 4 6) 4 1

1 5 10 10 5 1

Yang Hui triangle with a two-dimensional array of understanding:

ARR[I][J] = Arr[i-1][j] + arr[i-1][j-1]

The concept and usage of Java two-dimensional arrays

Related Article

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.