开发者

Unchecked warnings for Arraylist

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开发者_如何学Go(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


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


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


public static void main(String[] args) {
    final List<Object> list = new ArrayList<Object>();
    list.add(1);
    list.add("b");

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

   Collections.sort(gerbillist, new CompareGerbils());
}


  1. you need to know Generics http://download.oracle.com/javase/1.5.0/docs/guide/language/generics.html

  2. you are trying to add primitive and a string in arraylist. either u do this to take anything

List<Object> list = new ArrayList<Object>();

or if you want to allow only strings

 List<String> list = new ArrayList<String>();  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