If the statement does not work with two & lt; Long & gt; Lists [Java]

advertisements
long temp;
for ( int i = 0; i < size; i++ )
{
    temp = ls.get(i);
    System.out.println("temp is:" + temp);
    for ( int j = 0; j < size; j++)
    {
            if ( i == j )
            {

            }
            else if ( ls.get(i) == ls.get(j) )
            {
                // If Duplicate, do stuff... Dunno what yet.
                System.out.println(ls.get(i)+" is the same as: " + ls.get(j) );
                //System.out.println("Duplicate");
            }

    }
}

I have a List of type Long, it is filled with several 9 digit numbers like 900876012, I am checking for duplicates so that I can generate new numbers and replace the duplicate before exporting to a file.

First, I check to see if the position in the array is the same, since obviously something at position 1 is going to be the same in the same list at position 1, if so, ignore.

Then, I check to see if the contents are the same, but it doesn't evaluate as true for some reason, it did before as a plain "if" on its own.

For reference here's the numbers, just ignore the "temp is":

temp is:900876512
temp is:765867999
temp is:465979798
temp is:760098908
temp is:529086890
temp is:765867999
temp is:529086890
temp is:800003243
temp is:200900210
temp is:200900210
temp is:542087665
temp is:900876512
temp is:900876512
temp is:900876512
temp is:900876512

Here's the full method for reference:

public static void CheckContents(BufferedReader inFileStreamName, File aFile, Scanner s ) throws DuplicateSerialNumberException
{
    System.out.println();
    List<Long> ls = new ArrayList<Long>();
    List<String> ls2 = new ArrayList<String>();

long SerialNum = 0;
int counter = 0;
int size = 0;
String StringBuffer;

while (s.hasNextLine())
{
    ls.add(s.nextLong());
    //System.out.println();
    StringBuffer = s.nextLine();
    ls2.add(StringBuffer);
    size = ls.size();
    //System.out.println(ls.size());
    SerialNum = ls.get(size-1);
    //System.out.println(ls.get(size-1));
    System.out.println("Serial # is: " + SerialNum);
    //System.out.println(SerialNum + ": " + StringBuffer);
    counter++;
}

long temp;
for ( int i = 0; i < size; i++ )
{
    temp = ls.get(i);
    System.out.println("temp is:" + temp);
    for ( int j = 0; j < size; j++)
    {
        if ( i == j )
        {

        }
        else if ( ls.get(i) == ls.get(j) )
        {
            // If Duplicate, do stuff... Dunno what yet.
            System.out.println(ls.get(i)+" is the same as: " + ls.get(j) );
            //System.out.println("Duplicate");
        }

    }
}

}


A nice one.

In the list you don't store float (primitive) but Float objects. Autoboxing makes you transparent to you.

And the == comparator does not work with objects (it tells if both objects are the same, but if you have two object holding the same value it returns false).

You can use

   if ( ls.get(j).equals(ls.get(i))

or (since it is List<Long>)

   if ( ls.get(j).longValue() == ls.get(i).longValue())

or even (thanks to autoboxing)

   if ( ls.get(j).longValue() == ls.get(i))