0

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.

5
  • 6
    Write an interface and specify in that interface the methods you want to use. Have your shape classes all implement the interface. Also, pyramid is spelled with a "y" Commented Feb 9, 2018 at 9:52
  • 2
    The word you are looking for is 'Polymorphism' javaworld.com/article/3033445/learn-java/… Commented Feb 9, 2018 at 9:53
  • use interface type to insert into the arraylist and let all classes Circle, cube etc implement it...refer: stackoverflow.com/questions/13566983/… Commented Feb 9, 2018 at 9:55
  • or simply use ArrayList<Object> list = new ArrayList<Object>();. This does allow every type tp be stored Commented Feb 9, 2018 at 9:56
  • Possible duplicate of Creating an array list of multiple data types in Java Commented Feb 9, 2018 at 10:07

5 Answers 5

4

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 Shapes:

List<Shape> shapes = Arrays.asList(new Circle(3), new Square(4), ...);
Sign up to request clarification or add additional context in comments.

Comments

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

you can call the all methods including Object class method.

1 Comment

you will be able to access only the methods of Object class and not the methods of the instances added in the arrayList
0

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

Comments

0

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

Comments

0
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.