Learn/Java
java 배열 <-> 리스트 변환하기
배열 → List1. Arrays.asList()String[] arr = { "A", "B", "C" }; // 배열 -> List로 변환 List list = Arrays.asList(arr);2. new ArrayList(Arrays.asList())1번을 사용해서 배열을 list로 변환하면 변경된값들이 공유되는것을 방지하기 위한 방법String[] arr = { "A", "B", "C" };List list = new ArrayList(Arrays.asList(arr));3. Collectors.toList()java8이후부터 stream 사용하여 변환 가능import java.util.List;import java.util.stream.Collectors;import java...
코테에 쓰는 java로 정렬하는 법
배열오름차순: Arrays.sort()숫자도 가능하며 문자열의 경우 아스키코드(알파벳)순, 한글도 가나다 순으로 정렬된다.기본타입, 참조타입 전부 가능import java.util.Arrays;public class Main { public static void main(String[] args) { int[] arr = {5, 2, 8, 1, 3}; Arrays.sort(arr); System.out.println(Arrays.toString(arr)); // [1, 2, 3, 5, 8] }}임의의 정렬기준: Sort(Array, Comparator)주의: Primitive 타입은 불가Integer, Character, String 등만 가능import ..
Optional 왜 쓸까
개요Optional 은 Java8에서 Null값을 안전히 처리함으로써 NullPointerException 예외를 방지하기 위해 도입되었다.(물론 NullPointerException가 나올 때도 있다. 예시는 후략)그럼 언제 Optional을 사용해야 하는가? Optional is primarily intended for use as a method return type where there is a clear need to represent "no result," and where using null is likely to cause errors. A variable whose type is Optional should never itself be null; it should always point..

번호 맞추기
1~100 랜덤한 번호 맞추기 Up, Down 알려준다. 맞추면 횟수도 출력 후 종료 * Random 사용방법 java.lang.Math (따로 import 필요없음) : 정적 메소드 random() 이용 - Math.random() : 현재 시간을 seed로 사용 double num = Math.random(); int toi = (int)(num * 10); - 0.0~ 1.0인 double 값의 난수를 균일한 분포로 반환 -> *10 후 정수로 캐스팅 java.util.Random import java.util.Random; ... Random rand = new Random(); for (int i = 0; i < 10; i++) { int num = rand.nextInt(10)+1; //1~..