The Artima Developer Community
Sponsored Link

Java Answers Forum
Arraylist

15 replies on 2 pages. Most recent reply: Mar 12, 2002 7:14 PM by Hoody

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 15 replies on 2 pages [ 1 2 | » ]
Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Arraylist Posted: Mar 8, 2002 8:55 PM
Reply to this message Reply
Advertisement
use import java.util.ArrayList;

Write a notebook
notebook is implemented as a class named Notebook. The idea is can enter notes into it, and can later list all the notes in the notebook. I can also remove notes when I don?t need them anymore. The notebook should be able to hold any number of notes (hint: that means, of course, that you should use an ArrayList to hold the notes).

Create a Note class. A note is just a short line of text, so a String will do to store one note.
The class should have methods as follows
/**
* Insert a new note into the notebook.
*/
void insertNote(Note newNote)
/**
* Remove the note with number ?noteNumber? from the notebook.
* If there is no note with such a number, just return.
*/
void removeNote(int noteNumber)
/**
* List all notes in the notebook with a number associated
* with each note.
*/
void listNotes()
/**
* Return the number of notes currently in the notebook.
*/
int numberOfNotes()

? Implement this class, including at least the methods listed here


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Arraylist Posted: Mar 8, 2002 11:08 PM
Reply to this message Reply
Don't just post your homework assignment! Do some work on it, read the documentation of ArrayList and ask some specific questions if you have problems. While you're at it, check out this excellent web site, which was suggested in another recent post:
http://www.tuxedo.org/~esr/faqs/smart-questions.html

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: Arraylist Posted: Mar 9, 2002 8:43 AM
Reply to this message Reply
my bad... i am working on it now and i have a few questions so when i get all the code done ill post it and ask for help then.

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: Arraylist Posted: Mar 9, 2002 3:40 PM
Reply to this message Reply
aight im trying to make this note book so i could add notes to it ...i made a notebook class and a note class that i write the notes in and then i need to put notes into the notebook. so far i got this much but i still got number the notes and be able to remove notes by number...take a look at what i got so far if you think you can help...
import java.util.ArrayList;
 
/**
 * Notebook is a class that the user can enter notes into and can later list all the notes in the notebook.
 *
*/
 
public class NoteBook
{
    // instance variables - replace the example below with your own
    private ArrayList noteBook;
    private int noteCount = 0;
 
    /**
     * Constructor for objects of class NoteBook
     */
    public NoteBook()
    {
        // initialise instance variables
        noteBook = new ArrayList ();
    }
    /**
     * Add a note to the notebook.
     * @param note the person to add
     */
    public void addPerson(Note newNote) 
	{
        noteBook.add(newNote);
    }
 
    
        
     
    /**
     * List all notes in the notebook with a number associate with each note.
     */
    public void listAll () 
    {
        for (int i = 0; i < noteBook.size(); i++)
        {
            Note  note = (Note)noteBook.get(i);
            System.out.println(noteBook);
        }
    }
}
 
/**
 * Note is a class that enable the user to input a note into the notebook.
 * 
 */  
public class Note
{
    // instance variables - replace the example below with your own
    private int noteCount = 0;
    private int numberOfNotes = 0;
    private String note;
 
    /**
     * Constructor for objects of class Note
     */
    public Note(String n)
    {
       note = n;
    }
   
    
   
    
    /**
     * Return the number of notes currently in the notebook.
     */
    public int numberOfNotes()
    {
       return numberOfNotes;
    }
      
}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Arraylist Posted: Mar 9, 2002 8:35 PM
Reply to this message Reply
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.

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: Arraylist Posted: Mar 10, 2002 9:05 AM
Reply to this message Reply
the only reason i needed a numberofnotes was because i needed to have that as a method to return the numberofnotes that was in the notebook at the time...
thanx for the help i got listall working good now and im working on the remove the note with the notenumber method

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: Arraylist Posted: Mar 10, 2002 7:49 PM
Reply to this message Reply
this is basically an outline of the remove method i have to make.. i am unsure of how to make it so that it will remove the note that the user wants to remove by entering the note number in and it removes that note then lists all without the selected remove.....

/**
* Remove the note with number 'noteNumber' from the notebook.
* If there is no note with such a number, just return.
*/
public void removeNote(int noteNumber)
{
int i = 1;
boolean found = false;
while(i < noteBook.size() && !found)
{
if(newNote == i)

for(int j = i; j < noteBook.size() ; j++)
{

found = true;
}

else if (i == (noteBook.size() - 1) && !((noteNumber==i)))

found = true;



else

i++;
}
}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Arraylist Posted: Mar 10, 2002 8:11 PM
Reply to this message Reply
There you go again looping through the list like a rank amateur instead of using an Iterataror. For shame!

All you need to do is a noteBook.remove(index), since noteBook is a List (or ArrayList) and catch IndexOutOfBoundsException (or check the bounds first -- but then again, you should still catch the exception).

By the way, since it seems pretty likely that items will often be removed from random locations in your list, you would be better off using a LinkedList, probably.

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: Arraylist Posted: Mar 10, 2002 8:39 PM
Reply to this message Reply
yea i just figured that out but when i do that it doesnt remove the int noteNumber i entered it keeps that one and lists that one the person entered

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Arraylist Posted: Mar 10, 2002 9:00 PM
Reply to this message Reply
I don't quite understand what you said, but it may be an off-by-one problem, if you count the first note as number 1, not number 0.

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: Arraylist Posted: Mar 11, 2002 9:29 AM
Reply to this message Reply
alright i got the add, remove, listall methods working good...the only method i cant seem to complet is the number of notes i have to have a method that adds up all the notes in the notebook and returns how many are in the notebook

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: Arraylist Posted: Mar 11, 2002 1:33 PM
Reply to this message Reply
yo matt thanx for the help... i now understand it a lot better and i studied the java class library a little so i understand more about everything...i got the program working good if you wanna take a look here it is...
import java.util.ArrayList;
import java.util.Iterator;
/**
 * Notebook is a class that the user can enter notes into and can later list all the notes in the notebook.
 *
 */
 
public class NoteBook
{
    // instance variables 
    private ArrayList noteBook;
    private int numberOfNotes = 0;
 
    /**
     * Constructor for objects of class NoteBook
     */
    public NoteBook()
    {
        // initialise instance variables
        noteBook = new ArrayList ();
    }
    /**
     * Insert a note to the notebook.
     * @param newNote the note to add
     */
    public void insertNote(Note newNote) 
    {
        noteBook.add(newNote);
    }
 
     /**
     * Remove the note with number 'noteNumber' from the notebook.
     * If there is no note with such a number, just return.
     */
    public void removeNote(int noteNumber)
    {    for( int i = 0; i < noteBook.size(); i++) 
            if(noteNumber < noteBook.size())
            {
            noteBook.remove(noteNumber);
            }
    } 
    
     
    /**
     * Return the number of notes currently in the notebook.
     */
    public int numberOfNotes()
    {
       
        for (int i = 0; i < noteBook.size(); i++)
        {
           numberOfNotes = noteBook.size() ;
        }
           
        return numberOfNotes;
    }
      
 
    /**
     * List all notes in the notebook with a number associate with each note.
     */
    
    public void listAll()    
    {        
        java.util.Iterator it = noteBook.iterator();        
        for( int i = 1; it.hasNext(); i++ )        
        {            
             Note note = (Note)it.next();           
             System.out.println( i + ". " + note);       
        }  
        
    }
    
    
}

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: Arraylist Posted: Mar 11, 2002 4:21 PM
Reply to this message Reply
it was working good then all of a sudden it stopped...does anyone no why when i add a note and goto listall() it prints this

Note number 1: Note@fea5261c

i dont know the error all my code looks good and its the same as the above posts

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Arraylist Posted: Mar 11, 2002 4:35 PM
Reply to this message Reply
This is probably because your Note object needs to define the toString() method. Either that, or you can call some method in your Note object which returns the text of the note.

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: Arraylist Posted: Mar 11, 2002 5:12 PM
Reply to this message Reply
yea that was it i took it out cause i thought i didnt need it at first thanx

Flat View: This topic has 15 replies on 2 pages [ 1  2 | » ]
Topic: please help me Previous Topic   Next Topic Topic: Icon vertical position in a JTextPane

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use