Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: ArrayList remove() implementation problem?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default ArrayList remove() implementation problem?

    Hey guys I wanted to know if someone could tell me what I'm doing wrong here. I need to implement the methods of an ArrayList, and I was doing great until I had to implement the remove() method.

    The problem is that the exercise tells me that after I remove an element from the list and move all the elements after the removed element one space to the left, I also need to check how many null elements there are in the array. If these null elements exceed 10, then I need to make a new array with smaller capacity and move all the elements from the previous array into the new, smaller one (for memory saving issues). Here are the exact words from my manual

    Our approach will be as follows: the initial capacity of the internal array is fixed to
    5. If a new element is to be added to the list, but that capacity has been exhausted, then the array
    capacity will be increased by 5. However, the array must be shrunk whenever there are more than 10
    positions available. In that case, the capacity is reduced by 5. (These numbers must be planned
    carefully on a real implementation.)
    My remove method works just fine in everything else except this part where I have to shrink the array.

    What I did was to create a variable counter, and loop through the whole array. Whenever an element was null, increase the counter. Afterwards, if the counter exceeds 10, then I use a pre-made method named changeCapacity() which does the part of creating a new smaller array and moving all the elements from the previous array into this new one. But for some reason, this implementation does not seem to work. I get lots of NullPointerExceptions.

    Could anyone check my code and tell me what I am doing wrong?

    Method:
        public E remove(int index) throws IndexOutOfBoundsException 
        {
            if(index < 0 || index > this.size)
            {
                throw new IndexOutOfBoundsException("Invalid index: " + index);
            }
     
     
            E temp = this.element[index];
            this.element[this.size- 1] = null;
            this.moveDataOnePositionTL(index + 1 ,this.size() - 1);
            this.size--;
     
     
            int counter = 0;
            for (int i = 0; i <= this.element.length - 1; i++)
            {
            	if(this.element[i].equals(null))
            	{
            		counter++;
            	}
            }
     
            if(counter >= 10)
            {
            	this.changeCapacity(-5);
            }
     
     
            return temp;
        }

    Also, here are the moveDataOnePositionTL and changeCapacity methods:

    // useful when removing an element from the list...
        private void moveDataOnePositionTL(int low, int sup) 
        { 
            // pre: 0 < low <= sup <= (element.length - 1)
            for (int pos = low; pos <= sup; pos++)
                element[pos-1] = element[pos]; 
        }

           private void changeCapacity(int change) 
        { 
            int newCapacity = element.length + change; 
            E[] newElement = (E[]) new Object[newCapacity]; 
            for (int i=0; i<size; i++) 
            { 
                newElement[i] = element[i]; 
                element[i] = null; 
            } 
            element = newElement; 
        }
    Thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: ArrayList remove() implementation problem?

    Can you make a small complete program the compiles, executes and shows the problem for testing?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how to remove items of an arraylist
    By crossit in forum Java Theory & Questions
    Replies: 7
    Last Post: December 18th, 2012, 01:14 PM
  2. ArrayList implementation
    By meijuh in forum Collections and Generics
    Replies: 8
    Last Post: March 22nd, 2012, 02:03 PM
  3. [SOLVED] Looping through ArrayList to remove elements
    By itispj in forum Collections and Generics
    Replies: 3
    Last Post: October 14th, 2011, 01:42 AM
  4. Implementation of remove method in linkedlist Iterator
    By jay2you in forum Collections and Generics
    Replies: 3
    Last Post: October 12th, 2011, 10:16 AM
  5. not able to remove from ArrayList
    By harsha_c in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 3rd, 2011, 03:28 AM