我需要使用 HashSet 返回包含负值和正值的值,同时保持顺序。这就是我所拥有的,但我不断收到错误消息。任何帮助都是极好的。
public static double[] negated(double[] a)
{
Set<Double> x = new HashSet<Double>();
for (int i = 0; i < a.length; i++)
{
x.add((Double)a[i]);
}
for(double e : x)
{
if (x.contains(-e) == false)
{
x.remove(e);
}
}
List<Double> list = new ArrayList<Double>(x);
double[] b = new double[list.size()];
for (int i = 0; i < a.length; i++)
{
if (list.contains(a[i]))
{
b[i] = list.get(i);
}
}
return b;
}
public static void main(String[] args)
{
double[] c = {3, 4, 5, 6, -7, -5, -3, 9};
System.out.println(negated(c));
}
这是错误:
xception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:894)
at java.util.HashMap$KeyIterator.next(HashMap.java:928)
at Ass10.negated(Ass10.java:144)
at Ass10.main(Ass10.java:182)