线程"main" java.lang.NumberFormat异常:对于输入字符串:"[C@378bf509"



我有两个问题。

  1. 为什么我可以在字符串temp =新字符串(数字)上运行,但不能没有它们?

  2. 它与下面的代码有什么不同?这不会警告错误

int src = Integer.parseInt(String.valueOf(numbers[i]));
int trg = Integer.parseInt(String.valueOf(numbers[j]));

numbers[i] = (char)(trg+'0');
numbers[j] = (char)(src+'0');
dfs(cnt-1, i, numbers);
numbers[i] = (char)(src+'0');
numbers[j] = (char)(trg+'0');

有问题的代码
// 4. dfs function
public static void dfs(int cnt, int start, char[] numbers) {

// 4-1. finish condition
if(cnt==0) {
//          String temp = new String(numbers);
int current = Integer.parseInt(numbers.toString());
if(current>result) {
result = current;
}
return;
}
// 4-2. change execution
for(int i=start; i<numbers.length-1; ++i) {
for(int j=i+1; j<numbers.length; ++j) {
char src = numbers[i];
char trg = numbers[j];

numbers[i] = trg;   
numbers[j] = src;
dfs(cnt-1, i, numbers);
numbers[i] = src;   
numbers[j] = trg;

}
}

错误控制台

3
123 1
2737 1
32888 2Exception in thread "main" java.lang.NumberFormatException: For input string: "[C@378bf509"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:668)
at java.base/java.lang.Integer.parseInt(Integer.java:786)
at Solution.dfs(Solution.java:35)
at Solution.dfs(Solution.java:50)
at Solution.main(Solution.java:20)

numbers为数组。数组的默认toString类似于[C@378bf509]。new String(char[])将char[]转换为String对象。

相关内容

  • 没有找到相关文章

最新更新