#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    3

    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.
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    You could create a temporary ArrayList, copy everything except the element into it, and then rearrange the name of the list.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    3
    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.
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    In what class is the remove method?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    3
    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.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    3
    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.

IMN logo majestic logo threadwatch logo seochat tools logo