0

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

4

2 回答 2

1
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.

于 2013-10-04T13:53:10.840 回答
0

k类型ListOfLists是你自己写的,不扩展任何东西。如果你没有写一个add方法,你就不能调用它。如果您想要一个还具有其他属性的列表,请尝试ArrayList在您的ListOfLists类中扩展。

于 2013-10-04T13:53:47.667 回答