3

I'm practicing what are pretty basic java array exercises and I'm having a hard time wrapping my head around how to insert an element into the beginning of an array and then shift the remaining elements to the right. So if the array hasn't gone over its max size, inserting a z in front of array, j, a, v, a would make for z, j, a, v, a.

I know how to do this with array lists, I'm just having a difficult time getting the logic correct with arrays. This is what I have so far:

 public void addFront(char ch)
 {
   for(int i = 1; i < data.length-1; i++){
     char temp = data[i - 1]; 
     data[i] = temp; 
   }
   data[0] = ch; 

 }

It seems like I need a temporary variable but I'm not using it correctly in this instance. Any input would be appreciated!

2
  • I know how this would work with ArrayLists, but I'm practicing with just arrays and trying to wrap my brain around how this would work with arrays. Commented Dec 10, 2013 at 0:30
  • 1
    @nhgrif Why not Array? Commented Dec 10, 2013 at 0:30

3 Answers 3

2

Let's take a look at what your current loop is doing. It is copying the character from position i - 1 to the current position. But the next loop will copy it from (current) i to (current) i + 1. It will just copy the first character over every position in the array except for the last position.

You must iterate backwards through the array, so that one shift doesn't accidentally use the result of the previous shift.

Start at index data.length - 1, and copy from position i - 1 to position i, making sure that the last iteration is when i is 1.

Additionally, a temp variable isn't needed. You can copy the value directly, i.e.

data[i] = data[i - 1];
Sign up to request clarification or add additional context in comments.

Comments

1

Start at the back end of the array.

If you move element 0 to element 1, then element 1 to element 2... well you already copied element 0 to element 1.... so now you'll just copy element 0 over the entire array.

Start at the back end of the array shifting everything to the right, then after you're done with that, insert the new element at the front.

public void addFront(char ch) {
    for(int i = data.length-1; i > 0; --i) {
        data[i] = data[i-1];
    }
    data[0] = ch;
}

Comments

0

You don't need a temporary variable for an insert, as described this should work...

public void addFront(char ch)
{
  for(int i = data.length - 1; i > 0; i--){ // start at the end.
     data[i] = data[i-1]; // move every element up 1... that is set the current 
                          // element to the prior element.
  }
  for (int i = data.length - 1; i >= 0; i--) {
    if (data[i] == null) { // find the first blank...
      data[i] = ch;  // set the initial value.
      break;
    }
  }
}

4 Comments

Why are we iterating through the entire array twice?
@nhgrif I wasn't sure we started at 0... the question was a little vague... obviously this assumes data is a Character[].
That's fine if you want to do this. I still don't understand why we're iterating all the way through twice? Why not just data[i] = ch; in the first for loop first time we come across a null (after moving)?
for(/*stuff*/){ data[i] = data[i-1]; if(data[i] == null) { data[i] = ch; break; } } One loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.