3 Replies - 190 Views - Last Post: 14 October 2012 - 08:27 AM Rate Topic: -----

#1 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 309
  • Joined: 29-September 12

insertion into an array.

Posted 14 October 2012 - 12:11 AM

So I making a dynamic array that mimic a arraylist, I got the insertAt operation, but now I making an add method. I can call the insert at, but add method would add at the end of the array. I wonder what should be done, here what I have so far:

//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;
    	}
    	
    }


I was thinking if I get the size of the array, and look for the length, then I get length -1 to have the last index, and parse it thru the parameter.

//add value to the end of array
    public void add(int value) 
    {	
    	int endPosition = size;
    	insertAt(endPosition, value);
    }


This post has been edited by RozenKristal: 14 October 2012 - 12:15 AM


Is This A Good Question/Topic? 0
  • +

Replies To: insertion into an array.

#2 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: insertion into an array.

Posted 14 October 2012 - 12:28 AM

Without seeing your whole code, it's hard to tell what would happen.

Try it and see what happens. If you still have trouble, reply to this thread with what the problem is.
Was This Post Helpful? 0
  • +
  • -

#3 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 309
  • Joined: 29-September 12

Re: insertion into an array.

Posted 14 October 2012 - 12:34 AM

Here is everything: My problem is I am unsure what to put in the parameter of insertAt when I call it from the add method.

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

    	return false;  // added so code would compile
    }
    
    //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);
    }

    //remove 
    public void removeAt(int index) 
    {
    	rangeCheck(index);
    	
    }
    
    //print everything out
    public void printAll() 
    {

    }
    
    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));	       
    }    
} 


Was This Post Helpful? 0
  • +
  • -

#4 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 309
  • Joined: 29-September 12

Re: insertion into an array.

Posted 14 October 2012 - 08:27 AM

sorry but I would like to bump this to see if there is any response :(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1