[英]Compare Java List Vectors
我有兩個列表(Matches 和 Motifpoisitions)。 我想按以下方式組合列表:對於所有 i 使得 i%2 = 1,查找 Motifpoistions(k) = Matches(i) +1。 然后設置 Matches(i)=Motifpostions(k+1)。 此外,如果找不到 k,我想刪除 i 和 i-1。
例如,如果匹配項為 (1,3,10,12) 且 Motifpositions 為 (4,8),則最終向量應為 (1,8)。 由於 4 = 3+1,我用 8 替換了 3。並且由於 Motifpositions 中沒有等於 12+1 的值,因此刪除 10 和 12。這是我目前正在嘗試的
for( int i = 1; i < matches.size(); i = i+2){
int motifmatched = 0;
for(int k = 0; k <motifpositions.size(); k++){
if(motifpositions.get(k) == matches.get(i)+1){
matches(i) = motifpositions(k+1);
motifmatched ++;
}
}
if(motifmatched ==0){
matches.remove(i);
matches.remove(i-1);
}
}
不知道你為什么使用嵌套循環。
試試這個代碼,它根據要求打印[1, 8]
:-
List<Integer> matches = new LinkedList<>();
matches.add(1);
matches.add(3);
matches.add(10);
matches.add(12);
List<Integer> motifPositions = new LinkedList<>();
motifPositions.add(4);
motifPositions.add(8);
for (int i = 1; i < matches.size(); i += 2) {
int indexOfSumInMotifpositions = motifPositions.indexOf(matches.get(i) + 1);
if (indexOfSumInMotifpositions > -1) {
matches.set(i, motifPositions.get(indexOfSumInMotifpositions + 1));
} else {
matches.remove(i);
matches.remove(i - 1);
}
}
System.out.println(matches);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.