ArrayList Flashcards

1
Q

Difference between Collection and Collections

A
  1. A Collection is an interface, whereas Collections is a class.
  2. A Collection interface provides the standard functionality of a data structure to List, Set, and Queue. However, the Collections class provides the utility methods that can be used to search, sort, and synchronize collection elements.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

ArrayList

A

is the most widely used implementation of the List interface.

Features:

  1. Elements are stored in the order of insertion.
  2. It allows the storage of duplicate elements.
  3. ArrayList also supports null elements.
  4. Resizable
  5. Java 8 utilizes “Lazy Initialzation” to save memory.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Three ways to create an ArrayList

A
  1. List list = new ArrayList(); -default constructor, size 0.
  2. List list = new ArrayList(50); - capacity included, no resize needed
  3. List list - new ArrayList(oldList); - using an existing collection
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Four ways to add elements in an ArrayList

A
  1. add(E e) - inserting single element at the end
  2. add(int index, E element) - insert an element at a particular index & shift to the right
  3. adAlll(Collection c) - add all the elements at the end of the ArrayList.
  4. addAll(int index, Collection c) - add all elements at a particular index & shift to the right
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to make a ArrayList a Parameterized Type

A

List list = new ArrayList<>();

Creating a parameterized Collection is very important. Without it, there can be serious errors

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

Fetching an element in an ArrayList

A

list.get(int index);

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

Removing an element from an ArrayList

A
  1. remove(int index) -to remove an element at a particular index
  2. remove(Object o) - the first occurrence of that element will be removed.
  3. removeRange(int fromIndex, int toIndex) -will remove, from this list, all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive. ArrayList exclusive*
  4. removeAll(Collection> c) - to remove, from the given list, all of the elements that are contained in the specified collection.
  5. clear() - o remove all the elements from the ArrayList.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Replacing all the elements of the ArrayList

A

A new method, replaceAll(UnaryOperator operator), was added in Java 8. This method takes a single UnaryOperator type argument. The UnaryOperator interface is a functional interface that has a single abstract method, apply(), that returns a result of the same object type as the operand.

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

Updating an element in ArrayList

A

To update an element in ArrayList, the set(int index, E e) method can be used. This method takes the index, which needs to be updated and a new value.

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

Checking if an element is present in the ArrayList

A
  1. contains(Object o) method. This method returns true if the element is present in the list; otherwise, it returns false.
  2. indexOf(Object o) - If we need the index of the first occurrence of the element
  3. lastIndexOf(Object o) - if we need the last occurrence of the element
How well did you know this?
1
Not at all
2
3
4
5
Perfectly