Arrays
- 배열의 출력 -
toString()
1 | static String toString(boolean[] a) |
2 | static String toString(byte[] a) |
3 | static String toString(char[] a) |
4 | static String toString(short[] a) |
5 | static String toString(int[] a) |
6 | static String toString(long[] a) |
7 | static String toString(float[] a) |
8 | static String toString(double[] a) |
9 | static String toString(Object[] a) |
- 배열의 복사 -
copyOf()
,copyOfRange()
1 | int[] arr = {0,1,2,3,4}; |
2 | int[] arr2 = Arrays.copyOf(arr, arr.length); |
3 | int[] arr3 = Arrays.copyOf(arr, 3); |
4 | int[] arr4 = Arrays.copyOf(arr, 7); |
5 | int[] arr5 = Arrays.copyOfRange(arr, 2, 4); |
6 | int[] arr6 = Arrays.copyOfRange(arr, 0 ,7); |
- 배열 채우기 -
fill(), setAll()
1 | int[] arr = new int[5]; |
2 | Array.fill(arr, 9); // arr=[9,9,9,9,9] |
3 | Arrarys.setAll(arr, (i) -> (int)(Math.random()*5)+1); //arr=[1,5,2,1,1] |
- 배열의 정렬과 검색 -
sort(), binarySearch()
1 | int[] arr = {3, 2, 0, 1, 4}; |
2 | int idx = Arrays.binarySearch(arr, 2); |
3 | |
4 | Arrays.sort(aa); //배열arr을정렬 |
5 | System.out.println(Arrays.toString(arr)); |
6 | int idx = Arrays.binarySearch(arr, 2); //idx=2 |
- 다차원 배열의 출력 -
deepToString()
1 | int[] arr = {0,1,2,3,4}; |
2 | int[][ arr2D = {{11,12}, {21,22}}; |
3 | |
4 | System.out.println(Arrays.toString(arr)); //[0, 1, 2, 3, 4] |
5 | System.out.println(Arrarys.deepToString(arr2D)); //[[11, 12], [21, 22]] |
- 다차원 배열의 비교 -
deepEquals()
1 | String[][] str2D = new String[][]{{"aaa", "bbb"}, {"AAA", "BBB"}} |
2 | String[][] str2D2 = new String[][]{{"aaa", "bbb"},{"AAA", "BBB"}}; |
3 | |
4 | System.out.println(Arrays.equals(str2D, str2D2)); //false |
5 | System.out.println(Arrays.deepEquals(str2D, str2D2)); //true |
- 배열을 List로 변환 -
asList(Object....a)
1 | List list = Arrarys.asList(new Integer[]{1,2,3,4,5}); |
2 | List list = Arrays.asList(1,2,3,4,5); |
3 | list.add(6); |
4 | List list = new ArrayList(Arrays.asList(1,2,3,4,5)); |