Java/유틸
-
[JAVA] 배열의 중복 요소 제거하기Java/유틸 2024. 9. 17. 16:53
자바에서는 배열의 중복 요소를 제거하는 여러 가지 방법이 있습니다. `HashSet`을 사용하는 방법, `Stream API`를 사용하는 방법, 그리고 중첩 루프를 사용하는 방법이 있습니다. 1. HashSet을 사용하여 중복 제거`HashSet`을 사용하는 것은 배열의 중복 요소를 제거하는 가장 간단하고 효율적인 방법 중 하나입니다. `HashSet`은 중복 요소를 허용하지 않기 때문에 쉽게 중복을 처리할 수 있습니다./** * HashSet을 사용한 중복 제거 * * @param array 중복된 요소를 제거할 정수형 배열 * @return 중복이 제거된 새로운 배열, 입력 배열이 null인 경우 null 반환 */public int[] removeDuplicatesUsingSet(int[] arra..
-
[JAVA] 배열 회전하기Java/유틸 2024. 9. 17. 16:52
자바에서는 배열을 회전시키는 여러 가지 방법이 있습니다. 전통적인 for 문, 스트림 API, 그리고 헬퍼 메서드를 사용하는 방법이 있습니다. 1. 반복문을 사용하여 배열을 회전전통적인 for 문를 사용하여 배열을 회전하는 방법은 직관적이고 간단합니다./** * 반복문을 사용하여 배열을 회전 * * @param nums 회전할 배열 * @param k 회전할 위치 수 * @return 회전된 배열 */public int[] rotateWithLoop(int[] nums, int k) { int n = nums.length; if (n == 0) { return nums; } k = k % n; int[] rotated = new int[n]; for (int i = 0; i 단위 테..
-
[JAVA] 배열의 합 구하기Java/유틸 2024. 9. 17. 16:52
자바에서는 배열의 합을 구하는 여러 가지 방법이 있습니다. 전통적인 for 문, 향상된 for 문, 스트림 API, 재귀 호출, 그리고 `Arrays.parallelPrefix` 메서드를 사용하는 방법이 있습니다.1. 전통적인 for 문를 사용한 합 구하기전통적인 for 문을 사용하여 배열의 합을 구하는 방법은 직관적이고 간단합니다./** * 배열의 합을 전통적인 for 루프를 사용 * * @param array 합을 구할 배열 * @return 배열 요소들의 합 */public int sumUsingForLoop(int[] array) { int sum = 0; for (int i = 0; i 단위 테스트@Order(1)@DisplayName("sumUsingForLoop: 배열 요소들의 합")@T..
-
[JAVA] 배열 검색하기Java/유틸 2024. 9. 17. 16:52
자바에서는 배열을 검색하는 다양한 방법이 있습니다. 이번 문서에서는 배열을 검색하는 여러 가지 방법에 대해 설명하겠습니다. 주요 메서드로는 순차 검색, 이진 검색, 라이브러리 메서드를 사용한 검색, 그리고 2차원 배열에서 값의 존재 여부를 확인하는 방법이 있습니다. 추가로 배열에서 최대 및 최소값을 찾는 방법도 설명하겠습니다. 1. 순차 검색순차 검색을 사용하여 배열에서 특정 값을 찾는 방법입니다. 배열의 첫 번째 요소부터 시작하여 찾아야 하는 값을 순차적으로 비교합니다./** * 순차 검색을 사용하여 배열에서 특정 값을 찾음 * * @param array 검색할 배열 * @param value 검색할 값 * @return 배열에서 값의 인덱스 (존재하지 않으면 -1) */public int linear..
-
[JAVA] 배열 정렬하기 : 오름차순, 내림차순Java/유틸 2024. 9. 17. 16:52
자바에서는 배열을 정렬하는 다양한 방법이 있습니다. 이번 문서에서는 Arrays.sort, Arrays.parallelSort 메서드 등을 사용하여 배열을 오름차순 및 내림차순으로 정렬하는 방법을 기본과 병렬 방식으로 구분하여 설명하겠습니다. 1. 정수 배열을 오름차순 정렬Arrays.sort 메서드를 사용하여 정수 배열을 오름차순으로 정렬합니다./** * Arrays.sort 메소드를 사용하여 배열을 오름차순으로 정렬 * * @param arr 정렬할 배열 * @return 정렬된 배열 */public int[] arraySortAscending(int[] arr) { Arrays.sort(arr); return arr;}단위 테스트@Order(1)@DisplayName("arraySortAscen..