[英]how to print Multiple String arrays output in a single line
I have 3 String arrays. 我有3个String数组。 I want to print the all the 3 single arrays one after another in java.
我想在Java中一个接一个地打印所有3个单个数组。 So like my 2 arrays look like-
所以像我的2个数组看起来像-
o
ooo
ooooo
ooo
o
o o
ooooo
ooo
ooooo
o oo
I want to print each element of the array one after another (right next to eachother) 我想一个接一个地打印数组的每个元素(彼此相邻)
Current code result- 当前代码结果-
o
ooo
ooooo
ooo
o
o o
ooooo
ooo
ooooo
o oo
o o
ooo
ooooo
ooo
o
So expected output 如此预期的输出
o o o o o
ooo ooooo oooo
ooooo ooo oooo
ooo ooooo oooo
The output above may differ from inputs, but its just the sample I am showing . 上面的输出可能与输入不同,但是它只是我正在显示的示例。 This is how I am expecting to print.
这是我期望打印的方式。
current code- 当前代码
String[] tp1 = { " o ", " ooo ", "ooooo", " ooo ", " o " };
String[] tp2 = { " o o", "ooooo", " ooo ", "ooooo", " o oo" };
String[] tp3 = { " o o ", " ooo ", "ooooo", " ooo ", " o " };
List<String[]> values = new ArrayList<>();
values.add(tp1);
values.add(tp2);
values.add(tp3);
for (String[] strings : values) {
String output = "";
for (String string : strings) {
output += string;
output += "\n";
}
System.out.print(output);
}
You need to invert your for loops. 您需要反转for循环。 And hence you cannot you the
for each
syntax. 因此,您不能
for each
语法使用。
Use, 采用,
for (int i = 0; i < tp1.length; i++) {
for (int j = 0; j < values.size(); j++) {
System.out.print(values.get(j)[i]);
}
System.out.println(" ");
}
Using Java 8 you can use the new forEach and a Lambda like this: 使用Java 8,您可以像下面这样使用新的forEach和Lambda:
values.forEach(value -> {
Arrays.asList(value).forEach(array -> System.out.print(array));
System.out.println();
});
which results in 导致
o ooo ooooo ooo o
o oooooo ooo ooooo o oo
o o ooo ooooo ooo o
If you realy want this: 如果您真的想要这样:
o o o o o
ooo ooooo ooo
ooooo ooo ooooo
ooo ooooo ooo
o o oo o
You can do this like so: 您可以这样做:
for (int i=0; i<tp1.length; i++) {
final int j = i;
values.forEach(value -> System.out.print(value[j]));
System.out.println();
};
But then I would recommend you to flip the list and or the arrays. 但是,我建议您翻转列表和/或数组。 Since it is not clear if all arrays tp1..3 will have the same length.
由于尚不清楚所有数组tp1..3是否具有相同的长度。 For example this way:
例如,这种方式:
String[] tp1 = { " o ", " o o", " o o "};
String[] tp2 = { " ooo ", "ooooo", " ooo "};
String[] tp3 = { "ooooo", " ooo ", "ooooo" };
String[] tp4 = { " o ", " o oo" , " o " };
ArrayList<String[]> values = new ArrayList<>();
values.add(tp1);
values.add(tp2);
values.add(tp3);
values.add(tp4);
values.forEach(value -> {
Arrays.asList(value).forEach(array -> System.out.print(array));
System.out.println();
});
To get that output you don't require the values
variable. 要获得该输出,您不需要
values
变量。 You have to output the 1st string of all the arrays in a single line so change your for
loop to 您必须在一行中输出所有数组的第一个字符串,因此请将
for
循环更改for
for (int i = 0; i < 5; i++) {
System.out.println(tp1[i]+tp2[i]+tp3[i]);
}
I feel this should solve your problem. 我认为这应该可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.