👉 기본 환경
- Language: Java
- IDE: Sublime
⌨️ 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import java.util.Arrays;
import java.util.Collections;
class Solution {
public String solution(String s) {
String answer = "";
char[] sToCharArr = new char[s.length()];
for(int i=0; i<s.length(); i++){
sToCharArr[i] = s.charAt(i);
}
Arrays.sort(sToCharArr, Collections.reverseOrder());
StringBuilder sb = new StringBuilder();
for(int i=s.length()-1; i>-1; i--){
sb.append(sToCharArr[i]);
}
answer = sb.toString();
return answer;
}
}
|
🖨️오류
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/Solution.java:11: error: no suitable method found for sort(char[],Comparator<Object>)
Arrays.sort(sToCharArr, Collections.reverseOrder());
^
method Arrays.<T#1>sort(T#1[],Comparator<? super T#1>) is not applicable
(inference variable T#1 has incompatible bounds
equality constraints: char
lower bounds: Object)
method Arrays.<T#2>sort(T#2[],int,int,Comparator<? super T#2>) is not applicable
(cannot infer type-variable(s) T#2
(actual and formal argument lists differ in length))
where T#1,T#2 are type-variables:
T#1 extends Object declared in method <T#1>sort(T#1[],Comparator<? super T#1>)
T#2 extends Object declared in method <T#2>sort(T#2[],int,int,Comparator<? super T#2>)
|
📡 원인
기본형 타입에 대한 Comparator가 존재하지 않음
📰 해결 방법
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import java.util.Arrays;
import java.util.Collections;
class Solution {
public String solution(String s) {
String answer = "";
String[] sToStringArr = s.split("");
Arrays.sort(sToStringArr, Collections.reverseOrder());
StringBuilder sb = new StringBuilder();
for(int i=0; i<sToStringArr.length; i++){
sb.append(sToStringArr[i]);
}
answer = sb.toString();
return answer;
}
}
|
객체 타입의 배열 사용
📚 참고 자료
'Java > Java with Error' 카테고리의 다른 글
[해결 방법] no suitable constructor found for Jackson2JsonRedisSerializer(Class<CAP#1>) (0) | 2024.11.24 |
---|---|
[해결 방법] java.lang.ClassCastException (0) | 2023.08.15 |
[해결 방법] Syntax error (0) | 2023.08.09 |
[해결 방법] java.lang.Error (0) | 2023.08.09 |
[해결 방법] java.lang.Error (0) | 2023.06.06 |