向ArrayList添加整数会增加-1而不是值

时间:2015-04-10 01:32:29

标签: java arraylist

当我向一个arraylist添加一个整数时,它会添加-1的值而不是349823这样的值。我从一个名为" c"的对象中调用一个计数器。所以我的语法是nameofList.add(c.instructioncounter);

有人知道会出错吗?根据要求,这是我的全班,这是很多。我试图将所有计数器值添加到arraylist,然后将数组传递给一个方法,然后计算标准偏差。

import java.util.ArrayList;
import java.util.Random;

import javax.swing.plaf.synth.SynthSeparatorUI;

public class Comparator extends InstrumentedSorter {

    static int count = 1000;

    static float selectionIC = 0;static float selectionE = 0;static float insertionIC = 0;
    static float insertionE = 0;static float bubbleIC = 0;static float bubbleE = 0;static float quickIC = 0;
    static float quickE = 0;static float rquickIC = 0;static float rquickE = 0;static float mergeIC = 0;static float mergeE = 0;

    static ArrayList<Integer> saIC = new ArrayList<Integer>(); static ArrayList<Float> saE = new ArrayList<Float>();
    static ArrayList<Integer> maIC = new ArrayList<Integer>(); static ArrayList<Float> maE = new ArrayList<Float>();
    static ArrayList<Integer> baIC = new ArrayList<Integer>(); static ArrayList<Float> baE = new ArrayList<Float>();
    static ArrayList<Integer> iaIC = new ArrayList<Integer>(); static ArrayList<Float> iaE = new ArrayList<Float>();
    static ArrayList<Integer> qaIC = new ArrayList<Integer>(); static ArrayList<Float> qaE = new ArrayList<Float>();
    static ArrayList<Integer> raqIC = new ArrayList<Integer>(); static ArrayList<Float> raqE = new ArrayList<Float>();  


