Java 迭代 Arraylist<String[]>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37167378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterate over Arraylist<String[]>
提问by Essej
I have an ArrayList full with String arrays that I can't figure out how to loop through. Iterators are new to me and even though I have used them in the past, I'm not sure what to do here.
我有一个充满字符串数组的 ArrayList,我不知道如何循环。迭代器对我来说是新的,尽管我过去使用过它们,但我不知道在这里做什么。
Iterator<String[]> iterator = list.iterator();
while (iterator.hasNext()) {
String trip = "Till: " + iterator.next(0) + " | Fr?n: " + list.get(4) + " | Kostar: " + list.get(1) +"kr | Avg?r: " + list.get(2) + " | Ankomst Tid: " + list.get(3) + " | Antal bokningar: " + list.get(4);
This is some experimenting I am doing. I though that iterator.next() would return the StringArray on the current row so I have tried for example iterator.next(0). As I understand list.get(x) only gives me the row.
这是我正在做的一些实验。我认为 iterator.next() 会返回当前行上的 StringArray,所以我尝试过例如 iterator.next(0)。据我了解 list.get(x) 只给我行。
In the code above, I want every rows String Array at a certain index. Something like this:
在上面的代码中,我希望在某个索引处的每一行字符串数组。像这样的东西:
for each row
system.out.println(String[2] +String[5]);
采纳答案by cyroxis
That is because you have a list of arrays, not a list of Strings.
那是因为您有一个数组列表,而不是一个字符串列表。
List<String[]> list = //some data
for (String[] array : list){
System.out.println(array[3] + ":" + array[5]);
}
If you want to iterate over each string you will have to iterate twice.
如果要迭代每个字符串,则必须迭代两次。
List<String[]> list = //some data
for (String[] array : list){
for (String s : array){
System.out.println(s);
}
}
EDIT
编辑
The above is shorthand for using an iterator.
以上是使用迭代器的简写。
List<String[]> list = //some data
Iterator<String[]> itr = list.iterator();
while (itr.hasNext()){
String[] array = itr.next(); // You were just missing saving the value for reuse
System.out.println(array[3] + ":" + array[5]);
}
回答by StuPointerException
You can do this without having to worry about iterators in your code:
您可以这样做而不必担心代码中的迭代器:
List<String[]> test = new ArrayList<>();
for(String[] array : test) {
//Iterate over inner array:
for(String s : array) {
System.out.println(s);
}
//or access directly
System.out.println(array[0]);
}
回答by Zircon
Iterator<String[]> iterator = list.iterator();
while (iterator.hasNext()) {
String[] row = iterator.next();
//Do stuff with row
}
If you're using a list, you can also use a for loop:
如果您使用的是列表,您还可以使用 for 循环:
for(String[] row : list) {
//Do stuff with row
}
回答by David
Each item in the ArrayList is a String[], so iterator.next() returns a String[]. Once you have that String array, you can access it like any other array with array index notation. item[0]
is the first item in the array, item.length
tells you how many items are in the array.
ArrayList 中的每一项都是一个 String[],所以 iterator.next() 返回一个 String[]。一旦您拥有该 String 数组,您就可以像使用数组索引符号的任何其他数组一样访问它。item[0]
是数组中的第一项,item.length
告诉您数组中有多少项。
Where you're calling list.get(4)
will also return an array of strings, which almost certainly isn't what you want in the output.
你调用的地方list.get(4)
也会返回一个字符串数组,这几乎肯定不是你想要的输出。