9

Is it possible to create ArrayList<Object type car,Object type bus> list = new ArrayList<Object type car,Object type bus>();

I mean add objects from different classes to one arraylist?

Thanks.

Serv0
  • 181
  • 1
  • 7
  • 17

5 Answers5

18

Yes, it's possible:

public interface IVehicle { /* declare all common methods here */ }
public class Car implements IVehicle { /* ... */ }
public class Bus implements IVehicle { /* ... */ }

List<IVehicle> vehicles = new ArrayList<IVehicle>();

The vehicles list will accept any object that implements IVehicle.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
9

Yes you can. But you need a common class to your object types. In your case this would be Vehicle.

So for instance:

Vehicle class:

public abstract class Vehicle {
    protected String name;
}

Bus class:

public class Bus extends Vehicle {
    public Bus(String name) {
        this.name=name;
    }
}

Car class:

public class Car extends Vehicle {
    public Car(String name) {
        this.name=name;
    }
}

Main class:

public class Main {
    public static void main(String[] args) {
        Car car = new Car("BMW");
        Bus bus = new Bus("MAN");
        ArrayList<Vehicle> list = new ArrayList<Vehicle>();
        list.add(car);
        list.add(bus);
   }
}
Nejc
  • 692
  • 6
  • 13
  • ok, what about when i read the lines from the file i don't know what object i read. I have the array of strings and i need to assign it to a correct object based on the attributes from file.? – Serv0 Nov 26 '12 at 14:42
  • This can not be directly solved with this implementation. You can have a function in `Vehicle` class called `createVehicle`, which will try to parse the input string, and create an appropriate object. For instance, the function could look-up which is the manufacturer of the vehicle - if it is `BMW`, then this is a `Car`, in other case it is a `Bus`. – Nejc Nov 26 '12 at 14:51
  • Is this correct? the second array value is the objectType so i do this. 'ArrayList items = new ArrayList(); while ((strLine = br.readLine()) != null) { String s[] = strLine.split("\\|"); if(s[2].equals("Audi")){ Audi myAud = new Audi(s[0], s[1], s[2], s[4]); items.add(myAud); } else if(s[2].equals("BMW")){ BMW myBmw = new BMW(s[0], s[1], s[2], s[3], s[4]); items.add(myBmw); } else if(s[2].equals("Opel")){ Opel myOpe = new Opel(s[0], s[1], s[2], s[3], s[4]); items.add(myOpe); } else if(s[2].equals("Mazda")){ Mazda myMaz = new Mazda(s[0], s[1], s[2], s[3]); items.add(myMaz); } }' – Serv0 Nov 26 '12 at 15:25
  • Yeah, something in those lines. But here you need to sub-class all car manufacturers, which would take a lot of time, and also it is not modular. So I would stick with the representation, where you have class `Car`. And then while constructing an object, you would call `new Car(s[0],s[1],s[2],s[3],s[4])`, where `s[2]` is obviously the car brand. – Nejc Nov 26 '12 at 15:29
  • @Serv0, for the formatting to work, you should extend your question. – Nejc Nov 26 '12 at 15:30
  • yes, that will spare me the lot of time and nerves but the problem is that the all cars have same attributes.ID and Brand. But the other attributes are different...lets say audi have : id,type,gears,color but BMW have id,type,doors,wheels,engine...not every car have same number of attributes, they have only first two but other can vary. I am stacked almost all day with this :( – Serv0 Nov 26 '12 at 15:35
  • You can still have a common constructor, where you send `null` to the fields, which are not used for particular manufacturers. Or you could even create `public static` methods for each car brand (as constructors). For example, `public static Car createAudi(String id, String type, String gears, String colors) {return new Car(id,type,gears, colors, null, null)}`, where last two fields are doors ane engine (which are not know for Audi). – Nejc Nov 26 '12 at 15:41
  • how can i sort this array depend on object attributes? – Serv0 Nov 26 '12 at 21:09
  • 1
    For that, see http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property . You need to implement custom `Comparator`. – Nejc Nov 27 '12 at 08:40
7

Get use of polymorphism. Let's say you have a parent class Vehicle for Bus and Car.

ArrayList<Vehicle> list = new ArrayList<Vehicle>();

You can add objects of types Bus, Car or Vehicle to this list since Bus IS-A Vehicle, Car IS-A Vehicle and Vehicle IS-A Vehicle.

Retrieving an object from the list and operating based on its type:

Object obj = list.get(3);

if(obj instanceof Bus)
{
   Bus bus = (Bus) obj;
   bus.busMethod();
}
else if(obj instanceof Car)
{
   Car car = (Car) obj;
   car.carMethod();
}
else
{
   Vehicle vehicle = (Vehicle) obj;
   vehicle.vehicleMethod();
}
Juvanis
  • 25,802
  • 5
  • 69
  • 87
  • that is exactly what am i doing now but how can i acces the methods from extended classes? – Serv0 Nov 26 '12 at 14:38
  • Is this correct? the second array value is the objectType so i do this. 'ArrayList items = new ArrayList(); while ((strLine = br.readLine()) != null) { String s[] = strLine.split("\\|"); if(s[2].equals("Audi")){ Audi myAud = new Audi(s[0], s[1], s[2], s[4]); items.add(myAud); } else if(s[2].equals("BMW")){ BMW myBmw = new BMW(s[0], s[1], s[2], s[3], s[4]); items.add(myBmw); } else if(s[2].equals("Opel")){ Opel myOpe = new Opel(s[0], s[1], s[2], s[3], s[4]); items.add(myOpe); } else if(s[2].equals("Mazda")){ Mazda myMaz = new Mazda(s[0], s[1], s[2], s[3]); items.add(myMaz); } }' – Serv0 Nov 26 '12 at 15:26
2

You can't specify more than one type parameter unfortunately, so you'll have to find a common superclass for your types and use that. An extreme case would be just using Object:

List<Object> list = new ArrayList<Object>();

Be careful that you will need to cast the result to the specific type that you need if you retrieve an item (to get full functionality, not just the common one):

Car c = (Car)list.get(0); 
Tudor
  • 61,523
  • 12
  • 102
  • 142
  • Thanks, but in my case i cannot use this solution because i read the attributes from the array and i don't know what object i get from file. – Serv0 Nov 26 '12 at 14:56
0

Create a class and use polymorphism. And then to pick up the object in the click, use instanceof.