    public static void main(String[] args) {

        Comparator c = new Comparator();
        for (int size = 1000; size <= 10000; size += 1000) {
            for (int round = 1; round <= 10; round++) {
                int[] randomArray = c.createRandomArray(size);
                for (int iter = 1; iter <= 20; iter++) {
                    // clone randomArray for selection sort
                    int[] a = randomArray.clone();
                    c.resetInstructionCounter();
                    c.timer.start();
                    c.selectionSort(a);
                    c.timer.end();
                    saIC.add(c.getInstructionCounter());
                    selectionIC += c.instructionCounter;
                    saE.add((float) c.timer.getTotalTime());
                    selectionE += c.timer.getTotalTime();

                    // clone randomArray for bubble sort
                    int[] b = randomArray.clone();
                    c.resetInstructionCounter();
                    c.timer.start();
                    c.bubbleSort(b);
                    c.timer.end();
                    bubbleIC += c.instructionCounter;
                    baIC.add(c.instructionCounter);
                    baE.add((float) c.timer.getTotalTime());
                    bubbleE += c.timer.getTotalTime();

                    // clone randomArray for insertion sort
                    int[] d = randomArray.clone();
                    c.resetInstructionCounter();
                    c.timer.start();
                    c.insertionsort(d);
                    c.timer.end();
                    iaIC.add(c.instructionCounter);
                    insertionIC += c.instructionCounter;
                    iaE.add((float) c.timer.getTotalTime());
                    insertionE += c.timer.getTotalTime();

                    // clone randomArray for quick sort
                    int[] e = randomArray.clone();
                    c.resetInstructionCounter();
                    c.timer.start();
                    c.quickSort(e);
                    c.timer.end();
                    qaIC.add(c.instructionCounter);
                    quickIC += c.instructionCounter;
                    qaE.add((float) c.timer.getTotalTime());
                    quickE += c.timer.getTotalTime();

                    // clone randomArray for randomized quick sort
                    int[] f = randomArray.clone();
                    c.resetInstructionCounter();
                    c.timer.start();
                    c.randomizedQuickSort(f);
                    c.timer.end();
                    raqIC.add(c.instructionCounter);
                    rquickIC += c.instructionCounter;
                    raqE.add((float) c.timer.getTotalTime());
                    rquickE += c.timer.getTotalTime();

                    // clone randomArray for merge sort
                    int[] g = randomArray.clone();
                    c.resetInstructionCounter();
                    c.timer.start();
                    c.mergeSort(g);
                    c.timer.end();
                    maIC.add(c.instructionCounter);
                    mergeIC += c.instructionCounter;
                    maE.add((float) c.timer.getTotalTime());
                    mergeE += c.timer.getTotalTime();

                }
            }

            average(selectionIC);average(bubbleIC);average(rquickIC);average(insertionIC);average(quickIC);average(mergeIC);
            average(selectionE);average(bubbleE);average(rquickE);average(insertionE);average(quickE);average(mergeE);
            print();
            maE.clear();maIC.clear();raqE.clear();raqIC.clear();baE.clear();baIC.clear();iaE.clear();iaIC.clear();qaE.clear();
            qaIC.clear();saE.clear();saIC.clear();

        }

    }
    public static void print(){

        System.out.printf("┌────────────────────┬─────────────────────────────────────────────────────────────────────────────┐\n");
        System.out.printf("│                    │                              Array Size = " +count+"                              │\n");
        System.out.printf("├────────────────────┼──────────────────────────────────────┬──────────────────────────────────────┤\n");
        System.out.printf("│ Sorting Algorithm  │         instruction counter          │               run time               │\n");
        System.out.printf("├────────────────────┼─────────────────┬────────────────────┼─────────────────┬────────────────────┤\n");
        System.out.printf("│   Selection Sort   │%17.2f|%20.2f│%17.2f│%20.2f│\n", selectionIC, sdi(saIC), selectionE, sdl(saE));
        System.out.printf("├────────────────────┼─────────────────┼────────────────────┼─────────────────┼────────────────────┤\n");
        System.out.printf("│    Bubble Sort     │%17.2f│%20.2f│%17.2f│%20.2f│\n", bubbleIC, sdi(baIC), bubbleE, sdl(baE));
        System.out.printf("├────────────────────┼─────────────────┼────────────────────┼─────────────────┼────────────────────┤\n");
        System.out.printf("│     Merge Sort     │%17.2f│%20.2f│%17.2f│%20.2f│\n", mergeIC, sdi(maIC), mergeE, sdl(maE));
        System.out.printf("├────────────────────┼─────────────────┼────────────────────┼─────────────────┼────────────────────┤\n");
        System.out.printf("│   Insertion Sort   │%17.2f│%20.2f│%17.2f│%20.2f│\n", insertionIC, sdi(iaIC), insertionE, sdl(iaE));
        System.out.printf("├────────────────────┼─────────────────┼────────────────────┼─────────────────┼────────────────────┤\n");
        System.out.printf("│     Quick Sort     │%17.2f│%20.2f│%17.2f│%20.2f│\n", quickIC, sdi(qaIC), quickE, sdl(qaE));
        System.out.printf("├────────────────────┼─────────────────┼────────────────────┼─────────────────┼────────────────────┤\n");
        System.out.printf("│ Random Quick Sort  │%17.2f│%20.2f│%17.2f│%20.2f│\n", rquickIC, sdi(raqIC), rquickE, sdl(raqE));
        System.out.printf("└────────────────────┴─────────────────┴────────────────────┴─────────────────┴────────────────────┘\n\n\n");

        count += 1000;
    }
    public static float average (float variable){
        variable = variable/2000;
        return variable;
    }

    public static float sdl (ArrayList<Float> name){
        float total = 0;
        for(int i = 0; i < name.size(); i++) {
            total += name.indexOf(i);
        }
        total = total / name.size();
        float deviation = 0;
        for(int i = 0; i < name.size(); i++){
            deviation += (Math.pow((name.indexOf(i)-total),2));
        }
        deviation /= name.size();
        deviation = (float) Math.pow(deviation, 2);

        return deviation;
    }

