我有一个问题,我得到了这样的错误:[[Ljava.lang.Integer;@26f0a63f]

huangapple go评论73阅读模式
英文:

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&lt;Integer[]&gt; twoSum(int[] numbers, int targets) {
		 Map&lt;Integer, Integer&gt; arrayMap = new HashMap&lt;&gt;();
			List&lt;Integer[]&gt; pairList = new ArrayList&lt;&gt;();
			
			for (int i = 0; i &lt; 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&lt;Integer[]&gt; 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&lt;Integer[]&gt;.

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 &quot;[Ljava.lang.Integer;@26f0a63f&quot;. 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&lt;Integer[]&gt;:

System.out.println(&quot;[&quot;);
for (final Integer[] nested : index) {
    System.out.printf(&quot;  %s,\n&quot;, Arrays.toString(nested));
}
System.out.println(&quot;]&quot;);

An arguably better way of writing this, because it uses the List.toString method, is using the stream API:

final List&lt;String&gt; 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!)

huangapple
  • 本文由 发表于 2020年10月8日 17:22:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64259490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定