将整数列表分配到字符串列表中 [英] Assigning List of Integer Into a List of String

查看:111
本文介绍了将整数列表分配到字符串列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Java中的泛型,并且接近了一段非常有趣的代码。我知道在Java中,将一种类型的列表添加到另一种类型是非法的。

  List< Integer> integerList = new ArrayList< Integer>(); 
列表< String>的StringList = integerList;

所以在第二行中,我得到一个编译时错误。

但是如果我在这样的类中创建泛型方法,那么

  class GenericClass< E> {
void genericFunction(List< String> stringList){
stringList.add(foo);
}
//一些其他代码
}

主类调用带Integer列表的方法我没有收到任何错误。

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

GenericClass genericClass = new GenericClass();
列表<整数> integerList = new ArrayList< Integer>();
integerList.add(100);
genericClass.genericFunction(integerList);
System.out.println(integerList.get(0));
System.out.println(integerList.get(1));


$ / code>

输出

100 >
foo



为什么我没有收到任何错误?

解决方案

您将Generic与原始类型混合使用。它可以很好地编译,但是在运行时可能会失败,因为通用信息在运行时会丢失。



在编译时应该使用泛型来追踪这些错误。 什么是原始类型为什么我们不能使用它?详细说明。






警告:类型安全性:方法 genericFunction(List)属于原始类型 GenericClass 。对泛型类型 GenericClass< E> 的引用应该是
参数化的。



如果您有两种方法相同的名称与不同的通用类型的列表,则会导致编译时错误。如果方法参数可以通过下面的示例代码来证明,那么编译器无法解析泛型类型。

示例代码:(编译器错误 - 不是有效的重载方法)

  void genericFunction(List< String> stringList){...} 
void genericFunction(List< Integer> stringList ){...}






进行一些更改并再试一次:

  class GenericClass< E> {
void genericFunction(List< E> stringList){
...
}
//一些其他代码
}

...

GenericClass< String> genericClass = new GenericClass< String>(); // Genreric对象
List< Integer> integerList = new ArrayList< Integer>();
integerList.add(100);
genericClass.genericFunction(integerList); //编译时错误






创建这样的方法方式

  class GenericClass< E> {
私人列表< E> list = new ArrayList< E>();

public void addAll(List< E> newList){
list.addAll(newList);
}

public void add(E e){
list.add(e);
}

public E get(int index){
return list.get(index);
}
//一些其他代码
}


I was learning Generics in Java and I came close to a very interesting piece of code. I know in Java it is illegal to add list of one type to another.

List<Integer> integerList = new ArrayList<Integer>();
List<String> stringList=integerList;

So in the second line I get a compile time error.
But if I create a generic method inside a class like this,

class  GenericClass <E>{
    void genericFunction(List<String> stringList) {
        stringList.add("foo");
    }
    // some other code
}

And in the main class call the method with list of Integer I am not getting any error.

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

     GenericClass genericClass=new GenericClass();
     List<Integer> integerList= new ArrayList<Integer>();
     integerList.add(100);
     genericClass.genericFunction(integerList);
     System.out.println(integerList.get(0));
     System.out.println(integerList.get(1));
  }
}

Output
100
foo

Why I am not getting any error?

解决方案

You have mixed Generic with raw type. It will compile fine but at run-time it might fail because Generic information is lost at run-time.

Generic should be used to track such bugs at compile-time.

It's better explained at What is a raw type and why shouldn't we use it? in detail.


Warning: Type safety: The method genericFunction(List) belongs to the raw type GenericClass. References to generic type GenericClass<E> should be parameterized.

If you have two methods with same name with different Generic type of List then it results into compile time error. The compiler is unable to resolve Generic type in case of method arguments that can be proved by below sample code.

Sample code: (compiler error - not a valid overloaded method)

void genericFunction(List<String> stringList){...}
void genericFunction(List<Integer> stringList){...}


Make some changes and try it again:

class  GenericClass <E>{
    void genericFunction(List<E> stringList) {
        ...
    }
    // some other code
}

...

GenericClass<String> genericClass=new GenericClass<String>(); // Genreric object
List<Integer> integerList= new ArrayList<Integer>();
integerList.add(100);
genericClass.genericFunction(integerList); // compile time error


Create methods in such a way

class GenericClass<E> {
    private List<E> list = new ArrayList<E>();

    public void addAll(List<E> newList) {
        list.addAll(newList);
    }

    public void add(E e) {
        list.add(e);
    }

    public E get(int index) {
        return list.get(index);
    }
    // some other code
}

这篇关于将整数列表分配到字符串列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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