Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: Arraylist
|
Posted: Mar 9, 2002 8:35 PM
|
|
A few things: You don't need the noteCount variable, since you can get that information from the List.
This is a style point, but it is a little more flexible to use the List interface as the type of your variable, if you don't need any specific capabilities of the ArrayList (it allows you to very easily change to some other type of List, such as LinkedList, should the need arise -- for instance, if you are often adding and removing notes in the middle of the list, a LinkedList will be much more suitable). That is, instead of private ArrayList noteBook;
you can have private List noteBook = new ArrayList();
Why is the method that is adding a Note called addPerson()?
I would use an Iterator in the listAll() method, it is more flexible and faster:
public void listAll()
{
java.util.Iterator it = noteBook.iterator();
while( it.hasNext() )
{
Note note = (Note)it.next();
System.out.println(note);
}
}
If you want it also to display a count, as your (?) comment for the method indicated, then it might be something like this:
public void listAll()
{
java.util.Iterator it = noteBook.iterator();
for( int i = 1; it.hasNext(); i++ )
{
Note note = (Note)it.next();
System.out.println("Note number " + i + ": " + note);
}
}
Finally, your Note object should represent a single note, so it doesn't need noteCount, numberOfNotes or numberOfNotes() method. That is all NoteBook-type information. Also, having a variable and a method with the same name probably isn't a good idea.
|
|