ArrayList - LinkedList Flashcards

1
Q

Array vs ArrayList

A

•Array is fixed sized like below
int[] arr = new int[10];

  • Array members are accessed using [index]
  • Array can store both primitive data types as well as objects
•ArrayList is dynamic sized array like below
ArrayList arrList = new ArrayList();
  • ArrayList has a set of methods to access elements and modify them
  • ArrayList only supports object entries, not the primitive data types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

ArrayList Syntax:

A

ArrayList myArrayList= new ArrayList<>();

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

Basic Operations on ArrayList – Methods of ArrayList

A
  • Remove an element: remove()
  • Remove all elements: clear()
  • Change element: set()
  • Find index of specific element: indexOf() or lastIndexOf()
  • Check if ArrayList contains a specific element: contains()
  • Add element: add() or addAll()
  • Add element to specific index: overloaded add()
  • Access element: get()
  • Find size of ArrayList: size()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

ArrayList in Java

A

•Unlike Arrays, ArrayLists can automatically adjust its capacity when we add or remove elements from it and hence, ArrayLists are also known as dynamic arrays

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

What is Array and ArrayList and their differences in Java?

A

Array is one of the collection types in java
• Collection is storing multiple similar items in one place
• By using an Array, we can store many similar items/elements in one variable
• Arrays in Java have fixed size and once the size of an array is declared, it’s not possible to change it
• To handle this issue, we can use the ArrayList class. It allows us to create dynamic sized arrays
• Unlike Arrays, ArrayLists can automatically adjust its capacity when we add or remove elements from it and
hence, ArrayLists are also known as dynamic arrays
• Array members are accessed using [index] while ArrayList has a set of methods to access elements and
modify them like get(), add(), set() and so on…
• Array can store both primitive data types as well as objects while ArrayList only supports object entries, not
the primitive data types

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

What are differences between ArrayList and LinkedList?

A

• ArrayList stores the value in a single position while LinkedList stores 3 values (previous address, data, and
next address) in a single position
• Whenever an element is added or removed to an ArrayList in the middle, all elements after that position are
shifted but Whenever an element is added or removed to a LinkedList, only the address of previous and
next elements are changed
• LinkedList allocates more memory compared to ArrayList but more proper to use if we will add or remove
data frequently but ArrayList allocates less memory and preferred when there is not too much data change
• ArrayList is slower compared to LinkedList when we add or remove new data

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