향상된 for 문
for(int item: array) {
System.out.print(item);
}
두개의 배열 합치기
int[] array1 = new int[] {1, 2, 3, 4};
int[] array2 = new int[] {5, 6, 7, 8};
// 새로운 배열 생성
int[] combinedArray = new int[array1.length + array2.length];
// array1 복사
System.arraycopy(array1, 0, combinedArray, 0, array1.length);
// array2 복사
System.arraycopy(array2, 0, combinedArray, array1.length, array2.length);
두개의 리스트 합치기
List<Integer> combinedList = new ArrayList<>();
combinedList.addAll(list1);
combinedList.addAll(list2);
두개의 맵 합치기
Map<String, Integer> combinedMap = new HashMap<>();
combinedMap.putAll(map1);
combinedMap.putAll(map2);
리스트 정렬
Collections.sort(list);
효율적인 조건문 작성
기존 코드
void initialize() {
if (!isInitialized()) {
// 적절한 초기화 로직
}
}
개선된 코드
void initialize() {
if (isInitialized()) {
return;
}
// 적절한 초기화 로직
}
기존 중첩 반복문 코드
void compute() {
Server server = getServer();
if (server != null) {
Client client = server.getClient();
if (client != null) {
Request request = client.getRequest();
if (request != null) {
// 실제 처리할 로직
}
}
}
}
개선된 중첩 반복문 코드
void compute() {
Server server = getServer();
if (server == null) return;
Client client = server.getClient();
if (client == null) return;
Request request = client.getRequest();
if (request == null) return;
// 실제 처리할 로직
}