英文:
Print ArrayList element without square bracket and comma
问题
我想要打印出ArrayList的所有元素,就像打印数组一样,而不需要从ArrayList创建数组。
实际上,
ArrayList a = new ArrayList();
a.add(1);
a.add(2);
a.add(3);
System.out.println(a);
输出结果为:[1, 2, 3]
我想要的输出结果是:1 2 3
我该如何实现?ArrayList中是否有相应的方法?
英文:
I wanted to print all element of an ArrayList just like an array, without creating array from ArrayList.
Actually,
ArrayList a=new ArrayList();
a.add(1);
a.add(2);
a.add(3);
System.out.println(a);
here output:[1,2,3]
I wanted to print these element like as : 1 2 3
How can I do that?Is there any method in ArrayList?
答案1
得分: 0
有几种方法可以做到这一点。但首先,您将需要指定列表将使用的类型。我还建议您使用List作为数据类型,而不是ArrayList。在您的示例中,它将是
List<Integer> a = new ArrayList<>();
要打印它,您可以调用
System.out.println(Arrays.toString(a.toArray()));
但我建议您使用for循环或for-each循环来执行打印。就像这样
// for循环:这将通过索引遍历列表
for (int i = 0; i < a.size(); i++) {
System.out.print(a.get(i));
}
System.out.println();
// for-each循环:这将通过迭代器遍历列表
for (Integer val : a) {
System.out.print(val + " ");
}
System.out.println();
希望我能帮到您。
英文:
There are several ways to do that. But first, you will have to specify which type the List will use. I also recommend you to use List as datatype and not ArrayList. In your example it would be
List<Integer> a = new ArrayList<>();
To print it, you can either call
System.out.println(Arrays.toString(a.toArray()));
but I would recommend to do it with a for-loop or a for-each-loop. Just like that
// for-loop: This will iterate through the list by index
for(int i=0; i < a.size(); i++){
System.out.print(a.get(i));
}
System.out.println();
// for-each: This will iterate through the list by Iterator
for(Integer val : a){
System.out.print(val + " ");
}
System.out.println();
I hope I could help you
答案2
得分: 0
也许你想使用 Lambda 表达式:
a.forEach(System.out::println);
a.forEach(elem -> System.out.print(elem + " "));
希望有所帮助。
英文:
Maybe you want to use Lambda Expressions:
a.forEach(System.out::println);
a.forEach( elem -> System.out.print(elem + " "));
HTH
答案3
得分: 0
使用StringJoiner类可以获得你的结果,方法如下:
List<Integer> a = new ArrayList<Integer>();
a.add(1);
a.add(2);
a.add(3);
StringJoiner sj = new StringJoiner(" ");
for (Integer elem : a) {
sj.add(elem.toString());
}
String spaceSeparatedNumbers = sj.toString();
System.out.println(spaceSeparatedNumbers); // <-- it will print 1, 2, 3
获得相同结果的另一种方法是使用Stream
和Collectors.joining
,方法如下:
List<Integer> a = new ArrayList<Integer>();
a.add(1);
a.add(2);
a.add(3);
String spaceSeparatedNumbers = a.stream()
.map(i -> i.toString())
.collect(Collectors.joining(" "));
System.out.println(spaceSeparatedNumbers); // <-- it will print 1, 2, 3
英文:
A possible way to obtain your result is to use StringJoiner class in this way:
List<Integer> a = new ArrayList<Integer>();
a.add(1);
a.add(2);
a.add(3);
StringJoiner sj = new StringJoiner(" ");
for (Integer elem : a) {
sj.add(elem.toString());
}
String spaceSeparatedNumbers = sj.toString();
System.out.println(spaceSeparatedNumbers); //<-- it will print 1, 2, 3
An alternative way to obtain the same result is a Stream
using Collectors.joining
in this way:
List<Integer> a = new ArrayList<Integer>();
a.add(1);
a.add(2);
a.add(3);
String spaceSeparatedNumbers = a.stream()
.map(i -> i.toString())
.collect(Collectors.joining(" "));
System.out.println(spaceSeparatedNumbers); //<-- it will print 1, 2, 3
专注分享java语言的经验与见解,让所有开发者获益!
评论