ArrayList 이해

ArrayList의 개념 이해:



Authors : Divyansh Pratap Singh ( )



배열 목록 대 배열:




배열 목록
정렬


그들은 가변 크기를 가지고
그들은 크기가 고정되어 있습니다

비연속 메모리 할당이 있습니다.
그들은 지속적인 메모리 할당을 가지고 있습니다

객체만 저장할 수 있습니다.
예를 들어 원시 데이터 유형을 저장할 수 있습니다. 정수, 부동


ArrayList 구현



    `import java.util.ArrayList;
    public class arrayy_list{
        public static void main(String[]args){

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

        }
    }
   `

Similarly for declaring String


ArrayList<String> name = new ArrayList<String>();

ArrayList에 요소 추가



we use add function to add an element in ArrayList


ArrayList<Integer> list = new ArrayList<Integer>();
list.add(3);

Adding elements 3,2,5,0 --> below is the complete code for adding element
'''
import java.util.ArrayList;
public class arrayy_list{
public static void main(String[]args){



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

            list.add(3);
            list.add(2);
            list.add(5);
            list.add(0);
        }
    }

`

ArrayList의 요소에 액세스:



to access element in ArrayList we use get function . Inside get function we pass the index position of the element .


list.get(0);

Gives us : 3



ArrayList 중간에 요소 추가:



add function adds an element at the end of the ArrayList ; to add element at any position in ArrayList we use add function and pass 2 parameters ;


list.add(<indexposition><element>);list.add(0,9);

Result : [9,3,2,5,0]



ArrayList의 요소 변경:



to change a value in ArrayList we use set function . The function takes 2 parameters ; 1st is the index position of the element you want to change and the 2nd parameter is the new element.


list.set(0,8);

Result : [8,3,2,5,0]



ArrayList에서 요소 제거:



to remove a element from ArrayList we use remove function and pass the element index position as the parameter.


list.remove(0);

Result : [3,2,5,0]



인쇄 완료 ArrayList


System.out.println(<ArrayList name >);
System.out.println(list);

Result : [3,2,5,0]

좋은 웹페이지 즐겨찾기