[英]How to find the Missing Elements in a given sequence array list using java?`
1.I have an Integer array list with a starting element and arraylist limit. 1.我有一个带有起始元素和arraylist限制的Integer数组列表。 Example [5,6,9,10] 范例[5,6,9,10]
2.In which I have to iterate and find the missing element and its position. 2.我必须在其中迭代并找到缺少的元素及其位置。 According to the above example ,my output should be number 7 (position3 ),number 8 (position 4) are missing. 根据以上示例,我的输出应为7号(位置3),缺少8号(位置4)。
3.Now I am getting all the numbers printed instead of getting the missing elements. 3.现在我正在打印所有数字,而不是缺少元素。
Below is the code : 下面是代码:
public static List<Integer> issue_ret=new ArrayList<>();
Iterator<Integer> iter = issue_ret.iterator();
while(iter.hasNext()){
int value = iter.next();
if("1".equals(value)){
iter.remove();
}
else{
System.out.println("Missing value:"+value);
}
}
Can anyone help me to resolve this? 谁能帮我解决这个问题?
You are comparing every element with 1 您正在将每个元素与1进行比较
if("1".equals(value))
instead you should keep a counter which will start from your lists first element and then incremented by 1 and perform comparison with this counter. 相反,您应该保留一个计数器,该计数器将从列表的第一个元素开始,然后再递增1,然后与该计数器进行比较。
Suggest you a more efficient way than ArrayList.contains()
but more limited: 与ArrayList.contains()
相比,建议您一种更有效的方法,但要限制得多:
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(new Integer[]{5, 6, 9, 10}));
int head = list.get(0);
int tail = list.get(list.size() - 1);
int length = tail - head + 1;
int[] array = new int[length];
for (int i : list) {
array[i - head] = 1;
}
for (int i = 0; i < array.length; i++) {
if (array[i] == 0) {
System.out.println(String.format("Missing %d, position %d", i + head, i + 1));
}
}
The limit is: The top Integer number should not be too large. 限制是: 顶部的整数不应太大。 Anyway, it is a space for time
way, whether to use depends on your actual needs. 无论如何,这是space for time
的space for time
,是否使用取决于您的实际需求。
Try, 尝试,
List<Integer> integerList = new LinkedList<Integer>();
integerList.add(5);
integerList.add(6);
integerList.add(9);
integerList.add(10);
int first = integerList.get(0);
int last = integerList.get(integerList.size()-1);
for(int i=first+1; i<last; i++){
if(!integerList.contains(i))
System.out.println("Number Not in List : "+i);
}
By getting the first and last element from array you can know the start and end of the array and by iteration over that limit you can get what numbers are missing like below: 通过从数组中获取第一个和最后一个元素,您可以知道数组的开始和结束,并通过对该限制进行迭代,可以获取缺少的数字,如下所示:
List<Integer> input = new ArrayList<Integer>();
input.add(5);
input.add(8);
int firstElement = input.get(0);
int lastElement = input.get(input.size()-1);
for(int i=firstElement+1, j=2; i<lastElement-1; i++,j++){
if(!input.contains(i))
System.out.println("Missing Number : "+i + "(position " + j+")");
}
As we already know that first element and last element is already present in last, no need to check that so we would be only checking of elements exist between first and last element. 正如我们已经知道first元素和last元素已经存在于last中一样,不需要检查,因此我们仅检查first和last元素之间是否存在元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.