简体   繁体   English

从另一个通用类中创建的ArrayList中打印单个项目?

[英]Printing Single Item from ArrayList Created in Another Generic Class?

Trying to get an understanding of generics in Java. 试图了解Java中的泛型。 I've created an ArrayList using non-generic methods and casting to print them out, everything works fine using the usual array.get(index) but when I use generics to create the list, the get method doesn't work. 我已经使用非泛型方法创建了一个ArrayList并进行了强制转换以将其打印出来,使用通常的array.get(index)一切都很好,但是当我使用泛型来创建列表时,get方法不起作用。 I can print the whole list just fine, but I want to pick out individual elements. 我可以很好地打印整个列表,但是我想挑选单个元素。 I've tried a few different things which have made the code a little messy probably, so sorry about that. 我尝试了一些不同的操作,这些操作可能会使代码有些混乱,因此感到抱歉。 I know things could probably be done an easier way, I'm just trying to learn about Generics in Java and I ran into this issue. 我知道事情可能可以通过更简单的方式来完成,我只是想学习Java中的泛型,因此遇到了这个问题。 I've watched several different videos and found several examples, so I may be trying to put two concepts together the incorrect way. 我看了几个不同的视频并找到了几个示例,所以我可能试图以错误的方式将两个概念放在一起。 Tried researching, can't find the answer. 尝试研究,找不到答案。

First class Generic.java is to create the generic ArrayList. 第一类Generic.java是创建通用ArrayList。

import java.util.ArrayList;
import java.util.List;

public class Generic<T> {

private ArrayList<T> data;

    public Generic() {
        data = new ArrayList<T>();
    }

    public void add(T element){
        data.add(element);
    }

    //Not even sure if I need these get/set but I put them in there for testing
    public ArrayList<T> getData() {return data;}

    public void setData(ArrayList<T> data) {this.data = data;}

    public String toString(){
        return data.toString();
    }
}

Second class is the main test class: 第二类是主要的测试类:

import java.util.ArrayList;
import java.util.List;


public class Test {

    public static void main(String[] args) {

        List nonGenericList = new ArrayList();
        nonGenericList.add("Enzo");
        nonGenericList.add(458);
        String nonGen1 = (String) nonGenericList.get(0);
        Integer nonGen2 = (Integer) nonGenericList.get(1);

        System.out.println(nonGen1);
        System.out.println(nonGen2);


        Generic<Object> genericList = new Generic<>();
        genericList.add("Enzo");
        genericList.add(458);

        //This is where the .get(0) isn't working for me
        System.out.println(genericList.get(0));
    }
}

Thanks for any help! 谢谢你的帮助!

Your Generic<T> class doesn't have the method get(int index) , you should create the method if you want to call it... 您的Generic<T>类没有方法get(int index) ,如果要调用该方法,则应创建该方法...

Try adding this to your Generic<T> class 尝试将其添加到您的Generic<T>类中

public T get(int index){
    return data.get(index);
}

您可以在泛型类中定义以下get方法:

   public T get(int i) {return data.get(i);}

You do not have add method you have defined the getData() method, so write 您没有定义getData()方法的add方法,因此请编写

ArrayList a=genericList.getData();
System.out.println(a.get(0));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM