반응형

이 게시물은 다음 링크를 참조하여 학습했습니다.

 

ArrayList (Java Platform SE 8 )

Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is

docs.oracle.com

 

Arrays (Java Platform SE 7 )

Sorts the specified range of the array into ascending order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty. Implementation note: The sorting alg

docs.oracle.com

 

[자료구조] Array vs ArrayList

Array와 ArrayList은 모든 것이 비슷합니다. 가장 큰 차이점은 길이를 조정할 수 있는가? 없는가? 입니다.Java의 Array는 고정 길이 입니다. 따라서, 정해진 길이의 배열을 모두 채우면, 새로운 데이터를

velog.io

 

[Java] ArrayList 정렬하기 (오름차순, 내림차순, 사용자 정의)

Collections.sort() 오름차순으로 정렬하기 내림차순으로 정렬하기 대소문자 구분없이 정렬하기 List.sort() - Java 8 이후 오름차순으로 정렬하기 내림차순으로 정렬하기 대소문자 구분없이 정렬하기 사

hianna.tistory.com

 

 

이번 게시물은 배열에 관한 내용이다.

배열에 대해서는 다 알것이라 생각하고 바로 시작한다.

 

1. Array

Array의 사용법은 C++과 거의 유사하다.

아래처럼 사용하면 된다.

1
2
int[] arr;
int arr[];
cs

 

C++에서는 Array에서 배열의 동적 할당이 안되어서 maximum size를 크게 잡아놓고 사용했었는데, Java의 Array는 동적 할당이 가능하다.

1
2
int[] arr = new int[5];
int arr[] = new int[5];
cs

이런식으로 사용하면 된다.

 

추가로 배열의 길이를 알고싶을 때는 아래처럼 사용하면 된다

1
arr.length;
cs

 

2. ArrayList

2-1. ArrayList와 Array의 차이점 

(1) Resizable

Array는 사이즈를 초기지정한 후 바꿀 수 없다.  반면에 ArrayList는 add()메서드를 통해 사이즈를 가변적으로 사용할 수 있다.

(2) Primitive type

Array는 primitive type, Object를 사용할 수 있는 반면, ArrayList는 Object만을 가질 수 있다.

(3) Length

Array는 length 변수를 사용해 길이를 확인하지만, ArrayList는 size() 메서드를 통해 길이를 확인할 수 있다.

(4) Multi-dimensional

Array는 다차원이 가능하지만, ArrayList는 항상 단일차원이다.

 

2-2. 메서드

자주 사용할 메서드들만 정리했다.

 

(1) boolean add(E element), void add(int index, E element)

element를 마지막 or index에 넣어준다.

 

(2) void clear()

ArrayList의 모든 원소를 제거한다.

 

(3) boolean contains(Object o)

o가 ArrayList에 있는지 확인한다.

 

(4) E get(int index)

ArrayList에서 index를 통한 참조

 

(5) int indexOf(Object o)

ArrayList에 o가 있는지 확인하고 있으면 index, 없으면 -1을 반환한다.

 

(6) boolean isEmpty()

ArrayList가 비어있는지 확인한다.

 

(7) E remove(int index), boolean remove(Object o)

ArrayList에서 o에 해당하는 값 또는 index에 해당하는 위치의 원소를 제거한다.

 

(8) E set(int index, E element)

ArrayList의 index위치의 값을 element로 바꿔준다.

 

(9) int size()

ArrayList의 사이즈를 반환한다.

 

(10) void sort(Comparator<? super E> c)

ArrayList의 원소들을 정렬한다.

Comparator은 아래처럼 사용하면 된다.

1
2
3
4
list.sort(Comparator.naturalOrder());                                 // 오름차순으로 정렬
list.sort(Comparator.reverseOrder());                                // 내림차순으로 정렬
list.sort(String.CASE_INSENSITIVE_ORDER);                            // 대소문자 구분없이 오름차순 정렬
list.sort(Collections.reverseOrder(String.CASE_INSENSITIVE_ORDER));    // 대소문자 구분없이 내림차순 정렬
cs

 

3. Arrays

3-1. Arrays?

Arrays는 이름은 배열같지만 배열은 아니고, 배열을 조작하기 위한 다양한 메서드가 포함되어 있다.

C++ 에서 <algorithm> 헤더에 대한 내용이 포함되있다고 생각하면 이해하기 쉽다.(똑같은건 아니고, algorithm에서 배열(vector) 관련 내용정도....?)

Arrays의 모든 메서드들은 static메서드이다.

 

3-2. 메서드

메서드의 매개변수에 해당하는 내용들은 Array 또는 Array의 값에 해당되는 내용들이고 int가 아니라 다른 값도 사용이 가능하다!

메서드들은 내가 자주 사용할 내용들만 정리했따.

 

(1) static int binarySeatch(int[] a, int key)

배열 a에서 key를 이진탐색한다.( 인덱스 반환 )

 

(2) static boolean equals(int[] a, int[] a2)

배열 a와 배열 a2가 같은지 확인한다.

 

(3) static void fill(int[] a, int val)

배열 a를 val로 채워준다.

 

(4) static void sort(int[] a)

배열a 를 오름차순으로 정렬한다.

 

(5) static String toString(int[] a)

배열을 문자열로 출력한다.

ex) int arr[] = { 1,  2, 3, 4, 5} 일때

[1, 2, 3, 4, 5] 로 출력된다.

반응형

'Legacy' 카테고리의 다른 글

[Java#5] Set, HashSet, TreeSet  (0) 2022.05.03
[Java#4] Map, HashMap, TreeMap  (0) 2022.05.03
[Java#2] String, StringBuilder  (0) 2022.04.29
[Java#1] 입출력  (0) 2022.04.29
[C++#5-5] map  (2) 2022.04.18

+ Recent posts