충분히 쌓여가는
Arrays 본문
Arrays
배열을 다루기 편리한 메서드(static) 제공
1. 배열의 출력 - toString()
static String toString(boolean[] a)
static String toString(byte[] a)
static String toString(char[] a)
static String toString(short[] a)
static String toString(int[] a)
static String toString(long[] a)
static String toString(float[] a)
static String toString(double[] a)
static String toString(Object[] a)
2. 배열의 복사 - copyOf(), copyRange()
int[] arr = {0,1,2,3,4};
int[] arr2 = Arrays.copyOf(arr, arr.length); // arr2=[0,1,2,3,4]
int[] arr3 = Arrays.copyOf(arr, 3); // arr3=[0,1,2]
int[] arr4 = Arrays.copyOf(arr, 7); // arr4=[0,1,2,3,4,0,0]
int[] arr5 = Arrays.copyOfRange(arr, 2, 4); // arr5=[2,3] <- 4는 불포함
int[] arr6 = Arrays.copyOfRange(arr, 0, 7); // arr6=[0,1,2,3,4,0.0]
3. 배열 채우기- fill(), setAll()
int[] arr = new int[5];
Arrays.fill(arr, 9); // arr=[9,9,9,9,9]
Arrays.setAll(arr, (i) -> (int)(Math.random()*5)+1); // arr=[1,5,2,1,1]
4. 배열의 정렬과 검색 - sort(), binarySearch()
int[] arr = {3, 2, 0, 1, 4};
int idx = Arrays.binarySearch(arr, 2); // idx=-5 <- 잘못된 결과, 아직 배열이 정렬되어 있지 않기 때문
Arrays.sort(arr); // 배열 arr을 정렬
System.out.println(Arrays.toString(arr)); // [0, 1, 2, 3, 4]
int idx = Arrays.binarySearch(arr, 2); // idx = 2 <- 올바른 결과
5. 다차원 배열의 출력 - deepToString()
int[] arr = {0,1,2,3,4}; // 1차원
int[][] arr2D = {{11,12}, {21,22}}; // 2차원
System.out.println(Arrays.toString(arr)); // [0, 1, 2, 3, 4]
System.out.println(Arrays.deepToString(arr2D)); // [[11, 12], [21, 22]]
6. 다차원 배열의 비교 - deepEquals()
String[][] str2D = new String[][]{{"aaa", "bbb"}, {"AAA", "BBB"}};
String[][] str2D2 = new String[][]{{"aaa", "bbb"}, {"AAA", "BBB"}};
System.out.println(Arrays.equals(str2D, str2D2)); // false
System.out.println(Arrays.deepEquals(str2D, str2D2));// true
7. 배열을 List로 변환 - asList(Object...a)
List list = Arrays.asList(new Integer[]{1,2,3,4,5}); // list = [1, 2, 3, 4, 5]
List list = Arrays.asList(1,2,3,4,5); // list = [1, 2, 3, 4, 5]
list.add(6) // UnsupportedOperationException 예외 발생
List list = new ArraysList(Arrays.asList(1,2,3,4,5)); // 새로운 ArrayList
8. 람다와 스트림 관련 - parallelXXX(), spliterator(), stream()
코드
import java.util.Arrays;
public class ArraysTest {
public static void main(String[] args) {
int[] arr = {0,1,2,3,4};
int[][] arr2D = {{11,12,13}, {21,22,23}};
System.out.println("arr="+ Arrays.toString(arr));
System.out.println("arr="+ Arrays.toString(arr2D)); // 2차원 배열은 deepToString 사용하기, 이건 이상하게 나옴
System.out.println("arr2D="+Arrays.deepToString(arr2D));
System.out.println();
int[] arr2 = Arrays.copyOf(arr, arr.length);
int[] arr3 = Arrays.copyOf(arr, 3);
int[] arr4 = Arrays.copyOf(arr, 7);
int[] arr5 = Arrays.copyOfRange(arr, 2, 4);
int[] arr6 = Arrays.copyOfRange(arr, 0, 7);
System.out.println("arr2="+Arrays.toString(arr2));
System.out.println("arr3="+Arrays.toString(arr3));
System.out.println("arr4="+Arrays.toString(arr4));
System.out.println("arr5="+Arrays.toString(arr5));
System.out.println("arr6="+Arrays.toString(arr6));
System.out.println();
int[] arr7 = new int[5];
Arrays.fill(arr7, 9); // arr=[9,9,9,9,9]
System.out.println("arr7="+Arrays.toString(arr7));
System.out.println();
Arrays.setAll(arr7, i -> (int)(Math.random()*6)+1);
System.out.println("arr7="+Arrays.toString(arr7));
System.out.println();
for(int i : arr7) { // 향상된 for문
char[] graph = new char[i];
Arrays.fill(graph, '*');
System.out.println(new String(graph)+i);
}
System.out.println();
String[][] str2D = new String[][]{{"aaa","bbb"},{"AAA","BBB"}};
String[][] str2D2 = new String[][]{{"aaa","bbb"},{"AAA","BBB"}};
System.out.println(Arrays.equals(str2D, str2D2)); // false
System.out.println(Arrays.deepEquals(str2D, str2D2)); // true
System.out.println();
char[] chArr = { 'A', 'D', 'C', 'B', 'E' };
System.out.println("chArr="+Arrays.toString(chArr));
System.out.println("index of B ="+Arrays.binarySearch(chArr, 'B')); // -2 잘못된 결과, 정렬을 안했기 때문
System.out.println();
Arrays.sort(chArr);
System.out.println("chArr="+Arrays.toString(chArr));
System.out.println("index of B ="+Arrays.binarySearch(chArr, 'B'));
}
}
arr=[0, 1, 2, 3, 4]
arr=[[I@6aaa5eb0, [I@3498ed]
arr2D=[[11, 12, 13], [21, 22, 23]]
arr2=[0, 1, 2, 3, 4]
arr3=[0, 1, 2]
arr4=[0, 1, 2, 3, 4, 0, 0]
arr5=[2, 3]
arr6=[0, 1, 2, 3, 4, 0, 0]
arr7=[9, 9, 9, 9, 9]
arr7=[1, 6, 5, 5, 1]
*1
******6
*****5
*****5
*1
false
true
chArr=[A, D, C, B, E]
index of B =-2
chArr=[A, B, C, D, E]
index of B =1
'Java > JAVA2' 카테고리의 다른 글
HashSet 2 (0) | 2023.07.17 |
---|---|
HashSet 1 (0) | 2023.07.16 |
Iterator, ListIterator, Enumeration | Map과 Iterator (0) | 2023.07.13 |
Stack & Queue (0) | 2023.07.07 |
LinkedList - 배열의 단점 보완 (0) | 2023.07.07 |