Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unchecked warnings for Arraylist

Tags:

java

warnings

Why am I getting these 4 warnings from -Xlint and what should I do about them? I'm just starting in Java, so am likely missing something obvious.

import java.util.*;

class CompareGerbils implements Comparator {
    public int compare(Object o1, Object o2) {
       return ((Gerbil)o2).number() - ((Gerbil)o1).number();
    }
}

class Gerbil {
int gerbilNumber;

Gerbil(int gN) {
    gerbilNumber = gN;
    }

int number() {
    return gerbilNumber;
    }
}

public class lt {
    public static void main(String[] args) { 

    // I'd like to be able to add both ints and strings to list
    ArrayList list = new ArrayList();

    //unchecked call warning:
    list.add(1);  

    //unchecked call warning:
    list.add("b");  

    ArrayList<Gerbil> gerbillist = new ArrayList<Gerbil>();

    for(int i = 0; i < 5; i++) {
        gerbillist.add(new Gerbil(i));
    }

    //unchecked conversion warning
    //unchecked method invocation
    Collections.sort(gerbillist, new CompareGerbils());
  }
}

EDIT: replies so far have answered the Arraylist declaration. How about the sort warnings at the bottom of the code? thanks

like image 275
foosion Avatar asked Feb 28 '11 13:02

foosion


People also ask

What are unchecked warnings?

An unchecked warning tells a programmer that a cast may cause a program to throw an exception somewhere else. Suppressing the warning with @SuppressWarnings("unchecked") tells the compiler that the programmer believes the code to be safe and won't cause unexpected exceptions.

How can I avoid unchecked cast warnings?

If we can't eliminate the “unchecked cast” warning and we're sure that the code provoking the warning is typesafe, we can suppress the warning using the SuppressWarnings(“unchecked”) annotation. When we use the @SuppressWarning(“unchecked”) annotation, we should always put it on the smallest scope possible.

What is unchecked cast in Java?

Unchecked cast means that you are (implicitly or explicitly) casting from a generic type to a nonqualified type or the other way around.

What does unchecked assignment mean?

Unchecked assignment: 'java.util.List' to 'java.util.List<java.lang.String>' It means that you try to assign not type safe object to a type safe variable. If you are make sure that such assignment is type safe, you can disable the warning using @SuppressWarnings annotation, as in the following examples.


2 Answers

You're getting this because you have not defined a data type for the ArrayList list. The only way to add both Strings and Integers in list without getting warnings is by defining it as ArrayList<Object> list - which is what happens here implicitly (line list.add(1); is implicitly converting 1 to new Integer(1) - this is called autoboxing). Also note that if you want to have both Strings and Integers in lists, the sorting method does not really make sense - how are you expecting things to get sorted, alphabetically or numerically?

Edit: Also, it is not considered good practice to declare a concrete type (i.e. ArrayList<Object> list) unless you have very good reasons to do so. It is recommended that you initialise using an interface, i.e. List<Object> list.

So, your code would have to be like this (note the part Comparator<Gerbil> which fixes the warning in Collections.sort):

// I'd like to be able to add both ints and strings to list
List<Object> list = new ArrayList<Object>();

list.add(new Integer(1));  

list.add(new String("b"));  

List<Gerbil> gerbillist = new ArrayList<Gerbil>();

for(int i = 0; i < 5; i++) {
    gerbillist.add(new Gerbil(i));
}

Collections.sort(gerbillist, new Comparator<Gerbil>() {
    public int compare(Gerbil o1, Gerbil o2) {
        int diff = o1.getNumber() - o2.getNumber();
        if (diff > 0)
           return 1;
        else if (diff <0)
           return -1;
        else
           return 0;
    }
});

With respect to the Gerbil class, I suggest you use the form getNumber as a method name rather than number - it's a de facto standard for method names to retrieve the value of a member variable (and, respectively setNumber(int value) for setting it):

class Gerbil {
int gerbilNumber;

Gerbil(int gN) {
    gerbilNumber = gN;
    }

int getNumber() {
    return gerbilNumber;
    }
}
like image 129
Lucas Zamboulis Avatar answered Sep 22 '22 22:09

Lucas Zamboulis


The warning occurs when you are using a non-generic type in a context where a generic is expected. The compile is saying, you might be right, but I cannot check the type for you.

You cna either;

  • Make the type the correct generic.
  • Turn off the warning with @SuppressWarnings
  • Ignore the warning.

EDIT: In this example, you have to pick a super class/interface of the elements in the list.

// I'd like to be able to add both ints and strings to list
List<Object> list = new ArrayList<Object>();

list.add(1);  
list.add("b");  

Instead of Object you could pick Seralizable or Comparable however eneither is likely to be useful. Indeed a List<Object> is rarely useful except in exercises.

The correct way to implement the Comparator is to use comparison. Using - is only valid if you know this cannot possibly overflow. e.g. 2000000000 - -2000000000 < 0 whereas you might expect 2000000000 - -2000000000 > 0

class CompareGerbils implements Comparator<Gerbil> {
    public int compare(Gerbil a, Gerbil b) {
       return a.number() > b.number() ? +1 
            : a.number() < b.number() ? -1 : 0;
    }
}

For testing purposes I suggest trying to sort a List which is not already sorted. A simple way to do this is to use the shuffle() method. This could still be sorted but the large the list, the less likely that is the case.

Collections.shuffle(gerbillis);
like image 45
Peter Lawrey Avatar answered Sep 23 '22 22:09

Peter Lawrey