The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
ArrayList Remove Method
Discuss ArrayList Remove Method in the Java Help forum on Dev Shed. ArrayList Remove Method Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 28th, 2012, 10:47 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 38
Time spent in forums: 6 h 46 m 22 sec
Reputation Power: 1
|
|
|
ArrayList Remove Method
I am trying to write a remove method for an ArrayList class. I cannot use any built-in Java collections. I have been trying for at least a few days trying to figure this algorithm out, but nothing is working.
Here is what I have so far...
Code:
//Remove the element at the given index and returns it.
public E remove(int index)
{
E result = get(index);
for (int x = index; x < count - 1; x++)
{
data_store[count - 2] = data_store[count - 1] ;
}
count --;
return result;
}
Thanks.
|

October 29th, 2012, 08:18 AM
|
 |
Java Junkie
|
|
Join Date: Jan 2004
Location: Mobile, Alabama
|
|
|
You could create a temporary ArrayList, copy everything except the element into it, and then rearrange the name of the list.
|

October 29th, 2012, 10:22 AM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 38
Time spent in forums: 6 h 46 m 22 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by bullet You could create a temporary ArrayList, copy everything except the element into it, and then rearrange the name of the list. |
Thank you.
I tried this.
[/CODE] public E remove(int index)
{
E result = data_store[index];
E newArray[] = null;
E temp = newArray[index];
for (int x = index; x < count - 1; x++)
{
data_store[ count -2 ] = data_store[ count - 1] ;
}
temp = data_store[count];
count--;
return result;
}[/CODE]
Is this what you meant? Thanks again.
|

October 29th, 2012, 01:06 PM
|
 |
Java Junkie
|
|
Join Date: Jan 2004
Location: Mobile, Alabama
|
|
|
In what class is the remove method?
|

October 29th, 2012, 02:57 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 38
Time spent in forums: 6 h 46 m 22 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by bullet In what class is the remove method? |
Code:
public class MyArrayList<E> implements MyListInterface<E>
All code must be in the MyArrayList class.
The one method I am having trouble with is the remove method.
|

October 29th, 2012, 06:53 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 38
Time spent in forums: 6 h 46 m 22 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by anonymousme
Code:
public class MyArrayList<E> implements MyListInterface<E>
All code must be in the MyArrayList class.
The one method I am having trouble with is the remove method. |
I figured it out. Thanks for your help.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|