Results 1 to 16 of 16
- 06-22-2011, 04:51 PM #1
Member
- Join Date
- May 2011
- Posts
- 12
- Rep Power
- 0
Inserting a element in the middle of the array
Hey!
I´m 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 i´m 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 i´m really stack on the pushing of the elements part,any help would be appreciated!
the code i wrote so far is:
this is probably more simple than i´m making it to be,hope some1 can help me out,thanks in advance!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++; } }
- 06-22-2011, 05:03 PM #2
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?
- 06-22-2011, 05:28 PM #3
Member
- Join Date
- May 2011
- Posts
- 12
- Rep Power
- 0
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
- 06-22-2011, 05:33 PM #4
This is the part that needs the algorithm/design.then copy the elements after the specified position,living a "hole" in the array
Can you show us the results of executing your code? For example:
System.out.println("after shift " + Arrays.toString(elements)); // print changed array
- 06-22-2011, 05:40 PM #5
Member
- Join Date
- May 2011
- Posts
- 12
- Rep Power
- 0
Saw that i never really copied the array to the temp one propperly,edited the code abit:
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.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++;
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..
- 06-22-2011, 05:43 PM #6
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
- 06-22-2011, 05:52 PM #7
Member
- Join Date
- May 2011
- Posts
- 12
- Rep Power
- 0
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,i´m using a Textbased interface i wrote when i´m testing my programm,and not "hardcoding" it,if it matters.
- 06-22-2011, 06:01 PM #8
Then either the compile failed and you are executing an old versioni still didnt get any outprints.
Or the println statements are in the wrong place and are not being executed.
Not sure what that means. What is a "Textbased interface"?i´m using a Textbased interface i wrote when i´m testing my program
- 06-22-2011, 06:05 PM #9
Member
- Join Date
- May 2011
- Posts
- 12
- Rep Power
- 0
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.Not sure what that means. What is a "Textbased interface"?
- 06-22-2011, 06:08 PM #10
I assume you mean the the user interface I/O for the program is on a console.
- 06-22-2011, 06:49 PM #11
Member
- Join Date
- May 2011
- Posts
- 12
- Rep Power
- 0
Rewrote my code abit,it SHOULD work,draw the array/content on the paper etc.
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.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++; } }
Might be some small thing i missed..hope someone can help me out
- 06-22-2011, 06:57 PM #12
The computer will tell you.Might be some small thing i missed.
Have you executed it? What does it do?
- 06-22-2011, 07:20 PM #13
Member
- Join Date
- May 2011
- Posts
- 12
- Rep Power
- 0
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}
- 06-22-2011, 07:29 PM #14
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.
- 06-22-2011, 07:42 PM #15
Member
- Join Date
- May 2011
- Posts
- 12
- Rep Power
- 0
inserted a println in the loops,which shows the values of the arrays:
Seems like this loop that is supposed to move the content one step,is not doing what it should:temp array:0
temp array:2
temp array:null
new array0
new array2
2nd array after rearenging elements:null
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]); }
- 06-22-2011, 07:47 PM #16
Similar Threads
-
Delete Middle element from arrray
By tttestall in forum New To JavaReplies: 3Last Post: 04-24-2011, 07:25 PM -
Need help with inserting a new number into an array
By Get_tanked in forum New To JavaReplies: 7Last Post: 01-21-2011, 02:36 AM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks