티스토리 뷰

JAVA

Collection framework - ArrayList

seoca 2019. 1. 17. 15:33




Collection Framework  


collection framework is a collection providing an architecture to store the group of objects.

Java primitive types are not considered objects. Since Java collections only store objects, primitive types are not allowed to store. Thus, we need wrapper class or autoboxing to store data. 


Interfaces - Set,List,Queue,Deque

Classes - ArrayList, Vector, LinkedList,HashSet,LinkedHashSet,TreeSet,PriorityQueue


collection은 자료구조와 알고리즘을 클래스로 구현해 놓은 것. collection framework 를 구성하는 많은 class들은 많은 양의 instance들을 다양한 형태로 저장하는 기능을 제공하고 있다. 

collection framework 는 오직 참조값만을 인스턴스 저장의 대상으로 삼기때문에 wrapper class를 이용하거나 autoboxing을 이용해야 한다.





ArrayList


ArrayList and LinkedList represent List interface. ArrayList provides dynamic arrays in Java. It might slower than standard arrays. ArrayList inherits AbstractList class and implements List interface. ArrayList is initialized by size, but the size can be modified. It is the biggest difference between a standard array and ArrayList. As the array is not allowed to change the size after initializing it, ArrayList let us can modify it if we want to grow or shrink the size or when you don't even know the exact size you need. ArrayList and Linked List maintain the elements insertion order.


ArrayList는 기존의 array와 다르게 초기화를 한 이후에도 사이즈를 조절할 수 있다. Array보다 속도 면에서 다소 느릴 수 있지만 사이즈를 조절 할 일이 자주 있다면 훨씬 효과적인 방법이다. ArrayList와 LinkedList는 저장 순서를 유지한다.





Example code 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.ArrayList;
//To set up an ArrayList, you first need to import the package from the java.util library
public class ArrayListExample{
    public static void main(String[] args){
        ArrayList<Integer> list = new ArrayList<>();
        //create a new ArrayList object
        
        list.add(new Integer(11));
        //Once a new ArrayList objects are created, you can add elements to it with the add method
        list.add(new Integer(2));
        list.add(new Integer(15));
        
        for(int i = 0;i < list.size();i++){
//size() method is to get the size of the ArrayList
//length() method is for standard array
            System.out.println(list.get(i));
            //Items in the list can be referenced by an Index number, and by using the get method
        }
        
        list.remove(0);
        //You can also remove items from an ArrayList
        System.out.println("after removing 1st index");
        for(int i = 0;i < list.size();i++){
            System.out.println(list.get(i));
        }
    } 
}

cs


* set() method - you can update the list element with set() method. e.g  list.set(int index, element)



output

1
2
3
4
5
6
7
11
2
15
after removing 1st index
2
15
 
cs




Reference


https://www.youtube.com/watch?v=WwfhLC16bis

나는 정말 자바를 공부한 적이 없다구요 윤성우 저

https://www.javatpoint.com/collections-in-java

https://www.geeksforgeeks.org/arraylist-in-java/

https://stackoverflow.com/questions/2504959/why-can-java-collections-not-directly-store-primitives-types

'JAVA' 카테고리의 다른 글

Upcasting (Object type casting) in java  (0) 2019.01.30
String class in Java  (0) 2019.01.20
abstract & Interface in java  (0) 2019.01.17
toString method  (0) 2019.01.08
wrapper class in java  (0) 2019.01.07