java.lang.IndexOutOfBoundsException:索引:1,大小:1

时间:2015-12-10 04:49:50

标签: java arraylist

就像标题所说,我的问题是,当我编译时,我在线程“main”中得到Exception java.lang.IndexOutOfBoundsException:Index:1,Size:1。这是问题来自的部分。

编辑。

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1   
at java.util.ArrayList.rangeCheck(ArrayList.java:653)   
at java.util.ArrayList.get(ArrayList.java:429)  
at pa6.FlightFinder.bestDirectPrice(FlightFinder.java:117)  
at pa6.FlightFinder.main(FlightFinder.java:14)


public static ArrayList<String> bestDirectPrice(ArrayList<String> flightList,String city1, String city2) {
ArrayList<String> list = new ArrayList<String>();
ArrayList<String> Price = new ArrayList<String>();
for (int i=0; i<flightList.size(); i++) {
    list = directFlights(flightList, city1, city2);
    Price.add(getPrice(list.get(i)));

}
return Price;

我一直在网上寻找解决方案,所以我很确定问题是阵列太小了,但我还是不确定如何修复它。我试图让代码的上半部分只取出数组中的数字并将它们放入一个新数组中。

ArrayList<String> test = new ArrayList<String>(); //Array with list of flights
test.add("Orlando#DesMoines#194.88");
test.add("Portland#Orlando#287.74");
test.add("Buffalo#Boston#299.52"); 
test.add("Buffalo#Portland#264.80"); 
test.add("Chicago#Buffalo#223.56");

System.out.println(bestDirectPrice(test,"Buffalo","Orlando"));

以下是我正在调用的其他方法。

public static String getPrice(String price) { //Takes flight description, which is a string, as a parameter and returns price of flight
String[] sArray = price.split("#", -1);
String newPrice = "";
for (int i = 0; i<1; i++)       
    newPrice = sArray[2];
return newPrice;
}

public static ArrayList<String> directFlights(ArrayList<String> flightList, String city1, String city2) { // Method to create array list containing every direct flight from city1 to city 2
ArrayList<String> list = new ArrayList<String>();
for (int i=0; i<flightList.size(); i++){
    String city2a = getDestinationCity(flightList.get(i));
    String city1a = getOriginationCity(flightList.get(i));
    if (city1a.equals(city1) && city2a.equals(city2)) {
        list.add(flightList.get(i));
    }
}
return list;
}

我已经坚持了一段时间,所以任何帮助都会非常感激!

编辑。

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1   
at java.util.ArrayList.rangeCheck(ArrayList.java:653)   
at java.util.ArrayList.get(ArrayList.java:429)  
at pa6.FlightFinder.bestDirectPrice(FlightFinder.java:117)  
at pa6.FlightFinder.main(FlightFinder.java:14)

2 个答案:

答案 0 :(得分:2)

请查看这两个块

循环直到i < flightList.size()

但是getPrice(list.get(i)正在使用上面的索引,但是在不同的列表中

for (int i=0; i<flightList.size(); i++) {
    list = directFlights(flightList, city1, city2);
    Price.add(getPrice(list.get(i)));

}

内部方法,directFlights()

使用以下条件创建列表,即size of list will always be less than or equal to size of flightList

但是getPrice(list.get(i)以上正在使用父列表(flightList)的索引。

if (city1a.equals(city1) && city2a.equals(city2)) {
        list.add(flightList.get(i));
    }

答案 1 :(得分:1)

你的for循环是关于flightList,而不是list。 在每次迭代中,您都要将新值重新分配给列表,而不是添加新元素。

尝试更改

for (int i=0; i<flightList.size(); i++) {
    list = directFlights(flightList, city1, city2);
    Price.add(getPrice(list.get(i)));

}

list = directFlights(flightList, city1, city2);
for (int i=0; i<list.size(); i++) {
    Price.add(getPrice(list.get(i)));
}