2D array, output is not next to correct

import java.util.Scanner; public class Maze { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int rows = 0; int cols = 0; String arrayLine = ""; int counter = 0; rows = sc.nextInt(); cols = sc.nextInt(); arrayLine = sc.next(); char[][] array = new char[rows][cols]; for(int r=0; r<rows; r++){ for (int c=0; c<cols; c++){ array[r][c] = arrayLine.charAt(counter); counter ++; } } System.out.println(array); System.out.println(); } } 

The document from which I will provide information:

 8 7 000000011111S0000000110101111010101100010110111011010E00 

The output that I get when I run it is [[ C@252f0999

Please help, I'm just starting to learn java!

+4
source share
2 answers

array is a special kind of object, it does not have an implicit toString() , which controls the pretty printed printing of the contents of the array, which happens when the object is represented by the standard representation for objects that are its hash code.

You should use Arrays.toString() :

 for (int i = 0; i < array.length; ++i) System.out.println(Arrays.toString(array[i])); 

Remember that you cannot write Arrays.toString(array) directly, because, as stated in the documentation:

If the array contains other arrays as elements, they are converted to strings by the Object.toString () method, inherited from Object, which describes their identifiers, and not their contents.

+5
source

When you call println () on an object, Java returns the location of the resource by default (this is what C @ 252 .... is). Do you want to call

 System.out.println(Arrays.deepToString(array) 

to display data in an array.

+4
source

All Articles