Creating an Arraylist of Objects


Question

How do I fill an ArrayList with objects, with each object inside being different?

1
48
5/23/2017 3:06:20 PM

Accepted Answer

ArrayList<Matrices> list = new ArrayList<Matrices>();
list.add( new Matrices(1,1,10) );
list.add( new Matrices(1,2,20) );
65
10/20/2010 9:44:52 PM

How to Creating an Arraylist of Objects.

Create an array to store the objects:

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

In a single step:

list.add(new MyObject (1, 2, 3)); //Create a new object and adding it to list. 

or

MyObject myObject = new MyObject (1, 2, 3); //Create a new object.
list.add(myObject); // Adding it to the list.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon