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;
}
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;
}
這是我的方法,它只是遍歷列表中的每個元素並遞增值,它在80%的時間內運行良好。但是,如果1是「元素」,它會將其增加一倍(有時也是這樣)。遇到Eclipse中的「bug」或某些邏輯我不明白
我已經給下面的一些例子:
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));
輸出是: [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));
輸出爲:[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));
輸出是:[5, 19, 10, 5, 10, 10, 7, 12, 15, 11, 18, 15, 24, 24]
有些數字會觸發此事發生,有些則不會。所以我再次感謝任何反饋。
好最後一點,也是 –
是你確定它會得到兩次索引?我認爲它是相當於1遞增10到11,然後當它在列表indexOf(11)中達到11時將再次返回第一個索引,因爲該元素現在是11。 –
我知道,該分配要求創建通用類,但在這種特定情況下,我們必須制定一個方法,除了列表中的整數外,其他方法基本上都沒有意義。因此,瘋狂的鑄造。 – AfroYak