목록Java (308)
SeouliteLab
예제 1: 오토박싱(Autoboxing) int primitiveInt = 10; Integer wrappedInt = primitiveInt; // Autoboxing System.out.println("Wrapped Integer: " + wrappedInt); 오토박싱은 기본 자료형을 해당하는 래퍼 클래스로 자동으로 변환하는 것을 의미합니다. 위 예제에서는 `int`형 변수를 `Integer` 래퍼 클래스로 자동으로 변환하여 할당하고 있습니다. 예제 2: 언박싱(Unboxing) Integer wrappedInt = 20; int primitiveInt = wrappedInt; // Unboxing System.out.println("Primitive int: " + primitiveInt); 언박..
예제 1: TreeMap을 사용하여 키(Key)로 정렬 HashMap map = new HashMap(); map.put("apple", 50); map.put("banana", 30); map.put("orange", 40); map.put("grape", 20); TreeMap sortedMap = new TreeMap(map); System.out.println(sortedMap); `TreeMap`을 사용하여 HashMap을 키(Key)로 정렬하는 예제입니다. TreeMap은 키의 자연 순서에 따라 정렬됩니다. 예제 2: Comparator를 사용하여 값(Value)로 정렬 HashMap map = new HashMap(); map.put("apple", 50); map.put("banana", ..
예제 1: 반복문을 사용하여 최대값 및 최소값 찾기 HashSet numbers = new HashSet(); numbers.add(10); numbers.add(30); numbers.add(20); numbers.add(50); numbers.add(40); int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for (int num : numbers) { if (num > max) { max = num; } if (num < min) { min = num; } } System.out.println("최대값: " + max); System.out.println("최소값: " + min); 반복문을 사용하여 HashSet의 요소를 순회하면서 최대값과 최..
예제 1: LocalDate에서 일 수 더하기/빼기 // 현재 날짜 LocalDate today = LocalDate.now(); // 3일 후 날짜 LocalDate threeDaysLater = today.plusDays(3); System.out.println("3일 후 날짜: " + threeDaysLater); // 1달 전 날짜 LocalDate oneMonthAgo = today.minusMonths(1); System.out.println("1달 전 날짜: " + oneMonthAgo); `LocalDate`를 사용하여 날짜에 대한 연산을 수행하는 예제입니다. `plusDays()` 및 `minusMonths()` 메서드를 사용하여 날짜를 더하거나 빼는 방법을 보여줍니다. 예제 2: Loc..
예제 1: 특수문자 제거 String str = "Hello, World! This is a test string."; String result = str.replaceAll("[^a-zA-Z0-9]", ""); System.out.println(result); // 출력 결과: HelloWorldThisisateststring 정규 표현식을 사용하여 문자열에서 특수문자를 제거하는 예제입니다. `replaceAll` 메서드를 사용하여 대상 문자열에서 알파벳과 숫자가 아닌 모든 문자를 제거합니다. 예제 2: 숫자 제거 String str = "The price is $9.99"; String result = str.replaceAll("\\d", ""); System.out.println(result); ..
예제 1: Maven 프로젝트 생성 mvn archetype:generate -DgroupId=com.example -DartifactId=mybatis-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 먼저 Maven을 사용하여 기본적인 프로젝트를 생성합니다. groupId와 artifactId는 프로젝트에 맞게 설정합니다. 예제 2: pom.xml 파일에 MyBatis 및 MySQL Connector 의존성 추가 org.mybatis mybatis 3.5.6 mysql mysql-connector-java 8.0.27 pom.xml 파일에 MyBatis 및 MySQL Connector의 의존성을 추가합니다. 현재..
예제 1: String 클래스의 replaceAll() 메서드 사용 public class Main { public static void main(String[] args) { // 따옴표가 포함된 문자열 String str = "\"Hello, World!\""; // replaceAll() 메서드를 사용하여 따옴표 제거 String result = str.replaceAll("\"", ""); // 결과 출력 System.out.println("따옴표 제거 후 문자열: " + result); } } String 클래스의 replaceAll() 메서드를 사용하여 따옴표를 제거할 수 있습니다. 정규표현식으로 따옴표를 검색하고 빈 문자열로 대체하여 제거합니다. 예제 2: Apache Commons Lang..
예제 1: HashSet을 활용한 중복 제거 import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] args) { // 중복이 포함된 배열 Integer[] arr = {1, 2, 3, 3, 4, 5, 5, 6}; // 배열을 HashSet으로 변환하여 중복 제거 Set set = new HashSet(Arrays.asList(arr)); // 결과 출력 System.out.println("중복 제거 후 배열: " + set); } } HashSet을 사용하여 중복 요소를 제거할 수 있습니다. HashSet은 중복을 허용하지 않는 특징을..
예제 1: charAt() 메서드 사용 public class Main { public static void main(String[] args) { String str = "Hello, World!"; // Index로 문자 가져오기 char ch = str.charAt(7); // 결과 출력 System.out.println("Index 7에 위치한 문자: " + ch); } } charAt() 메서드를 사용하여 문자열에서 특정 Index에 위치한 문자를 가져올 수 있습니다. 예제 2: substring() 메서드 사용 public class Main { public static void main(String[] args) { String str = "Hello, World!"; // Index로 문자..
예제 1: Arrays.equals() 메서드 사용 import java.util.Arrays; public class Main { public static void main(String[] args) { // 비교할 두 배열 생성 int[] arr1 = {1, 2, 3}; int[] arr2 = {1, 2, 3}; // 배열 비교 boolean isEqual = Arrays.equals(arr1, arr2); // 결과 출력 System.out.println("두 배열이 동일한가요? " + isEqual); } } Arrays.equals() 메서드를 사용하여 두 배열을 비교할 수 있습니다. 이 메서드는 두 배열의 요소가 동일한지를 확인합니다. 예제 2: 반복문을 사용한 요소별 비교 public cla..