-4

What the wrong in the code, it is just show Array memory address instead of value in first index info[0][0].

    car camry = new car();
    car[][]info;
    info = new car[2][3];
    info[0][0] = camry;
   
   
    int i = 0;
    while( i < info.length)
    {
        int j = 0;
      do 
      {
          
        
        System.out.println( info[i][j]);
        j++;
      }
      while(j < info[0].length);
      i++;
    }
Hisham
  • 25
  • 4
  • 4
    I recommend you to post your code directly instead of posting an image of your code. Anyhow, even your image is broken and we can't see much at the moment. – msmolcic Jul 27 '20 at 21:45
  • 2
    you need to implement the `toString` of the `car` class. – f1sh Jul 27 '20 at 22:14
  • 1
    You've only populated one spot your 2D array. The `println()` line will call the `.toString()` method of whatever is contained within. Since there is nothing there after that first slot, you'll get a NullPointerException... – Idle_Mind Jul 27 '20 at 22:43

1 Answers1

1

Another way to iterate over all the cars, and handle if there is no car there:

for(car[] cars : info)
{
    for(car c : cars)
    {
        if (c != null)
        {
            // you still need to override toString() in car
            // if you want to see something other than the default memory address
            System.out.println(c);
        }
    }
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40