When you are going to shift elements to the right, you can't walk through the array from left to right since you would overwrite values on the next array spot. I also noticed you wrote "i < elements.length - 1". You probably shouldn't add the -1, or you will skip the last value in the array (unless you want that).
First of all, make sure your array has enough space (you will need 1 more), if you are working with primitive types. Therefor you need to create a second array.
Example:
int[] array = new int[prevValue+1];
Now you will have to shift the values to the right, to prevent overwriting the next value we start from the right and go to the left (depends on whether you use the same array or a different array).
for (int i = array.length-1; i >= startPos; i--) {
elements[i+1] = array[i];
}
Then on the extra spot you reserved you can set a new value:
elements[startPos]=value;
Last but not least, you will have to fill your previous elements (if you are using the same array you can ignore this).
for (int i = 0; i < startPos; i++) {
elements[i] = array[i];
}
Answer from jetspiking on Stack Overflowshift array elements to the right?
How can I shift the elements of an array to the right
sorting - Is it possible to use array_shift() in PHP and get the key? - Stack Overflow
java - Shift elements of an array - Stack Overflow
Videos
When you are going to shift elements to the right, you can't walk through the array from left to right since you would overwrite values on the next array spot. I also noticed you wrote "i < elements.length - 1". You probably shouldn't add the -1, or you will skip the last value in the array (unless you want that).
First of all, make sure your array has enough space (you will need 1 more), if you are working with primitive types. Therefor you need to create a second array.
Example:
int[] array = new int[prevValue+1];
Now you will have to shift the values to the right, to prevent overwriting the next value we start from the right and go to the left (depends on whether you use the same array or a different array).
for (int i = array.length-1; i >= startPos; i--) {
elements[i+1] = array[i];
}
Then on the extra spot you reserved you can set a new value:
elements[startPos]=value;
Last but not least, you will have to fill your previous elements (if you are using the same array you can ignore this).
for (int i = 0; i < startPos; i++) {
elements[i] = array[i];
}
Here is where your problem is:
for(int i = index; i < elements.length - 1; i++)
{
elements[i + 1] = elements[i];
}
Lets take your example.
{0, 1, 2, 3} i = 1
You set elements[i + 1], or elements[2] to elements[1]. So, you're new array is {0, 1, 1, 3}. So far so good. But now, when you set 3 to the current number, you set that to the thing you set before. So you will get {0, 1, 1, 1}. The way to avoid this is to iterate from the right, so numbers won't get lost.
can anyone explain what to do?
for(int i =current_index; i <used; i++)
{
int temp = data[i+1];
data[i+1] = data[i];
data[i+2] = temp;
}this doesn't seem to be doing it
Im trying to add a string into an index of an existing array with inputted values and shift the old values to the right 1 place. for example an array I have created has 5 spaces in total and hold string values is [A, B, C] when I add Z to index 0 the output is [Z, A, A, A] instead of [Z, A, B, C]. I am confused on why it copies the A from index 0 to the other indices of the filled array. Here is the code for my function. curIndex is a integer instance variable that holds the current index of the array.
public void add (int index, String theData)
{
for(int i = 0; i < curIndex; i++)
{
strarray[i + 1] = strarray[i];
}
strarray[index] = theData;
}$result = array_splice( $yourArray, 0, 1 );
... should do the trick. See array_splice.
You could use each like:
$b = array(1=>2, 3=>4, 7=>3);
while(1) {
list($key,$value) = each($b);
if (empty($key))
break;
echo "$key $val\n";
}
Iterating the array with each will keep its last position.
"How many times does this number need to shift" in this case is actually just "how many odd numbers in total are before this number", because you are trying to move all the even numbers to the beginning without changing the relative positions of the even numbers. Each even number needs to "move over" however many odd numbers there are before it.
So you just need to keep a cumulative count:
int[] a = { 1, 3, 2, 5, 4, 7, 8, 6 };
List<Integer> shifts = new ArrayList<>();
int oddNumberCount = 0;
for (int i : a) {
if (i % 2 == 0) {
shifts.add(oddNumberCount);
} else {
oddNumberCount++;
}
}
I don't know how many even numbers there will be, so I used a list. If you require an array as the output you can convert it to a list simply like this (See Also):
int[] result = shifts.stream().mapToInt(x -> x).toArray();
If you do know how many even numbers there will be, you can of course just start with an array of that given size.
What are you lacking in your code is you don't track what's the current position of the even numbers correctly:
public static void main(String...arg){
int[] a={1,3,2,5, 4, 7, 8, 6,0}; //the array to be shifted
int indexEvenNumber = 0;//where should we put the even number if we found it.
for (int i = 0; i < a.length; i++) {
if (a[i] % 2 == 0) {
int temp = a[i];
a[i] = a[indexEvenNumber];
a[indexEvenNumber] = temp;
indexEvenNumber++;
}
}
// print the shifted array to confirm results
Arrays.stream(a).forEach(System.out::println);
}