简体   繁体   English

Java-ArrayLists有问题

[英]Java - having issues with ArrayLists

在此处输入图片说明

As you can see I have a StaffMember class and trying to make a list of StaffMember objects, but when I go to get them out of the list I get errors. 如您所见,我有一个StaffMember类,试图创建一个StaffMember对象列表,但是当我将它们从列表中删除时,我得到了错误。 What could be causing this (Or java lists are different from other languages). 可能是什么原因造成的(或者Java列表与其他语言不同)。

Since you're not using a generic List variable , the compiler has no way of knowing what type of objects the List contains, and so you'll have to cast the object returned by the get(...) method to the type you believe it to be. 由于您没有使用通用的List 变量 ,因此编译器无法知道List包含的对象类型,因此必须将get(...)方法返回的对象转换为您所需要的类型。相信它是。

A better solution is to declare your list variable to be a generic List<StaffMember> . 更好的解决方案是将您的列表变量声明为通用List<StaffMember>

public class StaffList {    
    private List<StaffMember> list;

I think your life will be better if you do it this way: 我认为,如果这样做,您的生活会更好:

public class Staff {
    private List<StaffMember> roster;

    public Staff() {
        this.roster = new ArrayList<StaffMember>();
    }

    // add the rest
}

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

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