- November 24, 2020
- saptrxuy_learnit
- 0 Comments
- 1504 Views
- 0 Likes
- Java
Printing Multi-dimensional Arrays
By Akshata Dupare
1. Introduction
2. Main class and main method
3. Create another method for stringArray
4. Another way to print arrays
5. Factory method to print arrays
6. Add two dimensional array
7. Factory method to print arrays
8. Factory method deepToString to print Multiple or 2d arrays
9. Multi-loop to print multi-dimensional array
10. For loop to print multiple or 2d arrays
1.Introduction▲
1.1.Code▲
10:/** 20: * Class Name: StringArrayPrintExample 30: * Author: Akshata 40: * Date: 2019-11-06 50: * Details: Print multidimensional arrays 60: */
2.Main class and main method▲
2.1.Code▲
70: 80:package xv.training.core.arrayexamples; 120: 130://200 main class and main method 140:public class StringArrayPrintExample { 150: 160: public static void main(String[] args) { 170: System.out.println("Programm started\n"); 250: 260: System.out.println("\nprogramm Ended"); 270: 280: } 900: 910:}
2.2.Output▲
Programm started programm Ended
3.Create another method for stringArray▲
3.1.Code▲
180: 190: // 300 calling of stringArray 200: StringArrayPrintExample.stringArray(); 290: 300: // 300 create another method for stringArray 310: public static void stringArray() { 320: String[] strings = new String[] { "Hello", "Everyone", "Good", "Morning", "to", "All" }; 330: 340: System.out.println("\nPrint array for String Value "); 350: for (int i = 0; i < strings.length; i++) { 360: System.out.println(strings[i]); 370: 380: } 480: }
3.2.Output▲
Print array for String Value Hello Everyone Good Morning to All
4.Another way to print arrays▲
4.1.Code▲
390: // 400 another way to print arrays 400: System.out.println("\nfor (ObjectType object : CollectionOfObjects): "); 410: for (String String : strings) { 420: System.out.println(String); 430: }
4.2.Output▲
for (ObjectType object : CollectionOfObjects): Hello Everyone Good Morning to All
5.Factory method to print arrays▲
5.1.Code▲
90: 100:// 500 import Arrays.toString(...) Factory method to print arrays 110:import java.util.Arrays; 440: 450: // 500 Arrays.toString(...) Factory method to print arrays 460: System.out.println("\nFactory method to print arrays: "); 470: System.out.println(Arrays.toString(strings));
5.2.Output▲
Factory method to print arrays: [Hello, Everyone, Good, Morning, to, All]
6.Add two dimensional array▲
6.1.Code▲
210: 220: // 600 two dimensional array 230: // this is String array whose members are also String arrays 240: StringArrayPrintExample.stringArrayOfStringArray(); 490: 500: // 600 two dimensional array 510: private static void stringArrayOfStringArray() { 520: String[][] stringArray2d = new String[][] { 530: { "Siddhant", "Prashik", "Swara", "Yuven" }, 540: { "Mohini", "Kanchan" }, 550: { "Abhijit", "Niraj" } }; 880: 890: }
7.Factory method to print arrays▲
1. Using Arrays.toString(…) Factory method to print Multiple arrays
2. It prints ids of member arrays, not the items
[[Ljava.lang.String;@24d46ca6, [Ljava.lang.String;@4517d9a3,
[Ljava.lang.String;@372f7a8d]
7.1.Code▲
560: 570: // 700 Arrays.toString(...) Factory method to print Multiple arrays 580: // it prints ids of member arrays, not the items 590: // output 600: // [[Ljava.lang.String;@24d46ca6, [Ljava.lang.String;@4517d9a3, 610: // [Ljava.lang.String;@372f7a8d] 620: System.out.println("\nFactory method toString to print arrays: "); 630: System.out.println(Arrays.toString(stringArray2d));
7.2.Output▲
Factory method toString to print arrays: [[Ljava.lang.String;@24d46ca6, [Ljava.lang.String;@4517d9a3, [Ljava.lang.String;@372f7a8d]
8.Factory method deepToString to print Multiple or 2d arrays▲
Now, we use Arrays.deepToString(stringArray2d) method. It prints items of any layer of arrays properly.
8.1.Code▲
640: 650: // 800 Arrays.toString(...) Factory method to print arrays 660: // it prints items of membebr arrays also 670: System.out.println("\nFactory method deepToString to print arrays: "); 680: System.out.println(Arrays.deepToString(stringArray2d));
8.2.Output▲
Factory method deepToString to print arrays: [[Siddhant, Prashik, Swara, Yuven], [Mohini, Kanchan], [Abhijit, Niraj]]
9.multi-loop to print multi-dimensional array▲
9.1.Code▲
690: 700: // 900 multi-loop to print multi-dimensional array 710: for (int i = 0; i < stringArray2d.length; i++) { 720: 730: System.out.println("\nPrinting stringArray2d[" + i + "]:"); 740: for (int j = 0; j < stringArray2d[i].length; j++) { 750: System.out.println(stringArray2d[i][j]); 760: } 770: }
9.2.Output▲
Printing stringArray2d[0]: Siddhant Prashik Swara Yuven Printing stringArray2d[1]: Mohini Kanchan Printing stringArray2d[2]: Abhijit Niraj
10.For loop to print multiple or 2d arrays▲
10.1.Code▲
780: 790: // 1000 another for loop 800: for (String[] strings : stringArray2d) { 810: 820: System.out.println("\nPrinting array item:"); 830: for (String string : strings) { 840: 850: System.out.println(string); 860: } 870: }
10.2.Output▲
Printing array item: Siddhant Prashik Swara Yuven Printing array item: Mohini Kanchan Printing array item: Abhijit Niraj
Leave a Comment