How to print two dimensional (2D) string array in Java

3 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        String[][] arr = {{"java", "c", "c++", "c#"}, 
                          {"python", "php", "javascript", "go"}};
                       
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++)
                 System.out.format("%-11s", arr[i][j]);
            System.out.println();
        }
    }
}
  
  
  
/*
run:
  
java       c          c++        c#         
python     php        javascript go  
 
*/

 



answered Jul 20, 2020 by avibootz
0 votes
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        String[][] arr = {{"java", "c", "c++", "c#"}, 
                          {"python", "php", "javascript", "go"}};
                       
        System.out.println(Arrays.deepToString(arr));
    }
}
  
  
  
/*
run:
  
[[java, c, c++, c#], [python, php, javascript, go]] 
 
*/

 



answered Jul 20, 2020 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        String[][] arr = {{"java", "c", "c++", "c#"}, 
                          {"python", "php", "javascript", "go"}};
                       
        for (String[] col : arr) {
            for (String s : col) {
                System.out.print(s + " ");
            }
            System.out.println();
        }
    }
}
  
  
  
/*
run:
  
java c c++ c# 
python php javascript go 
 
*/

 



answered Jul 20, 2020 by avibootz

Related questions

...