Tristan Cunningham
Tristan Cunningham

Reputation: 919

Java - having issues with ArrayLists

enter image description here

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. What could be causing this (Or java lists are different from other languages).

Upvotes: 1

Views: 731

Answers (2)

duffymo
duffymo

Reputation: 308733

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
}

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285415

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.

A better solution is to declare your list variable to be a generic List<StaffMember>.

public class StaffList {    
    private List<StaffMember> list;

Upvotes: 10

Related Questions