ArrayList (List) Methods Flashcards

1
Q

Declaring a ArrayList

A

ArrayList arraylist = new ArrayList();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Adding an Item at the List

A

arraylist.add(“Element”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Adding an element at a particular position of ArrayList

A

arraylist.add(3,Element);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Adding all element of a list to the end of the ArrayList

A

arraylist.addall(list-name);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Adding a list to a particular position of ArrayList

A

arraylist.addall(2,list-name);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Adding a ArrayList to Another ArrayList

A

arraylist-first.append(arraylist-second);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Removing an Item from the List (Any Element)

A

arraylist.remove(“Element”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Removing an Item from a particular position of an ArrayList

A

arraylist.remove(2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Retrieving an Item from an ArrayList based on the Index

A

arraylist.get(5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Retrieving a sublist from an ArrayList <i>Based on Index</i>

A

arraylist.sublist(2,5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Retrieving Index of an Item

A

arraylist.indexOf(“Element”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Retrieving Last Index of an Item

A

arraylist.lastIndexOf(“Element”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Size of an ArrayList

A

arraylist.size();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Sorting an ArrayList

A

Collection.sort(arraylist);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Reverse Sorting an Array List

A

Method 1 : Collection.reverse(arraylist);

Method 2 : Collection.sort (arraylist,Collection.reverseOrder());

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Iterating over an ArrayList (Method 1: For Loop)

A

Method 1 - FOR LOOP
for (int counter = 0; counter < arraylist.size(); counter++) {
System.out.println(arraylist.get(counter));
}

17
Q

Iterating over an ArrayList (Method 2: While Loop)

A
Method 2 - WHILE LOOP 
int count = 0; 		
while (arraylist.size() > count) {
    System.out.println(arraylist.get(count));
    count++;
     }
18
Q

Iterating over an ArrayList (Method 3: ITERATOR)

A

Method 3 - ITERATOR
Iterator iterator = arraylist.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}

19
Q

Is this item there in the ArrayList?

A

arraylist.contains(“Element”);

20
Q

Swapping elements in an arraylist

A

Collection.swap(arraylist,1,4);

21
Q

Difference between Array and ArrayList?

A

Contains:
Arrays can contain primitive or Objects
ArrayList can contain only Objects.

Size:
Arrays are fixed size
ArrayList size is dynamic.

Feature
Arrays doesn’t provide a lot of features like ArrayList, such as addAll, removeAll, iterator Set implementations: HashSet, LinkedHashSet, TreeSet etc.

22
Q

When will you use Array over ArrayList?

A

Although ArrayList is the obvious choice when we work on list, there are few times when array are good to use.

If the size of list is fixed and mostly used to store and traverse them.
For list of primitive data types, although Collections use autoboxing to reduce the coding effort but still it makes them slow when working on fixed size primitive data types.
If you are working on fixed multi-dimensional situation, using [][] is far more easier than List>