Update Print2DArray.java
This commit is contained in:
parent
6b8040ffff
commit
d64976dff4
|
@ -4,26 +4,34 @@ import java.util.*;
|
||||||
|
|
||||||
public class Print2DArray{
|
public class Print2DArray{
|
||||||
|
|
||||||
public static void print2DNested(int[][] myArray){
|
public static String print2DNested(int[][] myArray){
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
for (int i = 0; i < myArray.length; i++) {
|
for (int i = 0; i < myArray.length; i++) {
|
||||||
for (int j = 0; j < myArray[i].length; j++) {
|
for (int j = 0; j < myArray[i].length; j++) {
|
||||||
System.out.print(myArray[i][j] + " ");
|
result.append(myArray[i][j]).append(" ");
|
||||||
}
|
}
|
||||||
System.out.println();
|
result.append("\n");
|
||||||
}
|
}
|
||||||
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void print2DStream(int[][] myArray){
|
public static String print2DStream(int[][] myArray){
|
||||||
Arrays.stream(myArray).flatMapToInt(Arrays::stream).forEach(num -> System.out.print(num + " "));
|
StringBuilder result = new StringBuilder();
|
||||||
|
Arrays.stream(myArray).flatMapToInt(Arrays::stream).forEach(num -> result.append(num).append(" "));
|
||||||
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void print2DDeepToString(int[][] myArray){
|
public static String print2DDeepToString(int[][] myArray){
|
||||||
System.out.println(Arrays.deepToString(myArray));
|
StringBuilder result = new StringBuilder();
|
||||||
|
result.append(Arrays.deepToString(myArray)).append("\n");
|
||||||
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void printArrayString(int[][] myArray) {
|
public static String printArrayString(int[][] myArray) {
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
for (int[] row : myArray) {
|
for (int[] row : myArray) {
|
||||||
System.out.println(Arrays.toString(row));
|
result.append(Arrays.toString(row)).append("\n");
|
||||||
}
|
}
|
||||||
|
return result.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue