0

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?

citronas
  • 19,035
  • 27
  • 96
  • 164
user3034812
  • 1
  • 1
  • 1

3 Answers3

2

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);
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
0

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

Maroun
  • 94,125
  • 30
  • 188
  • 241
Tim Destan
  • 2,028
  • 12
  • 16
0

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" });
rich
  • 18,987
  • 11
  • 75
  • 101