Results 1 to 4 of 4
  1. #1
    RozenKristal is offline Member
    Join Date
    Sep 2012
    Posts
    10
    Rep Power
    0

    Default Need help with add method for array.

    BIG NOTICE: I making a dynamic array that apply the same principle using arraylist, I AM NOT ALLOWED TO USE ARRAYLIST.

    So here is my code so far:
    Java Code:
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Random;
     
    class DynamicArrayOfInts 
    {
     
        private int[] storage;
        private int size;
        private final  int INITIAL_CAPACITY = 8;
        private final int GROW_FACTOR = 2;
       
      
        public DynamicArrayOfInts() 
        {
          storage = new int[INITIAL_CAPACITY]; 
          size = 0;
        }
        
        //check for out of bound exception
        private void rangeCheck(int index)
        { 
        	if((index < 0)||(index == size)||(index > size))
        	{
          	  throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        	}
        	else
        	{
        		System.out.println("Array is inbound");
        	}
        }  
        
        //make sure array grow as there are more values to be added
        private void ensureCapacity(int size_wanted)
        {
          int max_capacity = storage.length;
          if (size_wanted > max_capacity) 
          {
            max_capacity = max_capacity * GROW_FACTOR +1; 
            storage = Arrays.copyOf(storage, max_capacity); // increases array size + copy contents
          } 
        }
        
        //get size of the array
        public int size() 
        {   
        	size = storage.length;
        	return size; // added so code would compile
        }
        
        //compare aThat array with the current array
    	public boolean equals(Object aThat) 
    	{ // aThat is a DynamicArrayOfInts object
    		//check if aThat is empty or not instance of Dynamic
    		if((aThat==null)||!(aThat instanceof DynamicArrayOfInts))
    			return false;
    		DynamicArrayOfInts aThatLocal = (DynamicArrayOfInts) aThat;
    		//compare length
    		if(storage.length != aThatLocal.size)
    			return false;
    		//compare elements
    		for (int i = 0;i<storage.length;i++)
    		{
    			if(storage[i]!=aThatLocal.get(i))
    			{
    				return false;
    			}			
    		}
    		return true;		
        } 
        
    	//compare list with current array
        public boolean equals(List<Integer> list)  
        { // list is a LinkedList, or ArrayList, etc
        	//check size
        	if(list.size()!=storage.length)
        		return false; 
        	//compare elements
        	for (int i = 0;i<storage.length;i++)
        	{
        		if(storage[i] != list.get(i))
        		{
        			return false;
        		}
        	}
        	return true;
        	
        }
        
        //get value at the specific index
        public int get(int position)
        {
        	rangeCheck(position);    	
        	return storage[position];  
        }
        
        //set value at specific index
        public void set(int index, int value)
        {
        	rangeCheck(index);
        	storage[index]= value;    	
        }
        
        //insert value at specific index and shift everything from that index to the right
        public void insertAt(int index, int value) 
        {
        	if ((index<0)||(index>size))
        		throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        	else
        	{
        		int theLength = storage.length;
        		int temp=0;
        		int j=theLength;
        		ensureCapacity(size+1);
        		for(int i = (theLength-1);i != index;i--)
        		{
        			temp = storage[i];
        			storage[j] = temp;
        			j--;
        			
        		}
        		storage[index] = value;
        	}
        	
        }
        
        //add value to the end of array
        public void add(int value) 
        {	
        	int endPosition = size;
        	insertAt(endPosition, value);
        	checkLast += 1;
        }
    
        //remove 
        public void removeAt(int index) 
        {
        	rangeCheck(index);
        	if (index == )
        		
        		
        		
        	checkLast -= 1;
        	
        }
        
        //print everything out
        public void printAll() 
        {
        	for(int i = 0;i<storage.length;i++)
        	{
        		
        		System.out.print("[" + i + "]=" + storage[i] + "\t" );
        	}
        	System.out.println();
        }
        
        public static void main(String args[]) 
        {
           DynamicArrayOfInts list1 = new DynamicArrayOfInts();
           list1.insertAt(0,1);
           list1.insertAt(1,2);
           list1.add(3);
           // list1 is 1, 2, 3
           System.out.print("list1: "); list1.printAll();
           list1.set(2,100);
           // list1 1 is 1, 2, 100
           //System.out.print("list1: "); list1.printAll();
           System.out.println("list1[2]=" + list1.get(2));
           list1.removeAt(2);
           // list1 is 1, 2
           System.out.print("list1: "); list1.printAll();	 
           DynamicArrayOfInts list2 = new DynamicArrayOfInts();
           list2.insertAt(0,2);
           list2.insertAt(0,3);	 	
           list2.insertAt(0,1);
           list2.removeAt(1);
           // list2 is 1, 2
           System.out.print("list2: ");list2.printAll();
           System.out.println("list1.size()=" + list1.size() + ", list2.size()=" + list2.size());
           // list1 and list2 are equal
           System.out.println("list1 equals list2 is " + list1.equals(list2));
           list2.insertAt(2,3);
           // list2 is 1, 2, 3
           System.out.println("list1.size()=" + list1.size() + ", list2.size()=" + list2.size());	 	
           // list1 and list2 are not equal
           System.out.println("list1 equals list2 is " + list1.equals(list2));	
           ArrayList list3 = new ArrayList();
           list3.add(1);list3.add(2);list3.add(3);
           System.out.println("list2 equals list3 is " + list2.equals(list3));	       
        }    
    }

    I am not sure how I going to make the add method work by calling insertAt....

  2. #2
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: Need help with add method for array.

    A quick look shows me a couple of easy syntax errors:
    Java Code:
        //add value to the end of array
        public void add(int value)
        {  
            int endPosition = size;
            insertAt(endPosition, value);
            checkLast += 1;
        }
    Is checkLast declared somewhere that I am not seeing?..


    Java Code:
        //remove
        public void removeAt(int index)
        {
            rangeCheck(index);
            if (index == )
                 
                 
                 
            checkLast -= 1;
             
        }
    What are you trying to compare index too?

    Still looking through code...

  3. #3
    RozenKristal is offline Member
    Join Date
    Sep 2012
    Posts
    10
    Rep Power
    0

    Default Re: Need help with add method for array.

    oh, removeAt(index) I have not yet doing it... I want to finish add(value) first before move to that method. And you right about checkLast, my apology, I did try to create it as a class field but delete it in the end. So basically the add method would add a value at the end of the array, by calling insertAt(index,value) but I am not sure how to accomplish that...

    For removeAt(index) method, I think there are 2 different cases. One is when I remove in the middle or first index of the array; second is remove the last member in the array. For the first case, if i remove the values, I can shift everything after that index to the left. For the second case, I have to have some check to verify that I am removing the last member of the array, so I dont need to do the shifting
    Last edited by RozenKristal; 10-15-2012 at 03:29 AM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Need help with add method for array.

    Quote Originally Posted by RozenKristal View Post
    I am not sure how I going to make the add method work by calling insertAt....
    What is wrong with the code you have?

Similar Threads

  1. Returning Array from a method
    By Manfizy in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2012, 01:01 PM
  2. 2D Array. method problem.
    By Juukamen in forum New To Java
    Replies: 12
    Last Post: 11-11-2011, 02:31 PM
  3. help writing array method
    By katiebear128 in forum New To Java
    Replies: 3
    Last Post: 10-16-2011, 06:31 PM
  4. Help - maxLength method for 2D Array
    By Hardo in forum New To Java
    Replies: 4
    Last Post: 09-14-2009, 05:28 PM
  5. Array Method Testing
    By Suzanne1187 in forum Java Applets
    Replies: 1
    Last Post: 04-15-2009, 09:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •