리스트의 null 체크는 isEmpty() 를 사용하자.
내부구조가 결국 return 타입이 boolean 이고, 요소의 존재 유무를 체크하기 때문.
더 나은 방법이 있다면 더 나은 방법을 쓰는 것이 맞다.
List<String> emptyTest1 = new ArrayList<>();
List<String> emptyTest2 = new ArrayList<>();
String test1 = "test1";
String test2 = "test2";
emptyTest1.add(test1);
emptyTest1.add(test2);
for (String listChild : emptyTest1) { // 리스트에 들어있는 값
System.out.println("listChild = " + listChild);
}
if (emptyTest1.size() == 0) { // 사이즈가 0이면,
System.out.println("사이즈 0");
} else { // 사이즈가 0이 아니면,
System.out.println("emptyTest.size = " + emptyTest1.size());
}
if (emptyTest1.isEmpty()) {
System.out.println("emptyTest1 empty Y");
} else {
System.out.println("emptyTest1 empty N");
}
System.out.println("=======================================================================");
if (emptyTest2.isEmpty()) {
System.out.println("emptyTest2 empty Y");
} else {
System.out.println("emptyTest2 empty N");
}
'자바' 카테고리의 다른 글
배열을 리스트로 (0) | 2022.12.21 |
---|---|
SimpleDateFormat (0) | 2022.09.16 |
Calendar, Date (0) | 2021.05.23 |
Wrapper 클래스 (0) | 2021.05.23 |
split, 문자열 자르기 (0) | 2021.05.23 |