小编Dom*_*que的帖子

使用int数组的HashSet用法

因为我有一个包含重复项的int数组的ArrayList,所以我想使用HashSet.不幸的是,我无法按照自己的意愿使用HashSet:

System.out.print("\nTESTs\n");
    ArrayList<int[]> list = new ArrayList<int[]>();
    list.add(new int[]{1,2,3});
    list.add(new int[]{5,1,1});
    list.add(new int[]{1,2,3});//duplicate
    list.add(new int[]{5,1,3});

    Set<int[]> set = new HashSet<int[]>(list);
    System.out.println("Size of the set = "+set.size());

    ArrayList<int[]> arrayList = new ArrayList<int[]>(set);
    System.out.println("Size of the arrayList = "+arrayList.size());

    for (int[] array:arrayList){
        System.out.println(Arrays.toString(array));
    }
Run Code Online (Sandbox Code Playgroud)

它导致:

Size of the set = 4
Size of the arrayList = 4
[1, 2, 3]
[1, 2, 3] // duplicate still here
[5, 1, 1]
[5, 1, 3]
Run Code Online (Sandbox Code Playgroud)

有谁能告诉我我哪里错了?

在此先感谢Dominique(java新手)

java hashset

4
推荐指数
2
解决办法
4473
查看次数

Pop() 在理解列表中使用,不会从初始列表中删除所有项目

我有一个列表,想通过一些计算创建一个新列表。在做的过程中,我遇到了这样的问题:

为什么 pop() 不立即从列表中删除所有项目?

>>> a= list(range(20))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> [a.pop() for elem in a]
[19, 18, 17, 16, 15, 14, 13, 12, 11, 10]
>>> [a.pop() for elem in a]
[9, 8, 7, 6, 5]
>>> [a.pop() for elem in a]
[4, 3, 2]
>>> [a.pop() for elem in a]
[1]
Run Code Online (Sandbox Code Playgroud)

我期望的是

>>> a= list(range(20))
>>> a
[0, 1, …
Run Code Online (Sandbox Code Playgroud)

python list

4
推荐指数
1
解决办法
537
查看次数

使用Apache Maths进行多项式回归(Java)

任何人都可以帮我用Apache Math库进行多项式回归(2阶).

以下数据应该给出这个等式:39.79 x ^ 2 - 497.66 x + 997.45(由Excel计算,r2 = 0.9998)

// coding style from http://commons.apache.org/proper/commons-math/userguide/fitting.html    

double[] y = { 540.0, 160.0, -140.0, -360.0, -480.0, -560.0, -540.0, -440.0, -260.0, 0.0, 340.0};              
        final WeightedObservedPoints obs = new WeightedObservedPoints();
        for (double figure:y){
            obs.add(1.0, figure);
        }
        final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2);
        final double[] coeff = fitter.fit(obs.toList());
        System.out.println("coef="+Arrays.toString(coeff));
Run Code Online (Sandbox Code Playgroud)

以下是前一代码提供的回归系数:

coef=[-53.73522460839947, -52.22329678670934, -52.22329678670934]
Run Code Online (Sandbox Code Playgroud)

显然,我有些遗漏......

谢谢你的帮助

大教堂

java apache regression

3
推荐指数
1
解决办法
3197
查看次数

标签 统计

java ×2

apache ×1

hashset ×1

list ×1

python ×1

regression ×1