Update Print2DArray.java

This commit is contained in:
Neetika23 2023-11-24 00:28:48 +05:30 committed by GitHub
parent 6b8040ffff
commit d64976dff4
1 changed files with 17 additions and 9 deletions

View File

@ -4,26 +4,34 @@ import java.util.*;
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 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){
Arrays.stream(myArray).flatMapToInt(Arrays::stream).forEach(num -> System.out.print(num + " "));
public static String print2DStream(int[][] myArray){
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){
System.out.println(Arrays.deepToString(myArray));
public static String print2DDeepToString(int[][] 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) {
System.out.println(Arrays.toString(row));
result.append(Arrays.toString(row)).append("\n");
}
return result.toString();
}
}