public static void deletionArray(int anArray[], int positionToDelete) {
for (int j = anArray[positionToDelete] - 1; j < anArray.length; j++) {
System.out.println("j is " + j);
anArray[j] = anArray[j + 1];
}
displayArray(anArray);
}
14 Replies - 1125 Views - Last Post: 02 February 2013 - 03:16 PM
#1
How do I shift array elements up one position in java?
Posted 02 February 2013 - 02:19 PM
Replies To: How do I shift array elements up one position in java?
#2
Re: How do I shift array elements up one position in java?
Posted 02 February 2013 - 02:27 PM
What you have posted should work except that you can't iterate to the end of the array and then use the index endOfTheArray + 1. Try iterating to one before the end of the array.
#3
Re: How do I shift array elements up one position in java?
Posted 02 February 2013 - 02:31 PM
for (int j = anArray[positionToDelete] - 1; j < anArray.length - 1; j++) {
System.out.println("j is " + j);
anArray[j] = anArray[j + 1];
}
displayArray(anArray);
}
But I get the same result. The array doesn't seem to delete the position by moving all of the other elements up.
#4
Re: How do I shift array elements up one position in java?
Posted 02 February 2013 - 02:32 PM
#5
Re: How do I shift array elements up one position in java?
Posted 02 February 2013 - 02:34 PM
#6
Re: How do I shift array elements up one position in java?
Posted 02 February 2013 - 02:34 PM
jon.kiparsky, on 02 February 2013 - 02:32 PM, said:
I am not allowed to use array.copy as I need to collect array usage information. Sadly, I have to do it manually.
#7
Re: How do I shift array elements up one position in java?
Posted 02 February 2013 - 02:37 PM
joerdie, on 02 February 2013 - 04:31 PM, said:
for (int j = anArray[positionToDelete] - 1; j < anArray.length - 1; j++) {
System.out.println("j is " + j);
anArray[j] = anArray[j + 1];
}
displayArray(anArray);
}
But I get the same result. The array doesn't seem to delete the position by moving all of the other elements up.
If you only have five filled, then you probably only want to copy five, no?
Let's try clarifying a little. What is the initial state of your array, and what would you like it to look like when you're done?
#8
Re: How do I shift array elements up one position in java?
Posted 02 February 2013 - 02:38 PM
GregBrannon, on 02 February 2013 - 02:34 PM, said:
public static void deletionArray(int anArray[], int positionToDelete) {
displayArray(anArray);
for (int j = anArray[positionToDelete] - 1; j < anArray.length - 1; j++) {
System.out.println("j is " + j);
anArray[j] = anArray[j + 1];
}
displayArray(anArray);
}
Gives me,
97
86
61
46
3
0
0
0
0
0
97
86
61
46
3
0
0
0
0
0
#9
Re: How do I shift array elements up one position in java?
Posted 02 February 2013 - 02:43 PM
Quote
Let's try clarifying a little. What is the initial state of your array, and what would you like it to look like when you're done?
Yes, I could move just the five. The initial state would be 5 integers followed by zeros since there is nothing there. When I am done, I am looking to have one of those integers deleted and the integers below the deleted one moved up one position. I suppose I would then have an extra 0 at the bottom of my array.
#10
Re: How do I shift array elements up one position in java?
Posted 02 February 2013 - 02:47 PM
public static void deletionArray(int anArray[], int positionToDelete) {
displayArray(anArray);
for (int j = anArray[positionToDelete] - 1; j < anArray.length - 1; j++) {
System.out.println("j is " + j);
anArray[j] = anArray[j + 1];
}
displayArray(anArray);
}
Oh, hang on. What's this? int j = anArray[positionToDelete] - 1;
What is that value?
EDIT: If you don't see why this is a problem, print out that value. Then fix your program and enjoy the rest of your day.
This post has been edited by jon.kiparsky: 02 February 2013 - 02:49 PM
#11
Re: How do I shift array elements up one position in java?
Posted 02 February 2013 - 02:54 PM
#12
Re: How do I shift array elements up one position in java?
Posted 02 February 2013 - 02:57 PM
jon.kiparsky, on 02 February 2013 - 02:47 PM, said:
public static void deletionArray(int anArray[], int positionToDelete) {
displayArray(anArray);
for (int j = anArray[positionToDelete] - 1; j < anArray.length - 1; j++) {
System.out.println("j is " + j);
anArray[j] = anArray[j + 1];
}
displayArray(anArray);
}
Oh, hang on. What's this? int j = anArray[positionToDelete] - 1;
What is that value?
EDIT: If you don't see why this is a problem, print out that value. Then fix your program and enjoy the rest of your day.
I am looking to go from this {95,90,81,41,7,0,0,0,0,0} getting rid of say the 90 to this. {95,81,41,7,0,0,0,0,0,0}
anArray[positionToDelete] is the position I am trying to delete. I can, and have printed that value while trying to solve this issue. the - 1 bit is there because I am advancing my loop before I change anything. So if I were using the above arrays as an example, anArray[positionToDelete] would be 90 in position 1. The -1 would make the position 0 (95) so that when the I go into the block I am back to position 1.
#13
Re: How do I shift array elements up one position in java?
Posted 02 February 2013 - 03:06 PM
joerdie, on 02 February 2013 - 04:57 PM, said:
Not exactly. anArray[positionToDelete] is the value at that position. positionToDelete is the index: that's the "position" you're trying to work with. If positionToDelete is 1 in the current example (and let's please just stick with one sample array, they're each as good as the next) then what is the value of anArray[positionToDelete]?
And what happens when you plug that value into your loop
for (int j = anArray[positionToDelete] - 1; j < anArray.length - 1; j++) {
anArray[j] = anArray[j + 1];
}
(The current array is the one you mentioned most recently: {95,90,81,41,7,0,0,0,0,0} )
#14
Re: How do I shift array elements up one position in java?
Posted 02 February 2013 - 03:15 PM
jon.kiparsky, on 02 February 2013 - 03:06 PM, said:
joerdie, on 02 February 2013 - 04:57 PM, said:
Not exactly. anArray[positionToDelete] is the value at that position. positionToDelete is the index: that's the "position" you're trying to work with. If positionToDelete is 1 in the current example (and let's please just stick with one sample array, they're each as good as the next) then what is the value of anArray[positionToDelete]?
And what happens when you plug that value into your loop
for (int j = anArray[positionToDelete] - 1; j < anArray.length - 1; j++) {
anArray[j] = anArray[j + 1];
}
(The current array is the one you mentioned most recently: {95,90,81,41,7,0,0,0,0,0} )
That did it thank you. I can't believe I have been on this for 4 hours. Thanks.
#15
Re: How do I shift array elements up one position in java?
Posted 02 February 2013 - 03:16 PM
|
|

New Topic/Question
Reply



MultiQuote




|