简体   繁体   English

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

[英]java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

Like the title says, my problem is that when I compile I get Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1. Here is the part where the problem is coming from. 就像标题所说的那样,我的问题是,当我编译时,线程“ main”中出现异常java.lang.IndexOutOfBoundsException:索引:1,大小:1.这是问题的根源。

Edit. 编辑。

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;

I've been searching online for a solution, so I'm pretty sure the problem is that the array is too small, but i'm still not sure how to fix it. 我一直在网上寻找解决方案,所以我很确定问题是数组太小了,但是我仍然不确定如何解决它。 I'm trying to get the upper part of code to take just the numbers out of the array and put them in a new one. 我正在尝试获取代码的上半部分,以便仅将数字从数组中取出并放入新的数字中。

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"));

And here are the other methods I'm calling. 这是我正在调用的其他方法。

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;
}

I've been stuck on this for awhile so any help would be greatly appreciated! 我已经坚持了一段时间,所以任何帮助将不胜感激!

Edit. 编辑。

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)

Please look into these two blocks 请研究这两个区块

Looping until i < flightList.size() 循环直到i < flightList.size()

But getPrice(list.get(i) is using index from above but on different list 但是getPrice(list.get(i)从上面使用索引但在不同的列表上

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

}

Inside Method , directFlights() 内部方法, directFlights()

List is created using below condition, meaning size of list will always be less than or equal to size of flightList . 使用以下条件创建列表,这意味着size of list will always be less than or equal to size of flightList

But above getPrice(list.get(i) is using indexing from parent list (flightList). 但是在getPrice(list.get(i)是使用父列表(flightList)的索引。

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

Your for loop is with respect to flightList, not list. 您的for循环是关于flightList,而不是list。 In every iteration you're reassigning a new value to list rather than adding a new element. 在每次迭代中,您都在重新分配要列出的新值,而不是添加新元素。

Try changing 尝试改变

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

}

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IndexOutOfBoundsException:索引:2,大小:0 - java.lang.IndexOutOfBoundsException: Index: 2, Size: 0 java.lang.IndexOutOfBoundsException:索引:1,大小:1 onBindViewHolder - java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 onBindViewHolder java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 in Kotlin - java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 in Kotlin GSEA:java.lang.IndexOutOfBoundsException:索引:0,大小:0 - GSEA : java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 java.lang.IndexOutOfBoundsException:无效的索引5,大小为5 - java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5 例外:java.lang.IndexOutOfBoundsException:索引:0,大小:0 - Exception : java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 java.lang.IndexOutOfBoundsException:索引:5,大小:5 - java.lang.IndexOutOfBoundsException: Index: 5, Size: 5 java.lang.IndexOutOfBoundsException:无效的索引0,大小为0 - java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 异常 java.lang.IndexOutOfBoundsException:索引:1,大小:1 - Exception java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 java.lang.IndexOutOfBoundsException:索引:3,大小:3 - java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM