Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing a 2d array in Java like a table [duplicate]

I'd like to print an inputed 2-dimensional array like a table i.e if for some reason they put in all 1s...

1 1 1 1 

1 1 1 1 

1 1 1 1

1 1 1 1 

Just like so above but in the console on Java eclipse, no fancy buttons and GUI's but in the console, here is what I have....

    import java.util.Scanner;
    public class Client {
        public static void main(String[] args){
            Scanner input = new Scanner(System.in);

            int[][] table = new int[4][4];
            for (int i=0; i < table.length; i++) {
                for (int j=0; j < table.length; j++) {
                    System.out.println("Enter a number.");
                    int x = input.nextInt();
                    table[i][j] = x;
                    System.out.print(table[i][j] + " "); 
                } 
                    System.out.println();
        }
        System.out.println(table);
    }
}

And this is what I get when I input everything and the console terminates:

Enter a number.

1

1 Enter a number.

1

1 Enter a number.

1

1 Enter a number.

1

1 

[[I@3fa1732d
like image 726
Adam Calcanes Avatar asked Jan 06 '14 21:01

Adam Calcanes


People also ask

How do you find duplicates in a 2D array?

Here's a brute-force straight forward way of counting duplicates. Turn the 2d array into a 1d array ( List<Integer> ), then loop through the 1d array counting the duplicates as you find them and removing them so you don't count them more than once.

Can you print a 2D array in Java?

Java provides multiple ways to print a 2d array, for example nested for-loop, for-each loop, Arrays. deepToString() method, etc. Each approach follows a different procedure, but all of them can still accomplish the same goal, i.e., printing a 2D array.


2 Answers

Consider using java.util.Arrays.

There is a method in there called deepToString. This will work great here.

System.out.println(Arrays.deepToString(table));

Relevant here: Simplest way to print an array in Java

like image 146
Obicere Avatar answered Sep 20 '22 23:09

Obicere


You need to print out the array separately from entering the number. So you can do something like this:

public class PrintArray {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int[][] table = new int[4][4];
        for (int i = 0; i < table.length; i++) {
            for (int j = 0; j < table.length; j++) {
                // System.out.println("Enter a number.");
                int x = input.nextInt();
                table[i][j] = x;
            }
            //System.out.println();
        }
        // System.out.println(table);

        for (int i = 0; i < table.length; i++) {
            for (int j = 0; j < table[i].length; j++) {
                System.out.print(table[i][j] + " ");
            }
            System.out.println();
        }
    }
}
like image 45
tonga Avatar answered Sep 19 '22 23:09

tonga