ArrayList

I summarized it for personal study

ArrayList

Some set is called a ** collection **, and an array is one of them. Java has multiple interfaces called ** collection APIs ** and ** collection frames **, and classes made up of classes to make it easier to work with collections. Many of these interfaces and classes are located in the java.util package, and the ** java.util.ArrayList class ** is also called a ** dynamic array ** and can be used like an array.

Features of ArrayList class

① Any type of object can be used ② Automatically increase the number of elements as needed ③ Arrange in the order of addition ④ null can also be added as a value ⑤ Duplicate values can be added ⑥ Not thread safe

The features of ① and ② cover the problems of arrangement. ③ is a ** list structure ** in which the values are arranged in the order of addition because the values are managed by numbers like an array. However, it can be inserted anywhere Whether ⑥ has a function to prevent unintended results when processing in parallel

Generics

Since the argument of the add method that adds the value of ArrayList and the return value of the get method that retrieves the value are ** Object type **, anything can be handled. However, collections with mixed values can raise an exception when downcasting Therefore, there is a function called ** Generics ** that limits the types that can be handled by the collection by specifying the type.

Example) Generics ArrayList<String> list=new ArrayList<>;

The specified type is ** type parameter ** (String part in the example), and the generics can be written more concisely ** Diamond operator <> ** (<> on the right in the example) There is a function called With this function, the compiler determines what type variable declares the variable that holds the reference to the instance, and uses the same type to determine the type variable at the time of instance creation. This is called ** type inference **

Recommended Posts

ArrayList
[Practice] ArrayList
[Practice] ArrayList
ArrayList practice
ArrayList class
LinkedList and ArrayList
[Java] What is ArrayList?