在迭代兩個列表並顯示佈局中的項目時,我會在下面一行中獲取數組索引越界異常。如何獲得一個空檢查或一個條件使用下面的parallellist此數組索引超出範圍異常:Android
public class ParallelList<T> implements Iterable<List<T>> {
private final List<List<T>> lists;
public ParallelList(List<T>... lists) {
this.lists = new ArrayList<List<T>>(lists.length);
this.lists.addAll(Arrays.asList(lists));
}
public Iterator<List<T>> iterator() {
return new Iterator<List<T>>() {
private int loc = 0;
public boolean hasNext() {
boolean hasNext = false;
for (List<T> list : lists) {
hasNext |= (loc < list.size());
}
return hasNext;
}
public List<T> next() {
List<T> vals = new ArrayList<T>(lists.size());
for (int i=0; i<lists.size(); i++) {
if(loc < lists.get(i).size()){
vals.add(lists.get(i).get(loc));
}
// vals.add(loc < lists.get(i).size() ? lists.get(i).get(loc): null);
}
loc++;
return vals;
}
public void remove() {
for (List<T> list : lists) {
if (loc < list.size()) {
list.remove(loc);
}
}
}
};
}
}
錯誤不會得到錯誤
ParallelList<BaseItem> list = new ParallelList<BaseItem>(highlighted, normal);
for (List<BaseItem> ints : list) {
final BaseItem itemH = ints.get(0);
if(!ints.get(1).equals(null) && ints.size()!=0){ // error here at this line
final BaseItem itemn = ints.get(1);
}
在這裏
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
這將崩潰的時候'ints.size()= = 1' –