Dan
Dan

Reputation: 43

Array Type Confusion?

This is my code for the class, ListOfLists. The constructor should make an array of type NameList.

public class ListOfLists {
private int capacity;
private NameList[] listOfLists;
private int size = 0;

public ListOfLists(int capacity) {
    listOfLists = new NameList[capacity];
}

My NameList class looks something like this..

public class NameList{
    public NameList(String initial){
    i = initial;
    }
    public void add(String data){
    ...
    }

If I make a new object in the Main of ListOfLists called k..

ListOfLists k = new ListOfLists(5);

How come I cannot do..

k.add("Whatever") ?

I get the error.. The type of the expression must be an array type but it resolved to ListOfLists

Upvotes: 0

Views: 122

Answers (2)

bclymer
bclymer

Reputation: 6771

k which is of type ListOfLists is something you wrote yourself, and doesn't extend anything. If you didn't write an add method, you can't call it. If you want a list that also has other properties, try extending ArrayList in your ListOfLists class.

Upvotes: 0

amicngh
amicngh

Reputation: 7899

How come I cannot do..

because you don't have add method in ListOfLists class.

If you want to use add method of class NameList then get the value of listOfLists which is of type NameList and then add the Whatever.

Upvotes: 1

Related Questions