Solved

Resizing an array in Java

Posted on 2009-04-30
7
206 Views
Last Modified: 2012-08-14
(Implementing ArrayList)  ArrayList is implemented in the Java API.  Implement ArrayList and the methods defined in table below (Hint use an array to store the elements in ArrayList.  If the size of ArrayList exceepds the capacity of the current array, create a new array that doubles the size of the current array and copy the contents of the current to the new array)
 

+ArrayList()                                 creates an empty list

+add(o: Object):void                    appends a new element o at the end of this list

+add(index: int, o: Object): void       Adds a new element o at the specified index in this list

+clear():void                                    removes all the elements form this list

+contains(o:Object):boolean            returns true if this list contains element o

+get(index:int):Object                       returns the element from this list at the specified index

+indexOf(o:Object):int                     returns the index of the last matching element in this list

+isEmpty():boolean                          returns true if this list contains no elements

+lastIndexOf(o:Object):int                returns the index of the last matching element in this list

+remove(o:Object):boolean              removes the element o from this list

+size():int                                         returns the number of elements in this list

+remove(index:int):boolean                removes the element at the specified index

+set(index:int, o:Object):Object          sets the elements at the specified index


This is the question that I was given: I'm getting an error  when I debug the program. I have to have a main to run it? Where am I suppose to put the main. Also, if there is anything wrong in my code could you please  help fix the issue. Thanks
import java.util.AbstractList;

import java.util.List;

import java.util.RandomAccess;
 
 

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable

     {

       // The capacity of the ArrayList is the length of this array buffer.

        

          private transient Object[] elementData;

	private int modCount;

      

          public ArrayList(int initialCapacity) 

          {

            super();

            if(initialCapacity < 0)

            throw new IllegalArgumentException("Illegal Capacity: "+initialCapacity);

             this.elementData = new Object[initialCapacity];

          }

 

  

     // Constructs an empty list with an initial capacity of 100.

 

        public ArrayList()

          {

            this(100);

          }

        public boolean isEmpty()

           {

              return size() == 0;

          }
 

       public boolean contains(Object o)

        {

          return indexOf(o) >= 0;

        }
 

     public int indexOf(Object o)

      {

         if (o == null)

           {

              for (int i = 0; i < size(); i++)

              if (elementData[i]==null)

               return i;

           }

           else

            {

               for (int i = 0; i < size(); i++)

               if (o.equals(elementData[i]))

               return i;

       }

        return -1;

  }

 

     public int lastIndexOf(Object o)

      {

         if (o == null)

          {

            for (int i = size()-1; i >= 0; i--)

            if (elementData[i]==null)

            return i;

           }

          else

          {

            for (int i = size()-1; i >= 0; i--)

            if (o.equals(elementData[i]))

            return i;

          }

         return -1;

       }
 

       public E remove(int index)

        {

           rangeCheck(index);

           int modCount++;

           E oldValue = elementData(index);

           int numMoved = size() - index - 1;

           if (numMoved > 0)

           System.arraycopy(elementData, index+1, elementData, index,numMoved);

           elementData[--size()] = null;

           return oldValue;

         }
 

    private E elementData(int index) 

    {

		// TODO Auto-generated method stub

		return null;

	}
 
 

	private void rangeCheck(int index) 

	{

		// TODO Auto-generated method stub

		

	}
 
 

	public boolean remove(Object o)

     {

      if (o == null)

       {

         for (int index = 0; index < size(); index++)

         if (elementData[index] == null)

          {

             fastRemove(index);

             return true;

          }

        }

        else

         {

           for (int index = 0; index < size(); index++)

           if (o.equals(elementData[index]))

            {

              fastRemove(index);

              return true;

            }

         }

          return false;

   }
 

  private void fastRemove(int index)

   {

        modCount++;

        int numMoved = size() - index - 1;

        if (numMoved > 0)

       System.arraycopy(elementData, index+1, elementData, index,

       numMoved);

       elementData[--size()] = null;

    }
 

     public void clear()

      {

        modCount++;

        for (int i = 0; i < size(); i++)

        elementData[i] = null;

        size() = 0;

      } 
 

        public int size()

           {

             checkForComodification();

             return this.size();

           }
 
 

		private void checkForComodification() {

			// TODO Auto-generated method stub

			

		}
 
 

	

		public E get(int index) {

			// TODO Auto-generated method stub

			return null;

		}

 

 }

