数组列表与链接列表

时间:2015-07-14 20:27:43

标签: java

所以我的任务是分配LinkedListsArrayLists来完成两项不同的工作。

第1部分) 随机选择列表中的位置,并为每个列表类型将这些值递增1

第2部分) 通过添加随机位置将每个列表大小加倍,然后立即删除相同数量的随机位置,将列表缩小到原始大小。

我觉得我已经很好地完成了这项工作,但我的老师说第1部分ArrayList应该更快(这在我的代码中)。他说LinkedList在第2部分应该更快,这对我来说恰恰相反......它的速度慢......我在SYSO's添加了不同的位置以验证列表是正确地修改了一切,并且无法弄清楚为什么它没有像他说的那样运作。

任何人都可以发现我做错了什么(如果有的话)。非常感谢你

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;

public class LinkedListVersusArrayList {

public static void main(String[] args) {

    long startTime, endTime, duration;
    List<Double> LL = new LinkedList<Double>();
    ArrayList<Double> AL = new ArrayList<Double>();
    int size = Integer.parseInt(JOptionPane.showInputDialog("Pick a list size (whole number only please)"));

    //fills the Linked List with random doubles
    for (int i = 0; i < size; i++){
        LL.add(Math.random());
    }
    //fills the ArrayList with random doubles
    for (int i = 0; i < size; i++){
        AL.add(Math.random());
    }

    //
    //Part 1
    //
    System.out.println("\nPART 1:\nBoth lists are now full of random numbers. \nI will now cycle through "
            + "and incremiment random locations " +size+ " times for each list.\n");

    //testing the LinkedList first for random access
    startTime = System.nanoTime();
    for (int i = 0; i < size; i++){
        int x = (int)(LL.size()*Math.random());
        double y = LL.get(x);
        LL.set(x, y+1);
    }
    endTime = System.nanoTime();
    duration = (endTime - startTime);
    System.out.println("Linked List took: " +(duration/1000000) +" milli seconds");

    //testing the ArrayList now for random access
    startTime = System.nanoTime();
    for (int i = 0; i < size; i++){
        int x = (int)(AL.size()*Math.random());
        double y = AL.get(x);
        AL.set(x, y+1);
    }
    endTime = System.nanoTime();
    duration = (endTime - startTime);
    System.out.println("Array List took: " +(duration/1000000) +" milli seconds");

    //
    //Part 2
    //
    System.out.println("\nPART 2:\nBoth lists will now get "+size+" slots added to them in random locations.\n"
            + "After this is complete, we will remove "+size+" slots from each list at random\n");

    //testing the LinkedList first for random adding/subtracting
    startTime = System.nanoTime();
    //add
    for (int i = 0; i < size; i++){
        int x = (int)(LL.size()*Math.random());
        LL.add(x, 1.0);
    }
    //delete
    for (int i = 0; i < size; i++){
        int x = (int)(LL.size()*Math.random());
        LL.remove(x);
    }
    endTime = System.nanoTime();
    duration = (endTime - startTime);
    System.out.println("Linked List took: " +(duration/1000000) +" milli seconds");

    //testing the ArrayList now for random adding/subtracting
    startTime = System.nanoTime();
    //add
    for (int i = 0; i < size; i++){
        int x = (int)(AL.size()*Math.random());
        AL.add(x, 1.0);
    }
    //delete
    for (int i = 0; i < size; i++){
        int x = (int)(AL.size()*Math.random());
        AL.remove(x);
    }
    endTime = System.nanoTime();
    duration = (endTime - startTime);
    System.out.println("Array List took: " +(duration/1000000) +" milli seconds");
}

}

0 个答案:

没有答案