警告ArrayList是一种原始类型。对泛型类型ArrayList的引用< E>应该参数化 [英] Warning ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized

查看:2169
本文介绍了警告ArrayList是一种原始类型。对泛型类型ArrayList的引用< E>应该参数化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

ArrayList是一种原始类型。对泛型类型ArrayList的引用< E>应该进行参数化


为了保存一个ArrayList,其中一个成员完成付款我想将支付ID列表变成一个字符串,所以我创建了以下方法:

  public String fromArraytoString(ArrayList items ){
JSONObject json = new JSONObject();
json.put(uniqueArrays,新的JSONArray(items));
返回json.toString();
}

但是我收到以下警告:

  ArrayList是一个原始类型。对泛型类型ArrayList的引用< E>应该参数化

任何人都可以解释为什么吗?

解决方案

您绝对应该阅读关于Java泛型的本教程
http://docs.oracle.com/javase/tutorial/java/generics/



在简而言之:许多Java类和类型(称为泛型类泛型类型), 通常是集合,在 ArrayList< E> 中具有所谓的类型参数,例如 E E 只是一个任意选择的名称,其他类名称为 T 或其他):

  public class ArrayList< E> (...)
$ b public E get b $ b //其他方法...
}

现在,当您创建您可以定义类型参数的具体值,例如 String ( E )通常可以被评估无论你想要什么类型):

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

从现在起,所有 E s被 String 替换为 stringList 变量,所以您只能添加字符串并从中只获取字符串它。编译器会检查你是否错误地添加了另一个类型的对象:

  stringList.add(Integer.valueOf( 1)); 
//编译错误 - 无法将整数添加到String的ArrayList






但是,由于泛型被添加到Java 5,为了向后兼容,仍然可以在没有它们的情况下编写代码。所以你可以这样写:

  ArrayList list = new ArrayList(); 

但是你失去了所有类型检查的好处。方法签名中的 E s简单地变为 Object s。

  list.add(Integer.valueOf(42)); //添加一个整数
list.add(aaa); //添加一个字符串

Object something = list.get(0); //未知类型的返回对象,需要转换
Integer i0 =(Integer); //这个不安全的转换工作...
Integer i1 =(Integer)list.get(1); //但是由于ClassCastException
//因为不能将字符串转换为整数

使用 raw type (这是一个省略了类型参数的泛型类型)是不安全的,这是您得到警告的原因。而不是 ArrayList ,使用 ArrayList< String> ArrayList< Integer> 或任何类型的项目都是。


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?

解决方案

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.

这篇关于警告ArrayList是一种原始类型。对泛型类型ArrayList的引用&lt; E&gt;应该参数化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