java.lang.IndexOutOfBoundsException:索引:1,大小:1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34193898/
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
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
提问by Scheids
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: Index: 1, Size: 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)
采纳答案by Dimitre Bogdanov
Your for loop is with respect to flightList, not list. In every iteration you're reassigning a new value to list rather than adding a new element.
你的for循环是关于flightList的,而不是list。在每次迭代中,您都将一个新值重新分配给列表,而不是添加一个新元素。
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)));
}
回答by Ankur Singhal
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));
}