简体   繁体   English

遇到Eclipse中的“错误”或某些我不理解的逻辑

[英]Encountered either of 'bug' in Eclipse or some logic I don't understand

public SortedArrayList<T> incrementItems(int increment) {
for(T sh: this) {
    Integer newValue = (Integer) sh + (Integer) increment;
    this.set(this.indexOf(sh), (T) newValue);
    }
return this;
}

This is my method, it simply goes through every element of the list and increments the value, it runs fine 80% of the time. 这是我的方法,它只遍历列表的每个元素并增加值,它在80%的时间内运行良好。 However, if 1 is the "element" it increments it by double (again only sometimes). 但是,如果1是“元素”,则它将增加两倍(仅在某些时候再次出现)。

I've given some examples below: 我在下面给出了一些示例:

    SortedArrayList<Integer> L1 = new SortedArrayList<Integer>();
    L1.addAll(Arrays.asList(1,3,6,1,6,6,7,8,11,11,14,15,20,20));
    System.out.println(L1.incrementItems(10));

Output is: [21, 13, 16, 21, 16, 16, 17, 18, 11, 11, 24, 25, 30, 30] 输出为: [21, 13, 16, 21, 16, 16, 17, 18, 11, 11, 24, 25, 30, 30]

    SortedArrayList<Integer> L1 = new SortedArrayList<Integer>();
    L1.addAll(Arrays.asList(1,3,6,1,6,6,7,8,11,11,14,15,20,20));
    System.out.println(L1.incrementItems(9));

Output is: [10, 12, 24, 10, 15, 15, 16, 17, 29, 29, 23, 15, 20, 20] 输出为: [10, 12, 24, 10, 15, 15, 16, 17, 29, 29, 23, 15, 20, 20]

    SortedArrayList<Integer> L1 = new SortedArrayList<Integer>();
    L1.addAll(Arrays.asList(1,3,6,1,6,6,7,8,11,11,14,15,20,20));
    System.out.println(L1.incrementItems(4));

Output is: [5, 19, 10, 5, 10, 10, 7, 12, 15, 11, 18, 15, 24, 24] 输出为: [5, 19, 10, 5, 10, 10, 7, 12, 15, 11, 18, 15, 24, 24]

Some numbers trigger this to happen, others don't. 一些数字触发了这种情况的发生,另一些则没有。 So again I'd appreciate any feedback. 因此,再次感谢您的任何反馈。

This has nothing to do with the Eclipse IDE. 这与Eclipse IDE无关。

You are invoking indexOf on your List , which will retrieve the first element matching. 您正在List上调用indexOf ,它将检索第一个匹配的元素。

Every time. 每次。

See docs : 参见文档

Returns the index of the first occurrence of the specified element in this list[...] 返回此列表中指定元素的首次出现的索引[...]

So if you are looping and 1 comes across twice, indexOf will return the first position of 1 , and the element will be incremented. 因此,如果循环播放,并且1遇到两次, indexOf将返回1的第一个位置,并且该元素将递增。

What happens next is based on the further items in your List : if a match is found for the incremented item later in your iteration, the same item will be incremeneted again while the latter occurrences will be left untouched. 接下来发生的情况取决于List的其他项:如果在迭代的后期找到与增量项匹配的项,则将再次扩大同一项的权重,而后一种情况将保持不变。

As an off-topic issue, you seem to be misusing generics here: your SortedArrayList class accepts any kind of type parameters, yet its incrementItems only assumes values will be Integer . 作为一个离题的问题,您似乎在这里滥用了泛型: SortedArrayList类接受任何类型的类型参数,但其incrementItems仅假定值将为Integer

Note 注意

If you're using Java 8, you can leverage the map functionality of streams to easily project all your List elements to their incremented value. 如果您使用的是Java 8,则可以利用流的map功能轻松地将所有List元素投影到其增量值。

For instance: 例如:

// this will generate a new `List` with incremented elements
List<Integer> incremented = originalList
    .stream()
    .map(x -> Integer.sum(x, increment))
    .collect(Collectors.toList());

If you are stuck with pre-Java 8 idioms, you can create a new List with the code such as: 如果您不熟悉Java 8之前的习惯用法,则可以使用以下代码创建新的List

List<Integer> incremented = new ArrayList<>();
for (Integer i: originalList) {
    incremented.add(i + increment);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM