【问题标题】:how to add a new linkedlist element into arraylist如何将新的linkedlist元素添加到arraylist
【发布时间】:2014-04-23 14:09:26
【问题描述】:

我是 Kava 的新手,我很难理解我做错了什么,以及为什么我的代码不起作用。

我正在尝试使用给定的双精度数组 (double[] ar) 来实现桶排序, 并使用linkedLists的arrayList。

这是我的代码:

        int n = ar.length; 

    //initializing the buckets 
    ArrayList<LinkedList<Double>> buckets = new ArrayList<LinkedList<Double>>(ar.length);

    // Initialize 'n' buckets (each is a LinkedList<Double>) (B[0..n-1])
    for (int i = 0; i < n -1; i++) {
        buckets.add(i, new LinkedList<Double>());
    }

    for (int i = 0; i < n; i++) {
    buckets.add((int)(n*ar[i]), ar[i]);
    }

它 在“添加”上显示错误,它说: "ArrayList> 类型中的方法 add(int, LinkedList) 不适用于参数 (int, double)”

谢谢。

【问题讨论】:

    标签: linked-list


    【解决方案1】:

    ArrayList&lt;LinkedList&lt;Double&gt;&gt; buckets[] 是你的 ArrayList OF LinkedList Of DOUBLE 的数组。

    当你这样做时,你会在你的 ArrayList 中添加一个 DOUBLE:

    buckets[(int)(n * ar[i])].add((Double)ar[i]);
    

    考虑更改您的存储桶声明

    也许你想要的是

     "ArrayList<Double> buckets[]"
    

    但要小心实例化更改为

    buckets[index] = new ArrayList<Double>();  
    

    【讨论】:

    • 好的我改了代码,请再看一遍,我还是有这种问题
    • for (int i = 0; i ar[i]), ar[i]);这是错误的。 ar[i] 是 double 类型,您将其添加到需要向其添加 LinkedList 的存储桶中。 for (int i = 0; i ar[i])).addLast(ar[i]); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    相关资源
    最近更新 更多