[英]Print list of list of integer
I have the following list:我有以下列表:
List<List<int[]>> graph;
How can I print the content of graph without using loop?如何在不使用循环的情况下打印图形的内容? I tried the following 2 methods but all of them failed to print:我尝试了以下两种方法,但都无法打印:
int[][] input=new int[][]{{1,2,5},{1,3,6},{2,3,1}};
List<List<int[]>> graph = new ArrayList<>();
for (int i = 0; i <= 3; i++) graph.add(new ArrayList<>());
for (int[] conn : input) {
int city_A = conn[0], city_B = conn[1], price = conn[2];
graph.get(city_A).add(new int[] {city_B, price});
graph.get(city_B).add(new int[] {city_A, price});
}
graph.forEach(s->System.out.println("Output:"+s));
System.out.println(Arrays.deepToString(graph.toArray()));
Output:[[I@1fb3ebeb, [I@548c4f57] Output:[[I@1218025c, [I@816f27d] Output:[[I@87aac27, [I@3e3abc88] Output:[[I@1fb3ebeb, [I@548c4f57] Output:[[I@1218025c, [I@816f27d] Output:[[I@87aac27, [I@3e3abc88]
Expected output is to print each element without going to new line:预期 output 是打印每个元素而不换行:
Output: [[1,2], [1,3], [2,0]] Output:[[1,2],[1,3],[2,0]]
Edit: the original question has changed.编辑:原来的问题已经改变。
I think what you actually want is a string representation of your graph.Have you tried a simple我认为您真正想要的是图形的字符串表示形式。您尝试过一个简单的
System.out.println(graph);
? ?
Leaving the answer to the original question up, which was how to print all the integers in the inner list without using a traditional for loop.留下原始问题的答案,即如何在不使用传统 for 循环的情况下打印内部列表中的所有整数。
graph.forEach(innerList -> {
innerList.forEach(s-> System.out.println("Output: "+ s))
});
But why though..但是为什么虽然..
Also the forEach is just the java functional library shorthand for a traditional for loop, so I'm not quite sure what you're gaining here.此外,forEach 只是传统 for 循环的 java 函数库简写,所以我不太确定你在这里获得了什么。 Hope that helps希望有帮助
Try this.尝试这个。
graph.stream
creates a stream of List<int[]>
graph.stream
创建List<int[]>
的 streamflatmap(List::stream)
takes those lists and creates a single stream of int[]
flatmap(List::stream)
获取这些列表并创建一个int[]
的 streamArrays.toString
takes an array and prints it separated by commas. Arrays.toString
接受一个数组并用逗号分隔打印它。 Note that printing an array is printing an object
.请注意,打印数组就是打印object
。 Its default toString
is what you see when you print it so it won't work.它的默认toString
是您在打印时看到的内容,因此它不起作用。 Arrays.toString()
actually iterates over each element and returns a string that is printable. Arrays.toString()
实际上迭代每个元素并返回一个可打印的字符串。
List<List<int[]>> graph = some list;
graph.stream().flatMap(List::stream)
.forEach(arr->System.out.print(Arrays.toString(arr) + " "));
More on printing arrays更多关于打印 arrays
Notice the numeric part (in hex) for printing out this array.注意打印出这个数组的数字部分(十六进制)。 The numeric part comes from the hashCode.数字部分来自 hashCode。 Also the [I
in front of the first output means a simple int array.还有第一个 output 前面的[I
表示一个简单的 int 数组。 [[I
signifies an array of int arrays. [[I
表示一个 int arrays 数组。
int[] arr1 = {1,2};
System.out.println(arr1); // prints [I@4617c264 on my machine
System.out.println(Integer.toHexString(arr1.hashCode()));
int[][] arr2 = {{1,2},{3,4}};
System.out.println(arr2); // prints [[I@36baf30c on my machine
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.