简体   繁体   English

ArrayList超出范围错误

[英]ArrayList out of bounds error

I'm building a program for class and I'm having a problem with sorting my array list. 我正在为类构建程序,但在对数组列表进行排序时遇到问题。 I need the elements of the arraylist to be sorted by the value of code. 我需要将arraylist的元素按代码的值排序。

public static ArrayList<MenuItem> orderByCode( ArrayList<MenuItem> items ){
  ArrayList<MenuItem> order=new ArrayList<MenuItem>(items.size());
  for (int i = 0; i < items.size(); i++) {
        for (int j = 1; j < (items.size() - i); j++) {
             if ( order.get(i).getCode()<order.get(i-1).getCode()){
                  order.add(items.get(j-1));
                  order.set(j-1, order.get(j));
                  order.set(j, order.get(order.size()-1));
             }
        }
  }
  return order;
 }

This is the part of the code having as=n issue. 这是代码中as = n问题的一部分。 If you need me to add the rest of the class or what I'm using to test it I can do that. 如果您需要我添加课程的其余部分或用于测试的内容,则可以这样做。

public static void main(String[] args){
  ArrayList<MenuItem> items = new ArrayList<MenuItem>();
  items.add( new MenuItem( "Big Bad Burger", 9.95, 2.15, 1, true ) );
  items.add( new MenuItem( "Cheeky Chicken", 5.95, 0.75, 1, true ) );
  items.add( new MenuItem( "Wild Wings", 5.95, 0.50, 0, true ) );
  items.add( new MenuItem( "Flying Fish", 15.95, 7.61, 1, false ) );
  items.add( new MenuItem( "Igloo Icecream", 1.95, 0.28, 2, true ) );

  ArrayList<MenuItem> ordered = orderByCode( items );

  // should list items in this order:
  // Wings, Burger, Chicken, Fish, Icecream
  for ( MenuItem item : ordered )
    System.out.println( item.menuString() );
}

There is the test code. 有测试代码。

I guess you are getting error for this line, 我想您在此行中遇到了错误,

if ( order.get(i).getCode()<order.get(i-1).getCode()){

Above line will throw ArrayList out of bound error for i=0; 上一行将使ArrayList超出i=0;的界限错误i=0;

Learn to use a debugger. 学习使用调试器。 Such problem should be easy to spot with aid of a debugger so that you know which line is causing problem, and what is the value of variables as of that moment. 在调试器的帮助下,应该很容易发现这种问题,以便您知道引起问题的是哪条线,以及那一刻变量的值是多少。

Anyway, although I have no clue what you are trying to do, this line is obviously having problem: 无论如何,尽管我不知道您要做什么,但这行显然有问题:

if ( order.get(i).getCode()<order.get(i-1).getCode()){

For i = 0, you will have problem for order.get(i-1) 对于i = 0,您会order.get(i-1)

check the size of items, for i is going through the items arraylist, but is used to access order, you may want to change that to 检查项目的大小,因为我正在遍历项目arraylist,但是用于访问订单,您可能需要将其更改为

for (int i=0;i<order.size();i++)

good luck! 祝好运!

这条线需要固定

if (order.get(i).getCode() < order.get(i - 1).getCode())

When iterating through an ArrayList, create an Iterator. 当遍历ArrayList时,创建一个Iterator。

Iterator<MenuItem> i = order.iterator();
while (i.hasNext())
{
   MenuItem current = i.next();
   // perform operations on current
}

Alternatively, convert your ArrayList into an array, and iterate over it 或者,将您的ArrayList转换为数组,并对其进行迭代

MenuItem[] orderArray = order.toArray(new MenuItem[order.size()]);
for (int i = 0; i < orderArray.length; i++)
{
   // perform operations on each order by using orderArray[i].relevantMethod()
}

Cleaning up your code like this will allow you to better understand what is going on. 这样清理代码将使您更好地了解正在发生的事情。

Good luck! 祝好运!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM