1

How do I add a number(73) into the middle of an array and then move all the numbers from the middle up one so no number is overwriting. Here is my code so far the 73 should go into the middle and the numbers after it should all move over. Can't use an ARRAYLIST.

int midpoint = length/2;
array[midpoint] = 73;

for (int i = midpoint; i<length; i++){
    aNums[i+1] = array[i];
    System.out.print(array[i] + " ");
}

displayArray1(array,length);
4
  • If you must use fixed-size Java arrays (vs one of Java's several array-like aggregate classes) then you need to create a new array that's one element larger than the current one and copy the entries appropriately. Commented Feb 27, 2013 at 17:38
  • 1
    Use at least System.arraycopy to move array contents. It automatically works in ascending/descending mode to prevent overwriting elements. Commented Feb 27, 2013 at 17:39
  • linkedlist add is O(1) complexity as well Commented Feb 27, 2013 at 17:39
  • 1
    @75inchpianist LinkedList only has that complexity if you can find the middle element in O(1) time. But since you have to navigate to the middle element, it's still O(n). Commented Feb 27, 2013 at 17:45

3 Answers 3

1

You can't add to an array. You first have to create a bigger array.

int[] newArray = new int[array.length + 1];

Then you have to copy the first half of the array

for(int i = 0; i < midpoint; i++) {
    newArray[i] = array[i];
}

Then put the new midpoint in

newArray[midpoint] = 73;

Then copy the other half

for(int i = midpoint + 1; i < array.length; i++) {
    newArray[i+1] = array[i];
}

And then newArray has the new midpoint.

Technically the last three steps could be done in any order, but it is much more readable to do them in that order. Now you can call your display method or really do whatever you want with it.

There is a utility method called arrayCopy that can assist with moving the array elements. You may or may not be permitted to use it. It's a bit wordy with its parameters, but is a bit faster than a typical for-loop at runtime because it leverages native code.

int[] newArray = new int[array.length + 1];
System.arrayCopy(array,0,newArray,0,midpoint);
newArray[midpoint] = 73;
System.arrayCopy(array,midpoint,newArray,midpoint+1,array.length - midpoint);

To explain those calls, the arraycopy uses:

System.arrayCopy(arrayFrom, 
                 startPosInArrayFrom, 
                 arrayTo, 
                 startPosInArrayTo, 
                 numElementsToCopy);
Sign up to request clarification or add additional context in comments.

3 Comments

I grudgingly give you the +1 for actually answering his question instead of saying "JUST USE ARRAYLIST", even though I dislike giving help to people who show no effort on their part. Haha
Jeff, how can you say there was no effort on his part? He posted his code with his attempt, but he failed. He wasn't far off, he was just missing an important property of arrays: they have a fixed length. And clearly he cannot use ArrayList, I'd put my job on the fact that this is a university assignment (hence the step by step explanation).
shrug I guess it came off as a "here's my code; fix it" request/demand from his last sentence. You're probably right.
1

Use a List, or more specifically an ArrayList:

ArrayList<Integer> list = new ArrayList<>();

// ... put stuff in list

int midpoint = list.size()/2;
list.add(midpoint, 73);

Comments

0

you are causing yourself a lot more trouble using an array.

use an ArrayList, which is backed by an array

ArrayList l = new ArrayList();

//...fill contents
int index = l.size()/2;
l.add(index, 72);

Comments

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.