Array, ArrayList, & LinkedList

Muhammad Azis Husein - Mar 1 - - Dev Community

Have you ever wondered about the difference between Array, ArrayList, and LinkedList?

Let’s answer that with a simple explanation!

Imagine you and four of your friends want to book seats for a movie at the cinema. You need to choose the seat numbers you want to book.

Image description

Array

Your friends require you to find seats that are connected in a row. So, you need to check which 5 seats in a row are available. Oh, you find it! Seats A1 to A5 are still available and can be booked for 5 people in a row. So, you book the seats and get the tickets.

Then, another one of your friends wants to join the movie. Since A6 is already taken, you need to rearrange the allocation to make sure the booked seats can accommodate 6 people in a row. So, you find B1 to B6.

It’s a bit cumbersome to allocate the seats each time someone wants to join the movie. But the good side is that it’s easy to check who is the n-th person. It’s easy because we know where the first person’s seat is, and we know they are all seated in order.

ArrayList

Your friends require you to find seats that are connected in a row, and you know there are still some other friends who might want to join. So, instead of booking just 5 seats, you reserve 20 seats in a row. So, each time another person wants to join, as long as it doesn’t exceed 20, you don’t need to reallocate the seats.

This can prevent excessive reallocation operations. But remember that the remaining seats might be unused and can’t be utilized by other people outside your group!

LinkedList

Your friends don’t care if they are not all seated in a row, so you just pick up the available seats without worrying about allocating 5 or 10 seats together. To help them remember where their friends’ seats are, each person remembers the seat of the next friend. For example, you sit in A1, then you need to remember that the 2nd person sits in B2, and the 2nd person needs to remember the 3rd person’s seat, and so on.

This makes the process easier if someone wants to join, since you don’t need to worry about reallocation. But, it will be difficult if we want to know where the last seat is. Since we need to ask each person starting from the first person.

Tldr;

Array

  • Data must be allocated in contiguous memory addresses.
  • Fixed size; requires reallocation if the next address after the last index is occupied.
  • Fast access to the n-th index.
  • Ideal for storing fixed-size data.

ArrayList

  • Essentially an Array with pre-allocated memory beyond the current data size.
  • Reduces the need for frequent memory reallocation during data appending.
  • Unused pre-allocated memory cannot be used by other data.
  • Suitable for storing variable-size data when random access to specific indices is still needed.

LinkedList

  • Data can be stored in non-contiguous memory addresses, as each element stores the address of the next element.
  • Efficient data appending.
  • Slower access to the last index, as it requires traversing the list from the beginning.
  • Optimal for storing data that is primarily accessed sequentially.
. . . . . . . . .