简体   繁体   English

气泡排序arraylis不起作用

[英]Bubble sorting arraylis is not working

I'm trying to bubble sort car make and year, where I would have the car year sorted and if two car makes are in the same year, then they are sorted alphabetically. 我正在尝试对汽车的制造商和年份进行泡沫排序,将汽车的年份进行排序,如果同一年有两个汽车制造商,那么它们将按字母顺序排序。 My program works up to the point where I call BubbleSorted(). 我的程序可以工作到调用BubbleSorted()的地步。 It's giving me an error java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 and I don't know why. 它给我一个错误java.lang.IndexOutOfBoundsException:索引:0,大小:0,我不知道为什么。 My program seems to be correct. 我的程序似乎是正确的。 Below is my program. 下面是我的程序。 I have 3 classes(main, bubblesortCars, GetCarInfo). 我有3个类(主要,bubbleortCars,GetCarInfo)。

import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.FileReader;


public class TheMain {

    public static void main(String[] args) {

        Scanner keyboard=new Scanner(System.in);
        int choice;
        boolean done = false;

        try{
            String filename1 = "Demo.txt";
            FileReader inputFile = new FileReader(filename1);

            //Instantiate the BufferedReader Class
            BufferedReader bufferReader = new BufferedReader(inputFile);
            ArrayList<GetCarInfo> CarList = new ArrayList();

            //Variable to hold the one line data
            String line;
            StringTokenizer st;
            int i=0;

            // Read file line by line and print on the console
            while ((line = bufferReader.readLine()) != null)   {
                st = new StringTokenizer(line, "\t");
                st.nextToken();
                st.nextToken();
                String getMake = st.nextToken();
                st.nextToken();
                int getYear = Integer.parseInt(st.nextToken());
                GetCarInfo temp;
                temp = new GetCarInfo(getMake, getYear);
                CarList.add(temp);
            }
            bufferReader.close();



                      BubbleSortCars Sorted = new BubbleSortCars();
                         Sorted.bubblesorted(CarList, 0, CarList.size());


        }
        catch (Exception e) 
        { 
             e.printStackTrace();
        }

    }

}



import java.util.ArrayList;


public class BubbleSortCars {
    ArrayList <GetCarInfo> temp= new ArrayList();


    public void bubblesorted(ArrayList <GetCarInfo> grabber, int began, int end){

        for(int i =0; i<end-began-1; i++){
            for(int j=began; j<(end-i-1); j++){
                if(grabber.get(j).year > grabber.get(j+1).year){
                    temp.set(j, grabber.get(j));
                    grabber.set(j,grabber.get(j+1));
                    grabber.set(j+1, temp.get(j));
                    System.out.println("Success"); 
                } 
                else if(grabber.get(j).year==grabber.get(j+1).year){
                    if((grabber.get(j).make).compareTo(grabber.get(j+1).make)>0){
                        temp.set(j, grabber.get(j));
                        grabber.set(j, grabber.get(j+1));
                        grabber.set(j+1, temp.get(j));
                        System.out.println("Success");
                    }
                }
            }
        } 
    }
}




public class GetCarInfo {
    int year;
    String make;

    public GetCarInfo(String newmake, int newyear){
        make = newmake;
        year = newyear;
    }
}

Reason you get IndeOutOfBoundException is due to this line: 您收到IndeOutOfBoundException的原因是由于以下原因:

temp.set(j, grabber.get(j));

and your definition of arrayList. 和你对arrayList的定义。

 ArrayList<GetCarInfo> temp = new ArrayList();

Here you are defining temp as arrayList without any element and you are trying to set an element at 0th location which is not defined. 在这里,您将temp定义为没有任何元素的arrayList,并且尝试在未定义的第0个位置设置元素。 See this for your reference http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#set%28int,%20E%29 请参阅此作为参考http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#set%28int,%20E%29

When you define temp as above it created with size 0 and jdk does check internally if index that you are trying to access is less than size then only it will allow you to access/set content. 当您按上述方式定义temp时,它将使用大小0创建,并且jdk会在内部检查您尝试访问的索引是否小于size,那么只有它才能允许您访问/设置内容。

So one way to avoid this, you define your temp arrayList in your method like: 因此,一种避免这种情况的方法是,在您的方法中定义您的临时arrayList,例如:

ArrayList<GetCarInfo> temp = new ArrayList(grabber);

Or you could use grabber arrayList to do the sorting without any other data structure. 或者,您可以使用采集器arrayList进行排序而无需任何其他数据结构。

The problem is with the ArrayList temp . 问题出在ArrayList temp上 Please refer the Oracle documentation for the set method. 请参阅Oracle文档以获取set方法。

Here you don't need an ArrayList. 在这里,您不需要ArrayList。 User a simple GetCarInfo object as temp variable. 使用一个简单的GetCarInfo对象作为临时变量。

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

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