    public static float sdi(ArrayList<Integer> name){
        int total = 0;
        for(int i = 0; i < name.size(); i++) {
            System.out.println(name.indexOf(i));
        }
        for(int i = 0; i < name.size(); i++) {
            total += name.indexOf(i);
        }
        total = total / name.size();

        int deviation = 0;
        for(int i = 0; i < name.size(); i++){
            deviation += (Math.pow((name.indexOf(i)-total),2));
        }
        deviation /= name.size();

        deviation = deviation*deviation;

        return (float) deviation;
    }

    public void bubbleSort(int[] array) {
        int temp;
        this.incrementInstructionCounter();
        this.incrementInstructionCounter();
        for (int i = 0; i < array.length; i++) {
            this.incrementInstructionCounter();
            this.incrementInstructionCounter();
            this.incrementInstructionCounter();
            for (int j = i + 1; j < array.length; j++) {
                this.incrementInstructionCounter();
                this.incrementInstructionCounter();
                if (array[i] > array[j]) {
                    this.incrementInstructionCounter();
                    temp = array[i];
                    this.incrementInstructionCounter();
                    array[i] = array[j];
                    this.incrementInstructionCounter();
                    array[j] = temp;
                    this.incrementInstructionCounter();
                }
            }
        }

    }

    public void selectionSort(int[] array) {
        int first, temp;
        this.incrementInstructionCounter();
        this.incrementInstructionCounter();
        this.incrementInstructionCounter();
        for (int i = array.length - 1; i > 0; i--) {
            this.incrementInstructionCounter();
            this.incrementInstructionCounter();
            first = i;
            this.incrementInstructionCounter();
            this.incrementInstructionCounter();
            for (int j = 1; i < i; j++) {
                this.incrementInstructionCounter();
                this.incrementInstructionCounter();
                if (array[j] < array[first]) {
                    this.incrementInstructionCounter();
                    first = j;
                    this.incrementInstructionCounter();
                }
            }
            this.incrementInstructionCounter();
            temp = array[first];
            this.incrementInstructionCounter();
            array[first] = array[i];
            this.incrementInstructionCounter();
            array[i] = temp;
        }

    }

    public void insertionsort(int[] array) {
        int temp;
        this.incrementInstructionCounter();
        this.incrementInstructionCounter();
        for (int i = 1; i < array.length; i++) {
            this.incrementInstructionCounter();
            this.incrementInstructionCounter();
            temp = array[i];
            this.incrementInstructionCounter();
            int j;
            this.incrementInstructionCounter();
            this.incrementInstructionCounter();
            for (j = i - 1; j >= 0 && temp < array[j]; j--) {
                this.incrementInstructionCounter();
                this.incrementInstructionCounter();
                array[j + 1] = array[j];
                this.incrementInstructionCounter();
            }
            array[j + 1] = temp;
            this.incrementInstructionCounter();
        }
    }

    public void mergerSort(int[] array) {
        mergeSort(array);
    }

