英文:
i have question i got error like this [[Ljava.lang.Integer;@26f0a63f]
问题
以下是翻译好的内容:
public class Solution {
private static List<Integer[]> twoSum(int[] numbers, int targets) {
Map<Integer, Integer> arrayMap = new HashMap<>();
List<Integer[]> pairList = new ArrayList<>();
for (int i = 0; i < numbers.length; i++) {
if (arrayMap.containsKey(targets - numbers[i])) {
Integer[] temp = { arrayMap.get(targets - numbers[i]), i }; // 将先前的索引和当前索引放入数组
pairList.add(temp);
}
else arrayMap.put(numbers[i], i);
}
return pairList;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[]nums = {2, 7, 2, 11, 15};
int target=9;
System.out.println(Arrays.toString(nums));
List<Integer[]> index = twoSum(nums, target);
System.out.println(index.toString());
}
}
但我想运行时出现了以下错误:
[[Ljava.lang.Integer;@26f0a63f]
如何解决我的问题?
英文:
i have source code like this
public class Solution {
private static List<Integer[]> twoSum(int[] numbers, int targets) {
Map<Integer, Integer> arrayMap = new HashMap<>();
List<Integer[]> pairList = new ArrayList<>();
for (int i = 0; i < numbers.length; i++) {
if (arrayMap.containsKey(targets - numbers[i])) {
Integer[] temp = { arrayMap.get(targets - numbers[i]), i }; // Put the previous and current index
pairList.add(temp);
}
else arrayMap.put(numbers[i], i);
}
return pairList;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[]nums = {2, 7, 2, 11, 15};
int target=9;
System.out.println(Arrays.toString(nums));
List<Integer[]> index =twoSum(nums, target);
System.out.println(index.toString());
}
}
but i want to run i got error like this
[[Ljava.lang.Integer;@26f0a63f]
how to fix my problem ?
答案1
得分: 2
这不是错误消息。这是在类型为List<Integer[]>
的实例上调用toString
方法的结果。
简单来说,Java中的数组类型并未定义有用的toString
方法。它们打印的不是数组内容,而是数组的类型和其在内存中的地址,因此出现了"[Ljava.lang.Integer;@26f0a63f"
这样的结果。若要打印数组的内容,您需要调用静态辅助方法Arrays.toString
。在您的情况下,您需要手动为List<Integer[]>
中的每个项目执行这一操作:
System.out.println("[");
for (final Integer[] nested : index) {
System.out.printf(" %s,\n", Arrays.toString(nested));
}
System.out.println("]");
一种更好的编写方式是使用流式API,因为它使用了List.toString
方法:
final List<String> indexStr = index.stream()
.map(Arrays::toString)
.collect(Collectors.toList());
System.out.println(indexStr);
(请注意,我没有显式调用indexStr.toString()
— 将对象传递给println
会自动执行这一操作!)
英文:
This isn’t an error message. It’s the result of calling toString
on an instance of type List<Integer[]>
.
Simply put, array types in Java don’t define a useful toString
method. Instead of the array contents, they print the type of the array and its address in memory, hence the "[Ljava.lang.Integer;@26f0a63f"
. To print an array’s contents, you need to invoke the static helper method Arrays.toString
instead. And in your case you will need to do this manually for every item in your List<Integer[]>
:
System.out.println("[");
for (final Integer[] nested : index) {
System.out.printf(" %s,\n", Arrays.toString(nested));
}
System.out.println("]");
An arguably better way of writing this, because it uses the List.toString
method, is using the stream API:
final List<String> indexStr = index.stream()
.map(Arrays::toString)
.collect(Collectors.toList());
System.out.println(indexStr);
(Note that I’m not calling indexStr.toString()
explicitly — passing an object to println
does that automatically for me!)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论