1

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};
}

}

1
  • 1
    private static Integer[] getTwoMissingNumbers(List<**Integer**> list)
    – fukanchik
    Commented Feb 3, 2016 at 5:42

2 Answers 2

3

Change your method parameter from List list to List<Integer> list:

private static Integer[] getTwoMissingNumbers(List<Integer> list) {
...
}

Because when you pass List<Integer> as List, there is a narrowing, and Java lost information about type of list.

1
  • Could you please help me now to get the value printed i.e, the missing numbers returned from the function ?
    – mindfreak
    Commented Feb 3, 2016 at 6:09
1

The issue is with the getTwoMissingNumbers method in this method you have taken the raw type of List which forces to use type as object. While iterating you can take the element of the list as object and type cast it to int type while using. Check the below sample code:

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 (Object num : list) {//use as object
        sum = sum + (int)num;//type cast the object in int format
        squaredSum = squaredSum + ((int)num * (int)num);//type cast the object in int format
    }

    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 };
}

you can even change the parmater type of the method to take the required type as:

private static Integer[] getTwoMissingNumbers(List<Integer> list) {
.....
.....
}
4
  • how do I get the value printed i.e, the missing numbers returned from the function ?
    – mindfreak
    Commented Feb 3, 2016 at 6:05
  • That is already being printed in your code. using the index positions. However you can also use loops there as well to do so. Commented Feb 3, 2016 at 6:17
  • No doubt, the answer is being printed, but I wish to get them printed only once. How to achieve that ?
    – mindfreak
    Commented Feb 3, 2016 at 6:37
  • you can add a check if the resulting array from the getTwoMissingNumbers method contains both the numbers which are removed. Print it and break the loop. Commented Feb 3, 2016 at 6:47

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.