我如何获得价值11和15,因为缺少值12,13,14.我得到了snippet from this SO post:
我无法将它用于没有固定大小的数组。
int binaryValue[] = null;
ArrayList<Integer> al;
Vector father = new Vector();
Vector child;
int start = al.get(0);
for(int i = 0; i < al.size(); i++) {
if(al.get(i) != i + start) {
child = new Vector();
child.add(al.get(i-1));
child.add(al.get(i));
father.add(child);
}
}
Child should have 11 and 15 and so on ....
答案 0 :(得分:0)
这是基于纯逻辑的问题,你只需要寻找下一个元素来找到差距,见下面的工作样本:
public class GapFinder {
private static int[] myIntArr = {9, 10, 11, 12, 13, 15};
public static void main(String[] args) {
int a = 0,b = 0;
for (int i = 0; i < myIntArr.length; i++) {
if(((i + 1) < myIntArr.length) && myIntArr[i] != myIntArr[i + 1] - 1) {
a = myIntArr[i];
b = myIntArr[i + 1];
break;
}
}
if(a == 0 && b == 0){
System.out.println("There were no gaps.");
} else{
System.out.println("Gaps: " + a + " | " + b);
}
}
}
请注意: