본문 바로가기

자바10

Collections 클래스(sort, reverse, min, max, binaryResearch) sort(): 정렬reverse(): 반대로 정렬max(), min(): 최대, 최소 구하기binarySearch(): 이진 검색 Collections 클래스의 메소드는 모두 static 타입이므로 Collections 객체를 생성할 필요가 없다 Math.max()와 Collections.max()의 차이점이 뭘까?Math.max()는 두 숫자 중 큰 수를 반환Collections.max()는 컬렉션 내에서 가장 큰 수를 반환 - 즉 List나 Set에서 사용 public Static void main(String[] args) { LinkedList myList = new LinkedList; myList.add("a"); myList.add("b"); myList.add("c"); myList.add.. 2024. 6. 8.
NumberFormatException - 문자열을 정수형으로 변환할 때 나타나는 오류 long answer = 0; String str = "12345"; String[] arr = str.split(""); String sortedStr = Arrays.toString(arr); answer = Long.parseLong(sortedStr);→ Exception in thread "main" java.lang.NumberFormatException: For input string: "[1, 2, 3, 4, 5]" 문자열을 정수로 변환하려 할 때 정수형이 아닌 경우 나타나는 오류이다대괄호([])와 쉼표(,) 가 있기 때문에 나타났다 나는 프로그래머스 정수 내림차순으로 .. 2024. 5. 23.
Arrays.toString() - 배열 내용 출력 배열 자체를 출력하면 주소값이 나온다 String str = "hello"; String[] arr = str.split(""); System.out.println(arr);→ 출력결과: [Ljava.lang.String;@58d25a40 배열을 출력하기 위해 문자형으로 변환한다여기서 그냥 toString()만 하면 똑같이 주소값이 출력된다 String str = "hello"; String[] arr = str.split(""); System.out.println(arr.toString());→ 출력결과: [Ljava.lang.String;@58d25a40 따라서 Arrays.toString()을 이용하여 .. 2024. 5. 22.
repeat() 함수 - 문자열 반복 프로그래머스 - 문자열 곱하기 문제문제 설명문자열 my_string과 정수 k가 주어질 때, my_string을 k번 반복한 문자열을 return 하는 solution 함수를 작성해 주세요.입출력 예my_string k result"string"3"stringstringstring""love"10"lovelovelovelovelovelovelovelovelovelove"나는 for문을 돌려서 +=으로 누적했다class Solution { public String solution(String my_string, int k) { String answer = ""; for(int i=0; i 그리고 다른 사람의 풀이로 들어가니 repeat() 함수를 발견했다repe.. 2024. 5. 22.