Results 1 to 11 of 11
  1. #1
    kian_hong2000 is offline Member
    Join Date
    Aug 2008
    Posts
    15
    Rep Power
    0

    Default Reading API for ArrayList

    I have this class from my notes:

    import java.util.*;

    public class SongCollection
    {
    private String colName;
    private ArrayList songNames;

    public SongCollection(String colName)
    {
    this.colName = colName;
    songNames = new ArrayList();
    }

    public String getColName()
    {
    return colName;
    }

    public void setColName(String colName)
    {
    this.colName = colName;
    }

    public ArrayList getSongNames()
    {
    return songNames;
    }

    public void addSong(String song)
    {
    songNames.add(song);
    }

    public boolean hasSongName(String song)
    {
    return (songNames.contains(song));
    }

    public boolean removeSongName(String song)
    {
    return (songNames.remove(song));
    }
    public String toString()
    {
    return ("Song collection: " + colName);
    }
    }

    public static void main(String[] args)
    {
    SongCollection2 popCollection = new SongCollection2("Collection");
    popCollection.addSong("Classic");
    popCollection.addSong("Dance");
    popCollection.addSong("Music");
    popCollection.addSong("Jazz");
    System.out.println(popCollection.getSongNames());
    System.out.println(popCollection.removeSongName("D ance"));
    System.out.println(popCollection.getSongNames());
    }

    It use the ArrayList class. I am wondering from the methods "songNames.remove()", "songNames.add{}", "songName.contain()", why the API does not state that it can grab the String input but this program can run and perform without any error?

    From the API

    boolean add(E e)
    Appends the specified element to the end of this list.
    void add(int index, E element)
    Inserts the specified element at the specified position in this list.

    E remove(int index)
    Removes the element at the specified position in this list.
    boolean remove(Object o)
    Removes the first occurrence of the specified element from this list, if it is present.

    boolean contains(Object o)
    Returns true if this list contains the specified element.
    Last edited by kian_hong2000; 08-22-2008 at 03:05 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    Sorry I don't understand your post/question.
    why the API does not state that it can grab the String input but this program can run and perform without any error?
    What does "grab the String input" mean? What statement in your program does it? Why do you think there should be an error?

  3. #3
    Rooneyz is offline Member
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Sorry I don't understand your post/question.

    What does "grab the String input" mean? What statement in your program does it? Why do you think there should be an error?
    I think kianhong2000 asking why his class is working. Per example:

    public boolean removeSongName(String song)
    {
    return (songNames.remove(song));
    }

    You should make clear what kind of objects your arraylist will contain. This must be done at the declaration and at the instantiation. This solves the compiler-warning you probably are getting containing " unsafe expressions".

    For your answer you might want to take a look at extends/inheritance
    Last edited by Rooneyz; 08-21-2008 at 04:23 PM. Reason: added a few thing

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    Sorry again.
    How is the code example you posted supposed to relate to the question?
    the remove method takes an object and returns a boolean
    that is what your code shows.
    Where is the expected error?
    Please explain.

  5. #5
    kian_hong2000 is offline Member
    Join Date
    Aug 2008
    Posts
    15
    Rep Power
    0

    Default

    You means it take an object? I thought it take in a String in the method()

    Correct me if i am wrong
    I have created an Object named popCollection then i add the String and expand the popCollection. Next, I pass the String to getSongNames(). It returns an array of all the collection.

    Now I pass in a String to removeSongName(). Inside this method, it will pass to the
    the Array Object which take in String variable. I thought the API for remove() does not have a String argument which means it is not allowed to take in a String.

    This is what i understand from it. I am new to Java.

    public boolean removeSongName(String song)
    {
    return (songNames.remove(song));
    }

    E remove(int index)
    Removes the element at the specified position in this list.
    boolean remove(Object o)
    Removes the first occurrence of the specified element from this list, if it is present.
    Last edited by kian_hong2000; 08-21-2008 at 05:47 PM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    Does your program compile and execute OK?
    Do you get the expected results?

    The API doc will tell you what the remove() method does.

  7. #7
    kian_hong2000 is offline Member
    Join Date
    Aug 2008
    Posts
    15
    Rep Power
    0

    Default

    Yes. It works. The API indeed tells what its does. But the data type is different as stated in the ArrayList Class. There are only two methods. One takes in int and the other take in an Object. Is this refer to overloading method?

    E remove(int index)
    Removes the element at the specified position in this list.
    boolean remove(Object o)
    Removes the first occurrence of the specified element from this list, if it is present.


    Result:
    [Classic, Dance, Music, Jazz]
    true
    [Classic, Music, Jazz]
    Last edited by kian_hong2000; 08-22-2008 at 03:04 AM.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    Is this refer to overloading method?
    I'm not good on textbook definitions, but I believe having the same named method with different args is called overloading.

  9. #9
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Norm View Post
    I believe having the same named method with different args is called overloading.
    Function overloading can be either:

    1) same name, different number of arguments
    2) same name, same arguments in subclass.

    So any class that wants to print something useful with toString() is called, needs to overload the toString() function. Which means you have to implement it in your class, and overload the base implementation in the super class. Since all Java classes are derived from Object, you have to overload the default toString() implementation. Or you get something generally useless like an "@" with the address of the object

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    2) same name, same arguments in subclass.
    Then what is overriding?

  11. #11
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Norm View Post
    Then what is overriding?
    There a difference?

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  2. ArrayList
    By ramitmehra123 in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 12:47 AM
  3. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 08:48 PM
  4. New to arraylist
    By kleave in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 06:45 PM
  5. Help ArrayList.add()
    By eNine in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 01:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •