Results 1 to 7 of 7
  1. #1
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default add method from Arraylist - question

    Good evening everyone. I was doing a homework assignment, and the assignment is to use the "add method" from the ArrayList class. I have to basically come up with my own code to make the "add" method work (to add an element into an ArrayList and return the list with the added element)

    here is my code, and the professors comments. Any advice on how to do what he asks me to do? this should be simple.....

    Java Code:
    public void add(Object o)
     {
    	Object [ ] old = a;
        a = (Object []) new Object[ a.length * 2 + 1 ];
    
        /****************************************
        You do not need to cast a.
        */
        for( int i = 0; i < size( ); i++ )
            a[ i ] = old[ i ];
    
            /********************************
            You have not added a.
            */
     }
    Any advice (not looking for handouts here...)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Where do you use ArrayList in the method or return an ArrayList from the method? What is your actual assignment?

  3. #3
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    Assignment says....

    (Implementing ArrayList) ArrayList is implemented in the Java API. Implement ArrayList and the methods defined using your own program.

    His notes on my code says that I am not adding 'a' to it. Should I return
    a[ i ] = old[ i + a ]; as the last line of the code then?

    Does that make more sense?

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Notice what you are doing. Remember that size() is the CURRENT size of the array. Let's say theoretically that your list is {5,4,3,2} (even though it's an object list--just pretend ;)). Now, you want to add the number 8. So let's walk through your loop:

    size() == 4, so our loop goes from i = 0 to i = 3:
    i = 0: a[0] = 5
    i = 1: a[1] = 4
    i = 2: a[2] = 3
    i = 3: a[3] = 2

    After this is done, you don't add 8 anywhere. You have to do something like, a[size()] = o, or something similar (just throwing code around to get the ideas going).

    EDIT: The above is actually incorrect relative to your assignment. See JosAH's post below as to why.

    Additionally I don't know why you multiplied a's length by 2 first. You're just adding an element. You only need a[old.length + 1].

    And casting... yeah, that's self explanatory. (Object[]) is not necessary.
    Last edited by Zack; 10-21-2010 at 05:05 PM.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    I think you're mixing up 'a' as being the element to be added and the buffer that contains all the elements in the collection ...

    kind regards,

    Jos

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    I think you're mixing up 'a' as being the element to be added and the buffer that contains all the elements in the collection ...

    kind regards,

    Jos
    Actually you're right. I misread. Instead the "old" should be modified. I was mislead by the (Object o) parameter.

  7. #7
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    You guys are the best. I have that portioned solved. Thanks again for all your help!

Similar Threads

  1. Question about arraylist
    By Metastar in forum New To Java
    Replies: 7
    Last Post: 10-01-2010, 11:53 PM
  2. ArrayList question
    By spatel14 in forum New To Java
    Replies: 4
    Last Post: 07-07-2010, 10:02 PM
  3. Calling a method when using an arraylist?
    By Jamison5213 in forum New To Java
    Replies: 10
    Last Post: 01-23-2010, 08:47 PM
  4. Beginner question about ArrayList
    By kesi in forum New To Java
    Replies: 3
    Last Post: 09-19-2009, 11:30 PM
  5. arraylist question
    By lisa.lipsky in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-16-2009, 11:07 AM

Posting Permissions

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