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



}

You should use the findInterval function. It will give you the index of the row where x$ts is immediately smaller than the value you are looking for (and you just have to add one to get the other index)

findInterval(1342980889, x$ts)
# [1] 1

Also note that the function is vectorized, i.e., the first argument can be a vector of values to look for:

findInterval(c(1342980889, 1342981483), x$ts)
# [1]  1 10
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 に答える