Open in new window

0
Comment
Question by:laughsalot
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24272930
>> I have to have a main to run it?

You do. You can put it inside the class for testing only, but it's better to create a separate class
0
 

Author Comment

by:laughsalot
ID: 24273022
I know I have to implement with a test program, is the code I have so far good to go? I'm working on the test program now. I will post when I am done.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24273140
You need to ensure the correct instance variables. This fixes it so it compiles:
import java.util.AbstractList;

import java.util.List;

import java.util.RandomAccess;

 

 

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable

     {

       // The capacity of the ArrayList is the length of this array buffer.

        

          private transient Object[] elementData;

        private int modCount;

      

          public ArrayList(int initialCapacity) 

          {

            super();

            if(initialCapacity < 0)

            throw new IllegalArgumentException("Illegal Capacity: "+initialCapacity);

             this.elementData = new Object[initialCapacity];

          }

 

  

     // Constructs an empty list with an initial capacity of 100.

 

        public ArrayList()

          {

            this(100);

          }

        public boolean isEmpty()

           {

              return size() == 0;

          }

 

       public boolean contains(Object o)

        {

          return indexOf(o) >= 0;

        }

 

     public int indexOf(Object o)

      {

         if (o == null)

           {

              for (int i = 0; i < size(); i++)

              if (elementData[i]==null)

               return i;

           }

           else

            {

               for (int i = 0; i < size(); i++)

               if (o.equals(elementData[i]))

               return i;

       }

        return -1;

  }

 

     public int lastIndexOf(Object o)

      {

         if (o == null)

          {

            for (int i = size()-1; i >= 0; i--)

            if (elementData[i]==null)

            return i;

           }

          else

          {

            for (int i = size()-1; i >= 0; i--)

            if (o.equals(elementData[i]))

            return i;

          }

         return -1;

       }

 

       public E remove(int index)

        {

           rangeCheck(index);

           int modCount++;

           E oldValue = elementData(index);

           int numMoved = size() - index - 1;

           if (numMoved > 0)

           System.arraycopy(elementData, index+1, elementData, index,numMoved);

           elementData[--size()] = null;

           return oldValue;

         }

 

    private E elementData(int index) 

    {

                // TODO Auto-generated method stub

                return null;

        }

 

 

        private void rangeCheck(int index) 

        {

                // TODO Auto-generated method stub

                

        }

 

 

        public boolean remove(Object o)

     {

      if (o == null)

       {

         for (int index = 0; index < size(); index++)

         if (elementData[index] == null)

          {

             fastRemove(index);

             return true;

          }

        }

        else

         {

           for (int index = 0; index < size(); index++)

           if (o.equals(elementData[index]))

            {

              fastRemove(index);

              return true;

            }

         }

          return false;

   }

 

  private void fastRemove(int index)

   {

        modCount++;

        int numMoved = size() - index - 1;

        if (numMoved > 0)

       System.arraycopy(elementData, index+1, elementData, index,

       numMoved);

       elementData[--size()] = null;

    }

 

     public void clear()

      {

        modCount++;

        for (int i = 0; i < size(); i++)

        elementData[i] = null;

        size() = 0;

      } 

 

        public int size()

           {

             checkForComodification();

             return this.size();

           }

 

 

                private void checkForComodification() {

                        // TODO Auto-generated method stub

                        

                }

 

 

        

                public E get(int index) {

                        // TODO Auto-generated method stub

                        return null;

                }

 

 }

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Assisted Solution

by:objects
objects earned 20 total points
ID: 24274600
you can put the main in any class you want, I'd suggest creating a separate test class and put your test main in there

0
 
LVL 92

Expert Comment

by:objects
ID: 24274611
for resizing an array see the following:
http://helpdesk.objects.com.au/java/how-do-i-resize-a-java-array

0
 

Accepted Solution

by:
laughsalot earned 0 total points
ID: 24274731
thanks guys I got it up and running
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24352426
laughsalot, i'm curious as to why you accepted an answer that's essentially the same as my earlier one..?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 77
Python Assistance 7 73
Eclipse IDE - Cannot copy/paste from console output 8 132
Where to store the queries for modification of table 4 55
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now