简体   繁体   中英

How to store different types of objects in ArrayList

I need to store different objects in the ArrayList. Objects are similar in nature but different and have different methods.

Circle c = new Circle();  
Cube s = new Cube();
Piramid p = new Piramid();
ArrayList<?> list = new ArrayList<?>();

So what to use in that I can use all the methods in the objects.

You can just create an ArrayList<Object> . That's a list that can store anything. It's not very useful to work with, because taking things out basically forces you to check their type in order to call any methods. For that reason you should almost always avoid doing this.

Objects in collections usually have something in common - if they don't, you should rethink why you're throwing them into the same collection. In your case, they're all shapes. So instead, consider adding a shared interface such as Shape which combines the common functionality.

I'm simplifying slightly because you have the concept of both 2D and 3D shapes, but here's the gist:

interface Shape
{
    double area();
    double perimeter();
}

class Circle implements Shape
{
    // ...

    public double area()
    {
        return radius * radius * Math.PI;
    }

    public double perimeter()
    {
        return 2 * Math.PI * radius;
    }
}

class Square implements Shape
{
     //...
}

And then creating a list of Shape s:

List<Shape> shapes = Arrays.asList(new Circle(3), new Square(4), ...);
ArrayList<Object> list = new ArrayList<Object>();

您可以调用包括 Object 类方法在内的所有方法。

i think you can create a list of objects, and when use someone u cast to the specific type

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

if (obj.get(i) instanceof Class) {
    Class objCast = (Class)obj 
}

Objects DOC

This is what you have to do.

  1. Define a Shape interface..

  2. Implement the interface for Circle and Cube

  3. Create shape objects for Circle and Cube and add these to arraylist..

Code below:

public interface Shape {

    public void draw();

}



public class Circle implements Shape {

@Override
public void draw() {
    System.out.println("Drawing Circle");

}

}



public class Cube implements Shape {

@Override
public void draw() {
    System.out.println("Drawing Cube");
}

}



public class Simulator{

public static void main(String[] s){

    Shape s1 = new Circle();
    Shape s2 = new Cube();

    ArrayList<Shape> shapeList = new ArrayList<Shape>();
    shapeList.add(s1);
    shapeList.add(s2);
}
}
List<Object> list = new ArrayList<>();

list.add(new ArrayList<>());
list.add(Integer.valueOf("24"));
list.add(Long.valueOf("25"));
list.add("STRING");
list.add(new Object());

System.out.println(Arrays.toString(list.toArray()));
//Console print: " [[], 24, 25, STRING, java.lang.Object@4554617c] "

You may store any object type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM