Java ArrayList,获取值,从 index() 到 index()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21752760/
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
ArrayList, Getting values, from index() to index()
提问by Heneko
Hi everybody I am trying to get values from an ArrayList I created. I have some indexes, and I want to print the data ONLY between the indexes I want, so far I ave done this, but it does not seem to work. I think the get() is not valid for want I want... Any ideas?
大家好,我正在尝试从我创建的 ArrayList 中获取值。我有一些索引,我只想在我想要的索引之间打印数据,到目前为止我已经这样做了,但它似乎不起作用。我认为 get() 对我想要的东西无效......有什么想法吗?
public static void main(String[] args) throws Exception {
Scanner dataSc = new Scanner(new FileReader("StudData1.txt"));
ArrayList<String> ArrayData = new ArrayList<String>();
ArrayList<String> idData = new ArrayList<String>();
ArrayList<String> idIndex = new ArrayList<String>();
int b = 0;
int a = 0;
int i = 0;
while (dataSc.hasNextLine()) {
String data = dataSc.nextLine();
ArrayData.add(i, data);
if (data.contains("ID: ")) {
idData.add(a, data);
idData.set(a, (idData.get(a).replaceAll("[\D]", "")));
a++;
b++;
}
i++;
idIndex.add(b, Integer.toString(i));
}
int idSt1 = Integer.parseInt(idData.get(0));
int idSt2 = Integer.parseInt(idData.get(1));
int idSt3 = Integer.parseInt(idData.get(2));
int idxID1 = Integer.parseInt(idIndex.get(0));
int idxID2 = Integer.parseInt(idIndex.get(1));
int idxId3 = Integer.parseInt(idIndex.get(2));
if (idSt1 < idSt2 && idSt2 < idSt3) {
System.out.println(ArrayData.get(idxID1-3 , idxID2-3 );}
}
}
}
}
采纳答案by kai
Its easy done with a for-loop.
使用for循环很容易完成。
for(int i = startindex+1; i<endindex; i++) {
System.out.println(ArrayData.get(i));
}
This loop will print all objects in the arraylist that are between the given indices.
But there is no method called get() which returns a collection of items that are between to given indices, you can only use the subList(arg0, arg1)
method to creat a subcollection and then iterate over this subcollection. Take a look at the Docs http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#get%28int%29
此循环将打印数组列表中给定索引之间的所有对象。但是没有名为 get() 的方法返回给定索引之间的项目集合,您只能使用该subList(arg0, arg1)
方法创建一个子集合,然后迭代这个子集合。看看文档http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#get%28int%29
Example:
例子:
List<String> al = new ArrayList<>();
al.add("a");
al.add("b");
al.add("c");
al.add("d");
List<String> sublist = al.subList(0, 3); //inclusive index 0, exclusive index 3
for(String s : sublist)
System.out.println(s);
Output: a, b, c
输出:a、b、c
回答by Ruchira Gayan Ranaweera
You can take range of values by index from ArrayList
as follows.
您可以ArrayList
按如下方式按索引获取值范围。
List<String> list=new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
for(int i=0;i<4;i++){
if(i<=2 && i>=1){
System.out.println(list.get(i));
}
}
回答by Zeeshan
Problem is in your code:
问题出在您的代码中:
System.out.println(ArrayData.get(idxID1-3 , idxID2-3 );
The getmethod of class ArrayListaccepts only one argument, here you are passing two.
ArrayList类的get方法只接受一个参数,这里传递了两个参数。
You can use subList(int fromIndex, int toIndex)
to get your desired result.
您可以使用subList(int fromIndex, int toIndex)
来获得您想要的结果。
回答by tr4pt
i would do it like that
我会那样做
if (idSt1 < idSt2 && idSt2 < idSt3) {
for (int j = idxID1-3; j < idxID2-3; j++){
System.out.println(ArrayData.get(j));
}
}