Rapid Readers
Rapid Readers

Reputation: 315

2D array of multiple different objects

For my project, I need a 2D array that can hold multiple different object types. The problem with java is that it doesn't let you do that. You can only have an array of a particular object type.

Now don't get me wrong, but I did some research, and one proposed solution is to use an Object array, since every class in java extends the object class by default. For example:

Object[][] someArray = new Object[5][5];
someArray[1][2] = new Cat();
someArray[3][4] = new Dog();

The problem with this is that the object class, being a superclass, cannot access the methods and fields of the subclasses that extend it. So if I have a public eat() method in my cat or dog class, the elements in someArray wont be able to access that method.

So I'm at a dead-end here. Any help is appreciated. Thanks in advance.

Upvotes: 1

Views: 1328

Answers (4)

Rafael Coelho
Rafael Coelho

Reputation: 166

The ideal use in your case is to create a superclass called Animal that is extended by dog and cat. Your ArrayList or array will be of Animal type.

Upvotes: 0

Caojinjin008
Caojinjin008

Reputation: 11

to call the eat() method of the Dog class and `Cat`` class, You can cast the array reference to a subclass reference.((Dog)someArray[x][y]).eat();

public class Dog{

    public Dog(){
        super();
    }

    public String eat(){
      String str = "yum";
      return str;
    }

    public static void main (String[]args){
        Object [][] arr = new Object [2] [2];
        for(int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr[i].length; j++){

               arr[i][j] = new Dog(); 
            }
        }
       System.out.println(((Dog)arr[1][1]).eat());
    }
}

Upvotes: 1

Rapid Readers
Rapid Readers

Reputation: 315

[ please don't judge me for answering my own question ]

One possible solution is using 2D ArrayLists. The way a 2D Array works is by literally having a 1D array consisting of 1D arrays.

Maybe the same concept can be applied to ArrayLists, which can in fact store any object types without having accessibility issues. A 5x5 ArrayList can be created as follows:

ArrayList<ArrayList> someArray = new ArrayList<ArrayList>();
for(int x=0 ; x<5 ; x++)
{
    someArray.add(new ArrayList());
    for(int y=0 ; y<5 ; y++)
        someArray.get(x).add(null);
}

and to set row 1 col 2 to a cat object:

someArray.get(1).set(2, new Cat());
someArray.get(3).set(4, new Dog());

So 2D ArrayLists, though a bit confusing might solve this issue pretty efficiently.

Upvotes: 1

CJ Burkey
CJ Burkey

Reputation: 385

How different do these objects need to be? If they're your own classes, then just create some sort of superclass that they all extend and use that as the array type. If you don't want to do that, I guess you could set something up like this:

private Object[][] array = new Object[5][5];

public void setValue(int x, int y, Object val) {
    if(x > 0 && x < array.length && y > 0 && y < array[0].length) {
        array[x][y] = val;
    }
}

public Object getValue(int x, int y) {
    if(x > 0 && x < array.length && y > 0 && y < array[0].length) {
        return array[x][y];
    }
    return null;
}

Using it would be a little tedious, but you could cast each call to getValue() to the type you needed it to be. With Object Oriented programming, having a simple solution to this can be kind of difficult. I could write some more advanced code that would return the object casted to a certain class, but it's a little bit of work. Let me know if you'd like that.

Upvotes: -1

Related Questions