TIL: Java ArrayList

Adam Sung Min Park·2022년 9월 26일
0
post-thumbnail

I think the one of the most important process when it comes to using the Java is how to convert data type.
During this process, I noticed that I got fond of using ArrayList a lot.
I mean A LOT.
Whenever I am not sure what to do or don't know how to handle the data, I just start with the defining a new ArrayList.

So what is an ArrayList?

ArrayList is a resizable- array implementation of the List interface. It implements all optional list operations, and permits all elements including null. It also allows methods to manipulate the size of the array. So unlike Array which does not allow size change, it is more convenient to deal with.
With that being said, the size of the array used to store the elements in the list is at least as large as the list size.

Example

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    System.out.println(cars);
  }
}

Some of the few convenient methods used to manipulate the ArrayList:

get(), set(), clear(), remove()

I guess one of the main reason why I like to use ArrayList is that it allows programmers to set which data type the array is consist of.

For instance, if I want to make an Integer ArrayList

ArrayList<Integer> intArrList = new ArrayList<Integer>();

the data type can be changed within the angled bracket (ex: String, Character etc).

For the reference, I will leave an URL to the Oracle documents if you want to take a closer look into the ArrayList(probably only for my future self).

링크텍스트

0개의 댓글