π 2023λ 12μ 11μΌ
[java 5μΌμ°¨]
String[] st_arr1 = {"hello", "hi", "bye", "good_morning"}; Arrays.sort(st_arr1); int[] Arr = {5,1,2,7,3,1,2}; // Arrays.sort(Arr, Comparator.reverseOrder()); κΈ°λ³Ένμ μλλ€. // Stream Api, lambdaλ₯Ό νμ©ν λ΄λ¦Όμ°¨μ μ λ ¬; int[] Arr2 = {5,1,2,7,3,1,2}; int[] new_Arr2 = Arrays.stream(Arr2) // Arr2λ₯Ό λμμΌλ‘ Stream κ°μ²΄ μμ± .boxed() // Integer ν λ³νν Stream κ°μ²΄ μμ± .sorted(Comparator.reverseOrder()) // λ΄λ¦Όμ°¨μ μ λ ¬ .mapToInt(a->a) // Integer -> int νλ³ν .toArray();
int[] arr1 = {30,22,20,25,12}; int[] arr2 = {30,22,20,25,12}; int[] arr3 = {30,22,20,25,12}; for (int i = 1; i < arr1.length; i++) { for (int j = 1; j < arr1.length; j++) { if(arr1[j]<arr1[j-1]){ int tmp = arr1[j]; arr1[j] = arr1[j-1]; arr1[j-1] = tmp; } } System.out.println(Arrays.toString(arr1)); } System.out.println(); for (int i = 1; i < arr1.length; i++) { for (int j = 1; j < arr1.length; j++) { if(arr1[j]>arr1[j-1]){ int tmp = arr1[j]; arr1[j] = arr1[j-1]; arr1[j-1] = tmp; } } System.out.println(Arrays.toString(arr1)); } System.out.println(); for (int i = 0; i < arr3.length-1; i++) { for (int j = i+1; j < arr3.length; j++) { if(arr3[i]>arr3[j]){ int tmp = arr3[i]; arr3[i] = arr3[j]; arr3[j] = tmp; } } System.out.println(Arrays.toString(arr3)); } System.out.println();
// λ²λΈ μ λ ¬ int[] arr = {10,30,20,30,40,50,80,70,60}; boolean flag = true; for (int i = 1; i < arr.length; i++) { flag = false; for (int j = 0; j < arr.length-1; j++) { if(arr[j] > arr [j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; flag = true; } System.out.println(Arrays.toString(arr)); } if(flag != true){ break; } System.out.println(i+"μ°¨"); System.out.println(Arrays.toString(arr)); }
int[] arr1 = {5,3,1,8,7}; int index = 0; for (int i = 0; i < arr1.length; i++) { if( arr1[i] == 8){ index = i; } } System.out.println(index); // μ΄μ§ κ²μ Binary Search // μ¬μ μ μ€λ¦μ°¨μ μ λ ¬μ΄ λμ΄ μμ΄μΌ μ΄μ§κ²μ κ°λ₯ int[] arr2 = {1,3,6,8,9,11}; System.out.println(Arrays.binarySearch(arr2,8));
int[][] arr = new int [2][3]; int temp = 0; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { arr[i][j] = temp; temp++; } } int[][] arr2 = new int[3][]; // κ°λ³λ°°μ΄ arr2[0] = new int [1]; arr2[1] = new int [2]; arr2[2] = new int [3]; System.out.println(Arrays.deepToString(arr2)); int[][] arr3 = {{1,2,3},{1,2,3}};