JAVA's one-dimensional array, two-dimensional array, three-dimensional array, multi-dimensional array, etc ., Multidimensional

Source: Internet
Author: User

JAVA's one-dimensional array, two-dimensional array, three-dimensional array, multi-dimensional array, etc ., Multidimensional

This array can be seen as a beginner's learning. From a one-dimensional array to a multi-dimensional array, it is actually very simple. As we all know, one-dimensional and two-dimensional arrays may often be used, it should be rare to use two or more dimensions.

Public class test {public static void main (String [] args) {/* One-dimensional array */int num [] = {0, 1, 2};/* output three rows of data below, 0 ~ 2 */for (int I = 0; I <num. length; I ++) {System. out. println ("output value of one-dimensional array:" + num [I]);} System. out. println ("~~~~~~~~~~~~~~~~~~~~~~~ ");//(!) Does it seem simple and clear? Then let's look at the two-dimensional array int num1 [] [] = {0, 1, 2}, {3, 4}, {5}; // note: in fact, you can think of it as rows and columns for (int I = 0; I <num1.length; I ++) {System. out. print ("two-dimensional array output value:"); for (int j = 0; j <num1 [I]. length; j ++) {System. out. print ("" + num1 [I] [j]); // num1 [I] [j] I only describe the first line, for example, num1 [0] [1] is the {0, 1, 2} object in num1 [0.} System. out. println ();} System. out. println ("~~~~~~~~~~~~~~~~~~~~~~~ ");//(!) Are you confused about this? Come on! Next, let's look at the 3D array. Int num2 [] [] [] ={{ 0, 1, 2 },{ },{ 6 },{ 7, 8 },{ 9 }}, {10, 11 }}; for (int I = 0; I <num2.length; I ++) {System. out. print ("3D array output value:"); for (int j = 0; j <num2 [I]. length; j ++) {for (int k = 0; k <num2 [I] [j]. length; k ++) {System. out. print ("" + num2 [I] [j] [k]) ;}} System. out. println ();} System. out. println ("~~~~~~~~~~~~~~~~~~~~~~~ ");//(!) You have seen 3D, right? Or what rules have you caught? Come! Come! Again, the four-dimensional array int num3 [] [] [] [] ={{{ 0, 1, 2}, {3, 4, 5, 6 }},{{ 7, 8, 9 },{ 10, 11, 12, 13, 14 }}}; // assign or replace a value to the number in the array // example: num3 [1] [0] [0] [1] = 404; // Replace the 8 Initial Value with 404 for (int I = 0; I <num3.length; I ++) {System. out. print ("four-dimensional array output value:"); for (int j = 0; j <num3 [I]. length; j ++) {for (int k = 0; k <num3 [I] [j]. length; k ++) {for (int l = 0; l <num3 [I] [j] [k]. length; l ++) {System. out. print ("" + num3 [I] [j] [k] [l]) ;}} System. out. println ();}}}
// Likewise
//......

Console output:

Output value of one-dimensional array: 0 output value of one-dimensional array: 1 Output value of one-dimensional array: 2 ~~~~~~~~~~~~~~~~~~~~~~~ Two-dimensional array output value: 0 1 2 two-dimensional array output value: 3 4 two-dimensional array output value: 5 ~~~~~~~~~~~~~~~~~~~~~~~ 3D array output value: 0 1 2 4 5 3D array output value: 6 7 8 9 3D array output value: 10 11 ~~~~~~~~~~~~~~~~~~~~~~~ Four-Dimensional array output value: 0 1 2 3 4 5 6 four-dimensional array output value: 7 404 9 10 11 12 13 14

 

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.