1

I seem to be having some problems witn my quicksort method. I am trying to sort an ArrayList of objects using my quicksort method. I am using the Apache POI library to extract data from an excel file and I am adding this data to my arraylist. I have confirmed that my arraylist is not empty by printing out the arraylist before applying the quicksort method.

My problem seems to be that after passing in my arraylist of objects it gets reset to null and the size of the arraylist becomes 0. I got a java IndexOutOfBounds Exception within my quicksort method while trying get an object from the arraylist. Any help would be appreciated, thanks !

Here is my main class :

EDIT I solved my problem using the given solution below, but I still don't understand why my quicksort function doesn't work. I would appreciate it if someone could look at the quicksort function and tell me where I am going wrong. Thanks !

public class Test {


private static ArrayList<Object> incom = new ArrayList<Object>();


private static int period;
private static String termination = "yes";
private static int pivotVal;

private static ArrayList<String> treatment_name = new ArrayList();
private static ArrayList<Integer> treatment_cstart = new ArrayList();
private static ArrayList<Integer> treatment_cend = new ArrayList();
private static ArrayList<Integer> treatment_cost = new ArrayList();
private static ArrayList<Integer> bridge_part = new ArrayList();
private static ArrayList<Integer> budget = new ArrayList();

private static Scanner input = new Scanner(System.in);
private static Scanner alt = new Scanner(System.in).useDelimiter("\n");

public static void main(String[] args) 
{   

    processFile();


}

public static void processFile(){
    try {
        POIFSFileSystem fs      =
            new POIFSFileSystem(new FileInputStream("Book2.xls"));
        HSSFWorkbook wb = new HSSFWorkbook(fs);

        HSSFSheet sheet =wb.getSheet("Table0");
        RowProcessor ip = IncomeProcessor.getInstance();
        Object [] incomes = ip.process(sheet);

        for (int i=0; i<incomes.length; i++)
          incom.add(incomes[i]);

        for (int i=0; i<incom.size(); i++)
        {
            Income income = (Income)incom.get(i);
            System.out.println(income.getBridgeID() + " " + income.getDeckState());
        }


          incom = quicksort(incom);

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}



public static ArrayList<Object> quicksort(ArrayList<Object> income){

    int pivot = income.size()/2;
    int samePivotVal = 0;
    ArrayList<Object> greater = new ArrayList<Object>();
    ArrayList<Object> lesser = new ArrayList<Object>();

    Income pivotIncome = (Income) income.get(pivot);
    pivotVal = pivotIncome.getDeckState();

    Income in;
    for(int i=0; i<income.size() ;i++){
        in = (Income)income.get(i);
        if(in.getDeckState() > pivotVal)
            greater.add(in);
        else if(in.getDeckState() < pivotVal)
            lesser.add(in);
        else 
            samePivotVal++;
    }


    lesser = quicksort(lesser);
    for(int i=0; i<samePivotVal; i++)
        lesser.add(pivotIncome);

    greater = quicksort(greater);

    ArrayList<Object> sorted = new ArrayList<Object>();

    for(Object result : lesser)
        sorted.add(result);

    for(Object result : greater)
        sorted.add(result);

    return sorted;
    }



}

It will be a lot easier (and better) if you use Java's builtin sorting methods.

public static void processFile(){ 
    //...

    Collections.sort(incom, new IncomComparator());
    //...
}


class IncomComparator implements Comparator<Object> {
    @Override
    public int compare(Object o1, Object o2) {
        /* compare logic goes here
           return a negative number when o1 < o2
                  a positive number when o1 > o2
                  0 when o1 == o2
        */ 
        return 0;
    }

}

or in a more simple way

public static void processFile(){ 
    //...

    Collections.sort(incom, new Comparator<Object>() {
        @Override
        public int compare(Object o1, Object o2) {
           /* compare logic goes here
               return a negative number when o1 < o2
                  a positive number when o1 > o2
                  0 when o1 == o2
           */ 
           return 0;
        }
    });
    //...
}

You can see some more examples here.

4

1 回答 1

6

如果您使用 Java 的内置排序方法,将会更容易(并且更好)。

public static void processFile(){ 
    //...

    Collections.sort(incom, new IncomComparator());
    //...
}


class IncomComparator implements Comparator<Object> {
    @Override
    public int compare(Object o1, Object o2) {
        /* compare logic goes here
           return a negative number when o1 < o2
                  a positive number when o1 > o2
                  0 when o1 == o2
        */ 
        return 0;
    }

}

或者以更简单的方式

public static void processFile(){ 
    //...

    Collections.sort(incom, new Comparator<Object>() {
        @Override
        public int compare(Object o1, Object o2) {
           /* compare logic goes here
               return a negative number when o1 < o2
                  a positive number when o1 > o2
                  0 when o1 == o2
           */ 
           return 0;
        }
    });
    //...
}

您可以在此处查看更多示例。

于 2012-07-29T01:13:36.333 回答