Contents

The difference between an Array and a List (with.Java)

   Aug 14, 2023     2 min read

In this article, we learned about The difference between an Array and a List (with.Java)

I’m preparing for a coding test and learning about grammar and organization. In this article, we’ll learn about arrays and lists.

The purpose of Arrays and Lists

Array and List are data structures for managing multiple pieces of data under one name.

They serve the same purpose, but what’s the difference?

Array:

  • It has a fixed space and an index (identifier). ex) arr[5]
  • Size allocation is mandatory when creating the object. ex) char[] a = new char[5]
  • In terms of speed, inserts and deletes are slow and data lookups are fast.

List:

  • No Index (identifier), if the 0th element is missing from a list with a size of 3, the newly added element can be stored at 0th.
  • No need for size allocation - in Java, it’s automatically increased by a factor of 1.5.
  • In terms of speed, inserts and deletes are fast and data lookups are slow.

Now that we know the differences, we can conclude.

Conclusion

When faced with a problem, if the data has a fixed size, use an Array; if there is no mention of size, use an Array.

Having concluded, we often write ArrayList among Java methods. What is it all about…?

What is an ArrayList?

It’s a method that combines the best of Array and List, allowing you to write identifiers with index, an Array attribute, and dynamically allocate the size with a List attribute.

In other words, it is a List that can write index!

How to use ArrayList
import java.util.ArrayList

ArrayList<Integer> integers1 = new ArrayList<Integer>(); // Specify the Integer type
introduces various uses
  • add(index, element): Adds element to the ArrayList at index. Index can be omitted, but if so, the value is added to the end of the ArrayList.
  • set(index, element): Resets element to match index in the ArrayList. Any element at index that exists in the existing ArrayList will be lost.
  • remove(indexelement): Clears the element at index in the ArrayList or directly clears an element in the ArrayList. remove can assign the value of the cleared element to a variable.
  • clear(): Deletes all elements in an ArrayList.
  • size(): Get the size of an ArrayList as an int type.
  • get(index): Get the element that matches the index of the ArrayList.
  • contains(element): Returns in the form of a boolean if the element exists in the ArrayList.
  • indexOf(element): Returns the index in the form of an int if the element exists in the ArrayList.