    public int[] mergeSort(int[] array) {
        if (array.length > 1) {
            this.incrementInstructionCounter();
            int elementsInA1 = array.length / 2;
            this.incrementInstructionCounter();
            this.incrementInstructionCounter();
            int elementsInA2 = elementsInA1;
            this.incrementInstructionCounter();


            if ((array.length % 2) == 1){
                this.incrementInstructionCounter();
                this.incrementInstructionCounter();
                elementsInA2 += 1;
                this.incrementInstructionCounter();
            }
            int arr1[] = new int[elementsInA1];
            this.incrementInstructionCounter();
            int arr2[] = new int[elementsInA2];
            this.incrementInstructionCounter();     
            this.incrementInstructionCounter();
            for (int i = 0; i < elementsInA1; i++) {
                this.incrementInstructionCounter();
                this.incrementInstructionCounter();
                arr1[i] = array[i];
                this.incrementInstructionCounter();
            }
            this.incrementInstructionCounter();
            for (int i = elementsInA1; i < elementsInA1 + elementsInA2; i++){
                this.incrementInstructionCounter();
                this.incrementInstructionCounter();
                arr2[i - elementsInA1] = array[i];
                this.incrementInstructionCounter();
                this.incrementInstructionCounter();
            }
            arr1 = mergeSort(arr1);
            arr2 = mergeSort(arr2);

            int i = 0, j = 0, k = 0;
            this.incrementInstructionCounter();
            this.incrementInstructionCounter();
            this.incrementInstructionCounter();

            while (arr1.length != j && arr2.length != k) {
                this.incrementInstructionCounter();
                this.incrementInstructionCounter();
                if (arr1[j] < arr2[k]) {
                    this.incrementInstructionCounter();
                    array[i] = arr1[j];
                    this.incrementInstructionCounter();
                    i++;
                    this.incrementInstructionCounter();
                    j++;
                    this.incrementInstructionCounter();
                } else {
                    array[i] = arr2[k];
                    this.incrementInstructionCounter();
                    i++;
                    this.incrementInstructionCounter();
                    k++;
                    this.incrementInstructionCounter();
                }
            }
            while (arr1.length != j) {
                this.incrementInstructionCounter();
                array[i] = arr1[j];
                this.incrementInstructionCounter();
                i++;
                this.incrementInstructionCounter();
                j++;
                this.incrementInstructionCounter();
            }
            while (arr2.length != k) {
                this.incrementInstructionCounter();
                array[i] = arr2[k];
                this.incrementInstructionCounter();
                i++;
                this.incrementInstructionCounter();
                k++;
                this.incrementInstructionCounter();
            }
        }
        return array;
    }

    public void quickSort(int[] array) {
        int low = 0;
        this.incrementInstructionCounter();
        int high = array.length - 1;
        this.incrementInstructionCounter();

        int middle = low + (high - low) / 2;
        this.incrementInstructionCounter();
        int pivot = array[middle];
        this.incrementInstructionCounter();
        int i = low;
        this.incrementInstructionCounter();
        int j = high;
        this.incrementInstructionCounter();

        while (i <= j) {
            this.incrementInstructionCounter();
            while (array[i] < pivot) {
                this.incrementInstructionCounter();
                i++;
                this.incrementInstructionCounter();
            }
            while (array[j] > pivot) {
                this.incrementInstructionCounter();
                j--;
                this.incrementInstructionCounter();
            }
            if (i <= j) {
                this.incrementInstructionCounter();
                int temp = array[i];
                this.incrementInstructionCounter();
                array[i] = array[j];
                this.incrementInstructionCounter();
                array[j] = temp;
                this.incrementInstructionCounter();
                i++;
                this.incrementInstructionCounter();
                j--;
                this.incrementInstructionCounter();
            }
        }
    }

    public void randomizedQuickSort(int[] array) {
        int low = 0;
        this.incrementInstructionCounter();
        int high = array.length - 1;
        this.incrementInstructionCounter();

        Random r = new Random();
        this.incrementInstructionCounter();
        int pivot = array[r.nextInt(array.length)];
        this.incrementInstructionCounter();
        int i = low;
        this.incrementInstructionCounter();
        int j = high;
        this.incrementInstructionCounter();

        while (i <= j) {
            this.incrementInstructionCounter();
            while (array[i] < pivot) {
                this.incrementInstructionCounter();
                i++;
                this.incrementInstructionCounter();
            }
            while (array[j] > pivot) {
                this.incrementInstructionCounter();
                j--;
                this.incrementInstructionCounter();
            }
            if (i <= j) {
                this.incrementInstructionCounter();
                int temp = array[i];
                this.incrementInstructionCounter();
                array[i] = array[j];
                this.incrementInstructionCounter();
                array[j] = temp;
                this.incrementInstructionCounter();
                i++;
                this.incrementInstructionCounter();
                j--;
                this.incrementInstructionCounter();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我确定你在ArrayList中插入-1。您应该在插入之前记录该值,以查看-1对您的计数器的影响。

以下是使用ArrayList

的一个小例子

答案 1 :(得分:1)

当您的意思是indexOf时,您正在使用getarrayList.indexOf(5)是列表中5号码的位置,如果不存在,则为-1arrayList.get(5)是索引5处的数字。