Results 1 to 7 of 7
- 10-21-2010, 04:11 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
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.....
Any advice (not looking for handouts here...)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. */ }
-
Where do you use ArrayList in the method or return an ArrayList from the method? What is your actual assignment?
- 10-21-2010, 04:57 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
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?
- 10-21-2010, 06:52 AM #4
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.
- 10-21-2010, 06:59 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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
- 10-21-2010, 05:04 PM #6
- 10-21-2010, 07:08 PM #7
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
Similar Threads
-
Question about arraylist
By Metastar in forum New To JavaReplies: 7Last Post: 10-01-2010, 11:53 PM -
ArrayList question
By spatel14 in forum New To JavaReplies: 4Last Post: 07-07-2010, 10:02 PM -
Calling a method when using an arraylist?
By Jamison5213 in forum New To JavaReplies: 10Last Post: 01-23-2010, 08:47 PM -
Beginner question about ArrayList
By kesi in forum New To JavaReplies: 3Last Post: 09-19-2009, 11:30 PM -
arraylist question
By lisa.lipsky in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-16-2009, 11:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks