Array of references to objects of the class

Hisham :

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++;
    }
Idle_Mind :

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);
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Django mptt Multiple references to class objects

Are objects prefetched from an array of references in Java?

How to create an array of references that includes references to different subclasses of a class

Passing array of class objects

Deleting an array of Class Objects

Array of objects class instantiation

PyYAML - Serialize classes (types / class references) that are members of objects

why references to members of a class in a vector point to the same value for different objects?

Implementing references to objects of many classes in the constructor of a new class with parameters

Iterate array of weak references where objects conform to a protocol in Swift

In Java, defining an array allocates space for references or for the objects themselves?

AngularJS Convert String to Object/Array that references the objects in the $scope

Difference between Objects of class and objects-array

How to allocate an array of class objects

Use an array of objects in another class

Pass an array of objects into another class

select array of class objects field

Array of class objects in Python 3.4

Transmute array of objects to class instances

Is there a way to add array objects to a class?

Adding Objects to Array in a different class

Creating an array of all objects of a class

Accessing a class function with array of objects

Instantiating an array of objects within a class

Pass array of objects into another class

Using an array to create objects in a class

Creating an array of objects within a class

JArray Objects to a Class with array property

Lifetimes and references to objects containing references