Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized [duplicate]

Possible Duplicate:
ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized

In order to save an ArrayList with payments done by one member I want to change the List of Payment ID's into a string, so I created the following method:

public String fromArraytoString(ArrayList items){
       JSONObject json = new JSONObject();
        json.put("uniqueArrays", new JSONArray(items));
        return json.toString();
           }

But I get the following warning:

   ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized

Can anyone explain me why?

like image 224
Diego Avatar asked Jan 08 '13 01:01

Diego


People also ask

Is a raw type should be parameterized?

In summary, raw types should NEVER be used in new code. You should always use parameterized types.

Is ArrayList a raw type?

Definition: The raw type is the generic type without any arguments. For example, ArrayList<String> and ArrayList<JFrame> are generic types, while ArrayList is a raw type.

Which of the following is raw type of generic box in Java?

Box rawBox = new Box(); Therefore, Box is the raw type of the generic type Box<T>. However, a non-generic class or interface type is not a raw type. The warning shows that raw types bypass generic type checks, deferring the catch of unsafe code to runtime.

What does raw type mean in Java?

A raw type is a name for a generic interface or class without its type argument: List list = new ArrayList(); // raw type. Instead of: List<Integer> listIntgrs = new ArrayList<>(); // parameterized type. List<Integer> is a parameterized type of interface List<E> while List is a raw type of interface List<E>.


2 Answers

You definitely should read this tutorial on Java generics: http://docs.oracle.com/javase/tutorial/java/generics/

In a nutshell:

Many Java classes and types (called generic classes or generic types), typically collections, have so called type parameters, such as E in ArrayList<E> (E is just an arbitrary chosen name, other classes name it as T or whatever):

public class ArrayList<E> extends ... {

    public E get(int index) { ... }

    public boolean add(E element) { ... }

    // other methods...
}

Now, when you create an instance of such class, you define a concrete value of the type parameter, for example String (E can usually be evaluated to whatever type you want):

ArrayList<String> stringList = new ArrayList<String>();

From now on, all the Es are "replaced" by String for the stringList variable, so you can add only Strings to it and get only Strings from it. The compiler checks for you that you don't mistakenly add an object of another type:

stringList.add(Integer.valueOf(1));
// compile error - cannot add Integer to ArrayList of Strings

However, because generics were added to Java 5, it is still possible to write code without them for backwards compatibility. So you can write:

ArrayList list = new ArrayList();

But you lose all the type checking benefits. Es in method signatures become simply Objects.

list.add(Integer.valueOf(42)); // adding an Integer
list.add("aaa"); // adding a String

Object something = list.get(0); // unknown type of returned object, need to cast
Integer i0 = (Integer) something; // this unsafe cast works...
Integer i1 = (Integer) list.get(1); // but this fails with a ClassCastException
// because you cannot cast a String to Integer

The fact that using a raw type (that is a generic type with its type parameters omitted) is unsafe, is the reason for the warning you've got. Instead of just ArrayList, use ArrayList<String> or ArrayList<Integer> or whatever the type of your items is.

like image 84
Natix Avatar answered Oct 16 '22 04:10

Natix


What kind of objects are stored in the ArrayList? You need to add it to the declaration. It's always

ArrayList<Type>

So if it's a list of JSONObjects, you would put

ArrayList<JSONObject>

Hope that helps.

like image 4
Marc Chambers Avatar answered Oct 16 '22 03:10

Marc Chambers