Results 1 to 16 of 16
  1. #1
    w95
    w95 is offline Member
    Join Date
    May 2011
    Posts
    12
    Rep Power
    0

    Default Inserting a element in the middle of the array

    Hey!
    Im writing a template with a array as a structure,working on a function atm,that should insert a element in the array by me specifing the position(index) of it.

    The trouble im having is,that when i try to insert a element in a position that allrdy has a element in it(as it should,since i dont want any "holes" in the array),i cant see to get the "pushing" of elements done properly.
    for example if the array with the capacity of 3,contain elements {0,2},and insert a integer 1 at position
    [1],i want the content of the array become {0,1,2}
    I got the extension of the array working,so it should "extend" the array if the is not enough space.
    But im really stack on the pushing of the elements part,any help would be appreciated!

    the code i wrote so far is:

    Java Code:
    public void add(T elem, int pos) 
    {
    	if(pos>this.capacity)
    	{
    		System.out.println("Invalid Position!");
    	}
    	if(this.nrofelements==this.capacity)
    	{
    
    		T[] tmp;
    		tmp=(T[])new Object[capacity];
    		for(int i=0;i<capacity;i++)
    		{
    			tmp[i]=this.elements[i];			
    		}
    		this.capacity+=3;
    		
    
    		this.elements=(T[])new Object[capacity];
    
    	}
    	if(this.elements[pos]==null)
    	{
    		this.elements[pos]=elem;
    		nrofelements++;
    	}
    	else
    	{
    		this.capacity+=1;
    		T[] tmp;
    		tmp=(T[])new Object[capacity];
    
    		tmp=this.elements;
    		
    
    		this.elements=(T[])new Object[capacity];
    		for(int i=0;i<pos;i++)
    		{
    			this.elements[i]=tmp[i];
    		}
    		for(int i=pos+1;i<this.capacity;i++)
    		{
    			this.elements[i+1]=tmp[i];
    		}
    		this.elements[pos]=elem;
    		nrofelements++;
    	}
    	
    }
    this is probably more simple than im making it to be,hope some1 can help me out,thanks in advance!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default

    What is the problem with the code that you posted? Can you print out the results to show us what happens?
    Can you explain the logic of your code?
    What are the steps you need to do to shift the contents of an array up by one element starting at element ix?
    Have you tested your algorithm using a piece of paper to see if it works?

  3. #3
    w95
    w95 is offline Member
    Join Date
    May 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What is the problem with the code that you posted? Can you print out the results to show us what happens?
    Can you explain the logic of your code?
    What are the steps you need to do to shift the contents of an array up by one element starting at element ix?
    Have you tested your algorithm using a piece of paper to see if it works?
    The way i want that code to work is,starting at the third if statement:
    if the position of the elements is equal to null(is empty) i insert the element the usual way.
    if its not,i create a new array with its capacity +1,copy over the old elements before the specified position,then copy the elements after the specified position,living a "hole" in the array,in which i then insert the element in.
    On second thought,the checking if the position is null is probably unnecessary,since if its null,its eighter at the end of the array,or the array is empty,or i already has a "hole" in it.

    There is probably a simplier way to solve this problem,gonna try to write the code to start moving the elements from the end of the array one step forward,and when i come to the position,inserting the element in the "hole".

    hope that made sence:P

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default

    then copy the elements after the specified position,living a "hole" in the array
    This is the part that needs the algorithm/design.

    Can you show us the results of executing your code? For example:
    System.out.println("after shift " + Arrays.toString(elements)); // print changed array

  5. #5
    w95
    w95 is offline Member
    Join Date
    May 2011
    Posts
    12
    Rep Power
    0

    Default

    Saw that i never really copied the array to the temp one propperly,edited the code abit:
    Java Code:
    	T[] tmp;
    		tmp=(T[])new Object[capacity];
    		this.capacity+=1;
    		
    		for(int i=0;i<capacity;i++)
    		{
    			tmp[i]=this.elements[i];
    		}
    		
    		this.elements=(T[])new Object[capacity];
    		for(int i=0;i<pos;i++)
    		{
    			this.elements[i]=tmp[i];
    		}
    		for(int i=pos+1;i<this.capacity;i++)
    		{
    			this.elements[i+1]=tmp[i];
    		}
    		this.elements[pos]=elem;
    		nrofelements++;
    I have two add functions,one that adds in the "usual" way,starting from index 0 and ascending,and this one,that adds to the position i specify.
    First i add int 0,and 2,the "usual" way,then i try to add element 1 to the position 1.
    so the array should look like this {0,1,2}
    then i call the function that shows the content of the array at the specified position,and it shows that at pos 0 the array value is null,at pos 1 the value is 1,and and the pos 2 the value is null.

    So my function dosent work at all..

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default

    Can you show us the before and after results of executing your code? For example:
    System.out.println("before shift " + Arrays.toString(elements)); // print original array
    ... insert here
    System.out.println("after shift " + Arrays.toString(elements)); // print changed array

  7. #7
    w95
    w95 is offline Member
    Join Date
    May 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Can you show us the before and after results of executing your code? For example:
    System.out.println("before shift " + Arrays.toString(elements)); // print original array
    ... insert here
    System.out.println("after shift " + Arrays.toString(elements)); // print changed array
    I tried the code you wrote,but Eclipse redmarked the "Arrays",and suggested that i would import some library..i did but after compiling i still didnt get any outprints.

    BTW,im using a Textbased interface i wrote when im testing my programm,and not "hardcoding" it,if it matters.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default

    i still didnt get any outprints.
    Then either the compile failed and you are executing an old version
    Or the println statements are in the wrong place and are not being executed.

    im using a Textbased interface i wrote when im testing my program
    Not sure what that means. What is a "Textbased interface"?

  9. #9
    w95
    w95 is offline Member
    Join Date
    May 2011
    Posts
    12
    Rep Power
    0

    Default

    Not sure what that means. What is a "Textbased interface"?
    Well basicly i get a menu printed,press 1 for adding and element,press 2 for adding element to specified position etc,not that it should matter.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default

    I assume you mean the the user interface I/O for the program is on a console.

  11. #11
    w95
    w95 is offline Member
    Join Date
    May 2011
    Posts
    12
    Rep Power
    0

    Default

    Rewrote my code abit,it SHOULD work,draw the array/content on the paper etc.

    Java Code:
    public void add(T elem, int pos) 
    {
    	if(pos>this.capacity)
    	{
    		System.out.println("Invalid Position!");
    	}
    	else
    	{
    		if(this.nrofelements==this.capacity)
    		{
    
    			T[] tmp;
    			tmp=(T[])new Object[capacity];
    			for(int i=0;i<capacity;i++)
    			{
    				tmp[i]=this.elements[i];			
    			}
    			this.capacity+=3;
    			
    
    			this.elements=(T[])new Object[capacity];
    
    		}
    
    			
    			T[] tmp;
    			tmp=(T[])new Object[capacity];
    
    
    			for(int i=0;i<capacity;i++)
    			{
    				tmp[i]=this.elements[i];
    			}
    			this.capacity+=1;
    			this.elements=(T[])new Object[capacity];
    			for(int i=0;i<this.nrofelements;i++)
    			{
    				this.elements[i]=tmp[i];
    			}
    			for(int i=this.nrofelements;i>pos;i--)
    			{
    				this.elements[i+1]=this.elements[i];
    			}
    			this.elements[pos]=elem;
    			nrofelements++;
    					
    
    
    	}
    
    	
    }
    Basicly,what it should do it,starting at the end of the array,traversing to the specified pos,while moving the elements one step "forward",creating a hole at the specified position,and the last inserting the element there.
    Might be some small thing i missed..hope someone can help me out

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default

    Might be some small thing i missed.
    The computer will tell you.
    Have you executed it? What does it do?

  13. #13
    w95
    w95 is offline Member
    Join Date
    May 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    The computer will tell you.
    Have you executed it? What does it do?
    Yes,i have executed it and it runs.But its logical error, if i add element 0 and 2 with my first add function,the elements are at pos 0 and 1.
    When i try to use my second add function that places elements by pos,i type in element 1 to be placed at pos 1,so the array should look like this {0,1,2}
    ,but the result is {null,1,null}

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default

    Are you sure you are working with the correct array?
    Your result shows an array with only the inserted item in it.
    Where are the copied elements?

    Can you write a small simple program that will compile and execute with an array with 2 elements? Then write the logic to insert a new element between those two elements.
    Print out the array before the insert and print out the new array after the insert.

  15. #15
    w95
    w95 is offline Member
    Join Date
    May 2011
    Posts
    12
    Rep Power
    0

    Default

    inserted a println in the loops,which shows the values of the arrays:


    temp array:0
    temp array:2
    temp array:null
    new array0
    new array2
    2nd array after rearenging elements:null
    Seems like this loop that is supposed to move the content one step,is not doing what it should:
    Java Code:
    			for(int i=this.nrofelements;i>pos;i--)
    			{
    				this.elements[i+1]=this.elements[i];
    				System.out.println("2nd array after rearenging elements:"+this.elements[i]);
    			}

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default

    Ok, I give up. I've suggested an approach but you'd rather do it your way.
    Good luck.

    Hopefully some one else will pick up this thread.

Similar Threads

  1. Delete Middle element from arrray
    By tttestall in forum New To Java
    Replies: 3
    Last Post: 04-24-2011, 07:25 PM
  2. Need help with inserting a new number into an array
    By Get_tanked in forum New To Java
    Replies: 7
    Last Post: 01-21-2011, 02:36 AM
  3. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 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
  •