The program is to find missing numbers from a ArrayList. However, it cannot output the result because of the below error:
Type mismatch:
cannot convert from element type Object to int
It occurs when the List is being iterated inside the called method. Typecasting seems to be of no help. How to get it corrected ?
import java.util.ArrayList;
import java.util.List;
public class FindMisingNumbersInAList {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 100; ++i){
list.add(i);
list.remove(new Integer(12));
list.remove(new Integer(79));
Integer[]missing = getTwoMissingNumbers(list);
System.out.println(missing[0]+ "," +missing[1]);
}
}
private static Integer[] getTwoMissingNumbers(List list) {
int n = list.size()+2;
int expectedSum = n*(n+1)/2;
int expectedSquaredSum = n*(n+1)*(2*n+1)/6;
int sum = 0;
int squaredSum = 0;
System.out.println("SIZE :::" +list.size());
for(int num : list)
{
sum = sum + num;
squaredSum = squaredSum + (num*num); }
int xplusy = expectedSum-sum;
int xsquareplusysquare = expectedSquaredSum-squaredSum;
int twoxy = xplusy*xplusy-xsquareplusysquare;
int xminusy = (int)Math.sqrt(xsquareplusysquare-twoxy);
int x = (xplusy+xminusy)/2;
int y = (xplusy-xminusy)/2;
return new Integer[]{x,y};
}
}
private static Integer[] getTwoMissingNumbers(List<**Integer**> list)