java 将列表元素显示为字符串
java displaying List element as string
我正在学习 Stream 教程,但我不明白我需要做什么才能让 List 元素将内容输出为其中包含的数字。
Given two lists of numbers, how would you return all pairs of numbers? For example, given a list [1, 2, 3] and a list [3, 4] you should return [(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)]. For simplicity, you can represent a pair as an array with two elements.
我已经阅读了一些关于覆盖 toString() 的帖子,但我没有要覆盖的对象。我显然遗漏了一些关于 'pairs' 的性质,是因为 'pairs' 是一个 int[][] 列表吗?
package testcode;
import java.util.Arrays;
import java.util.List;
import static java.util.stream.Collectors.toList;
public class TestCode {
public TestCode(){
testStream();
}
public static void main(String[] args) {
TestCode tC = new TestCode();
}
public void testStream(){
List<Integer> numbers1 = Arrays.asList(1,2,3);
List<Integer> numbers2 = Arrays.asList(3,4);
List<int[]> pairs = numbers1.stream()
.flatMap(i -> numbers2.stream()
.map(j -> new int[]{i, j})
)
.collect(toList());
pairs.forEach(System.out::println);
//I have also tried
//String a = Arrays.toString(pairs); // but no suitable method found.
String list = pairs.toString();
System.out.println("list = " + list);
//tried converting pairs element to a List<String> using concatenation to force it be a string.
List<String> stringsList = new ArrayList<String>(pairs.size());
for (int i = 0 ; i < pairs.size() ; i++) {
stringsList.add("" + pairs.get(i));
}
stringsList.forEach(System.out::println);
// or
System.out.println(" ** test 2");
String[] stringsList2 = new String[pairs.size()];
for (int i = 0 ; i < pairs.size() ; i++) {
stringsList2[i] = "" + pairs.get(i);
System.out.println(stringsList2[i]);
}
}
阅读干杯。
Arrays.deepToString(Object[] a)
应该可以满足您的需求。
deepToString
将递归打印多维数组的元素。
例子
代码
import java.util.Arrays;
public class Test{
public static void main(String[] args){
int[][] test = {{1,2,3},{4,5,6},{7,8,9}};
System.out.println(Arrays.deepToString(test));
}
}
输出
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
只需使用嵌套的 for
循环循环遍历数组并连接转换后的字符串。
public static String test_method() {
int[] array1 = {1, 2, 3};
int[] array2 = {3, 4};
String string = "";
for (int i: array1) {
for (int j: array2) {
String sub = "(" + String.valueOf(i) + ", "
+ String.valueOf(j) + ") ";
string += sub;
}
}
return string;
}
我正在学习 Stream 教程,但我不明白我需要做什么才能让 List 元素将内容输出为其中包含的数字。
Given two lists of numbers, how would you return all pairs of numbers? For example, given a list [1, 2, 3] and a list [3, 4] you should return [(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)]. For simplicity, you can represent a pair as an array with two elements.
我已经阅读了一些关于覆盖 toString() 的帖子,但我没有要覆盖的对象。我显然遗漏了一些关于 'pairs' 的性质,是因为 'pairs' 是一个 int[][] 列表吗?
package testcode;
import java.util.Arrays;
import java.util.List;
import static java.util.stream.Collectors.toList;
public class TestCode {
public TestCode(){
testStream();
}
public static void main(String[] args) {
TestCode tC = new TestCode();
}
public void testStream(){
List<Integer> numbers1 = Arrays.asList(1,2,3);
List<Integer> numbers2 = Arrays.asList(3,4);
List<int[]> pairs = numbers1.stream()
.flatMap(i -> numbers2.stream()
.map(j -> new int[]{i, j})
)
.collect(toList());
pairs.forEach(System.out::println);
//I have also tried
//String a = Arrays.toString(pairs); // but no suitable method found.
String list = pairs.toString();
System.out.println("list = " + list);
//tried converting pairs element to a List<String> using concatenation to force it be a string.
List<String> stringsList = new ArrayList<String>(pairs.size());
for (int i = 0 ; i < pairs.size() ; i++) {
stringsList.add("" + pairs.get(i));
}
stringsList.forEach(System.out::println);
// or
System.out.println(" ** test 2");
String[] stringsList2 = new String[pairs.size()];
for (int i = 0 ; i < pairs.size() ; i++) {
stringsList2[i] = "" + pairs.get(i);
System.out.println(stringsList2[i]);
}
}
阅读干杯。
Arrays.deepToString(Object[] a)
应该可以满足您的需求。
deepToString
将递归打印多维数组的元素。
例子
代码
import java.util.Arrays;
public class Test{
public static void main(String[] args){
int[][] test = {{1,2,3},{4,5,6},{7,8,9}};
System.out.println(Arrays.deepToString(test));
}
}
输出
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
只需使用嵌套的 for
循环循环遍历数组并连接转换后的字符串。
public static String test_method() {
int[] array1 = {1, 2, 3};
int[] array2 = {3, 4};
String string = "";
for (int i: array1) {
for (int j: array2) {
String sub = "(" + String.valueOf(i) + ", "
+ String.valueOf(j) + ") ";
string += sub;
}
}
return string;
}