Regarding already defined slots in arraylists

333 views Asked by At

I'm wondering if it's possible to prefill the slots for an arraylist? For example, it is possible to fill out the st array by the assignment operation like this:

    student [] st = new student[3];
    st[0] = new student("214365879","eric banner", 67);
st[1] = new student("988634321","tony park",45);
st[2] = new student("009451223","paul summers",59);

But how is it possible by using arraylist instead?

3

There are 3 answers

0
Kick Buttowski On

It is possible to do by using add method from ArrayList

Person p1 = new Person("Izak","1234",33);
Person p2 = new Person("Igal","5668", 25);

List<Person> pList = new ArrayList<>();
pList.add(p1);
pList.add(p2);
0
rich On

These are about as close as I can think to that sort of syntax.

List items = new ArrayList() {{
  add("A");
  add("B");
}};

Or

List items = Arrays.asList(new String[] { "A", "B" });
0
Tim Destan On

There's a constructor on ArrayList that takes an integer that tells the ArrayList the initial capacity.