The use of two-dimensional arrays in Java (indefinite long two-dimensional arrays) __java

Source: Internet
Author: User

The use of two-dimensional arrays in Java (indefinite long two-dimensional arrays), that is, the array lengths of each second dimension are different.

1 Code as follows:

Package com.demo.test; public class Test {public Test () {//TODO auto-generated constructor stub} public static void Main (string[] args
		{//TODO auto-generated method Stub int [] arrays;//declares a two-dimensional array//initializes the first dimension of the two-dimensional array arrays =new int [3][];
		Initializes the second dimension (unequal) for the two-dimensional array (int i=0;i<arrays.length;i++) {arrays[i]=new int [i+1];
			}//Initialize the two-dimensional array for (int i=0;i<arrays.length;i++) {for (int j=0;j<arrays[i].length;j++) {arrays[i][j]=i+j; ///output displays the two-dimensional array/*for (int i=0;i<arrays.length;i++) {for (int j=0;j<arrays[i].length;j++) {System.out
			. Print ("[+i+"] ["+j+"]: "+arrays[i][j]+");
		} System.out.println (); }*/for (int[] demen1:arrays) {for (int demen2:demen1) {//********* assigns values to an iteration variable and does not actually change the value of the array element *******//forea
				The loop variable in CH is equivalent to a temporary variable, and the system assigns the element in the array or collection to the temporary variable//and the temporary variable is not an element in an array or collection, it simply holds the value of the array element.
				So, by assigning values to the loop variable, you can't really change the value of the array element.
				If you want to change the value of an array element, you cannot use a foreach loop.
				demen2=-1;//System.out.print (DEMEN2); SyStem.out.print ("");
		} System.out.println ();
		} System.out.println ("--------------------------");
				For (int[] demen1:arrays) {for (int demen2:demen1) {System.out.print (DEMEN2);
			System.out.print ("");
		} System.out.println ();
 }

	}

}



2 The results of the operation are as follows:


Ps: Through the above, we can find that the two-dimensional array is each array element is a one-dimensional array of one-dimensional array.

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.