0

I am trying to return a 2D array from a method but I am returning some weird code back instead; I was expecting that to return a 3x3 array filled in

|x,x,x|
|x,x,x|
|x,x,x|

but I am getting back this code [[I@3cd1a2f

any help?

import java.util.*;

class Untitled {
    public static void main(String[] args) {
        System.out.print(test());
    }
    public static int[][] test(){
        int[][] result = new int[3][3];
    
        for(int i=0; i< 3 ; i++) // i = x
        {
            for(int j=0; j<3; j++) // j = y  result = [i][j]
            {
                result[i][j] = i;
            }
        }
        
        return result;
    }
}
  • `System.out.print(Arrays.deepToString(test()));` – Youcef LAIDANI Nov 16 '20 at 17:41
  • 1
    Java arrays don't provide a decent toString() implementation, see: https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array – stridecolossus Nov 16 '20 at 17:41
  • Aside from other comments, you seem to be confusing *returning* and *printing*. See [Differences between System.out.println() and return in Java](https://stackoverflow.com/q/25456472). In short print lets us *show* some value (for instance on the screen), while *returning* lets the application use that value (for instance it can add it to some other value, like `int totalPrice = 0; for (Item item : shoppingCart){ totalPrice = totalPrice + item.getPrice();}`. As you see `item.getPrice()` would *return* some value which application can use for instance to calculate total cost). – Pshemo Nov 16 '20 at 17:48

0 Answers0