我需要一个逻辑上的帮助..我有一个结果,
此结果集中的有一列名为“Number”
我需要发现其中没有数字,
示例:
Number
10
11
12
15
20
21
25
我这样做:将实际值作为A,执行result.next()
,然后检查实际的value == (A+1)
当然这不起作用, - ',因为行计数= dabatase中的数据而不是基于数字序列
有人有一个提示让它显示跳跃的NUMBERS?13
14
16
17
18
19
22
....直到最后一个号码!!
我通过以下方式:
while(results.next()){
a = previoslyresults("number");
if(actualresult("number") != (a + 1)) {
for(i = 1; i< (actualresults("number") - (a +1));i++) {
syso("are missing the " (actualresults("number") + i);
}
}
}
答案 0 :(得分:0)
你可以在arrayList中添加所有数字,并手动检查下限和上限,并且可以通过循环检查值来轻松获取不在结果集中的数字。
答案 1 :(得分:0)
这是我想要的解决方案,我已经包含了一个阵列版本来测试它:
class ExpectationChecker
{
private final boolean[] results;
private final int min;
public ExpectationChecker(final int min, final int max)
{
this.results = new boolean[max - min + 1];
this.min = min;
}
public Integer[] getMissingNumbers(final ResultSet results) throws SQLException
{
while(results.next())
{
//Offset by min
this.results[results.getInt(1) - this.min] = true;
}
final List<Integer> missingNums = new ArrayList<>();
for(int i = 0; i < this.results.length; i++)
{
if(!this.results[i])
{
//Add min in case min isn't 0
missingNums.add(i + this.min);
}
}
return missingNums.toArray(new Integer[missingNums.size()]);
}
public Integer[] getMissingNumbers(final int[] results)
{
for(int i = 0; i < results.length; i++)
{
//Offset by min
this.results[results[i] - this.min] = true;
}
final List<Integer> missingNums = new ArrayList<>();
for(int i = 0; i < this.results.length; i++)
{
if(!this.results[i])
{
//Add min in case min isn't 0
missingNums.add(i + this.min);
}
}
return missingNums.toArray(new Integer[missingNums.size()]);
}
public static void main(String[] args)
{
final ExpectationChecker ec = new ExpectationChecker(10, 20);
final int[] test = {11, 12, 13, 14, 17, 19};
System.out.println(Arrays.deepToString(ec.getMissingNumbers(test)));
}
}
产生输出:
[10, 15, 16, 18, 20]