0

What i am suppose to do is create a method that is able to copy a 2d array. The code below is what I have so far, the first part is made for user to enter the array info and the bottom is were my problem starts. I don't know what is wrong, every time I run the code I get something like this :

Enter the no. of rows: 2

Enter the no. of columns: 2

Enter the elements: 3

Enter the elements: 7

Enter the elements: 5

Enter the elements: 8

[[I@3669ae9f

[[I@3669ae9f

This is my code:

   public static int[][] copyArray(int[][] array) throws IOException
   {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the no. of rows: ");
    int m = Integer.parseInt(br.readLine());
    System.out.println("Enter the no. of columns: ");
    int n = Integer.parseInt(br.readLine());

    int A[][] = new int[m][n]; 

    for(int row = 0; row < m; row++)
    {
        for(int column = 0; column < n; column++)
        {
            System.out.print("Enter the elements: ");
            A[row][column] = Integer.parseInt(br.readLine());
        }
    } 

    int[][] newArray = new int[A.length][];
    for(int x = 0; x < A.length; x++)
    {
        newArray[x] = A[x].clone();
        System.out.println(newArray);
    }
    return newArray;
   }
nbrooks
  • 18,126
  • 5
  • 54
  • 66
JRendon
  • 3
  • 2

3 Answers3

0

Try following line to print Array,

System.out.println(Arrays.toString(newArray));

or for deep/nested array

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

this link is related I believe.

Community
  • 1
  • 1
Muhammad Abid
  • 801
  • 4
  • 12
0

See this question for how to clone arrays... Make copy of array Java

Also, I'd refactor the code a bit. Your method performs three separate tasks:

  • populate array
  • copy array
  • print array

These should be three separate methods.

Community
  • 1
  • 1
Lucien Stals
  • 237
  • 3
  • 15
0

An Array is an object. The name of the Array is actually the name of the reference variable to it. That variable holds the starting address of the array in memory. Printing it will display that memory address (in hexadecimal) . It also indicates the element type,preceded by the left bracket character "[" to indicate that the object is an Array

System.out.println(newArray);  

It prints the object referenced in the memory by your array while if you have to print the actual contents of the array use-

System.out.println(Arrays.toString(newArray));
Ayush Kalani
  • 40
  • 1
  • 10