If you are actually using the array as a stack (and thus, will only add or remove items at the top of the stack) then you could keep in another variable the next free index in the array.
int[] array = new int[21];
int nextIndex = 0;
public void push(int e) {
array[nextIndex] = e;
++nextIndex;
}
public int pop() {
--nextIndex;
return array[nextIndex];
}
If removals can occur anywhere, then I don't see a better solution than iterating over the array to find a free spot.
Answer from ARRG on Stack OverflowWithout using an ArrayList how would you insert a value into the middle of an array? Java
Java array, add item into next empty index - Stack Overflow
java - How to add an element to Array and shift indexes? - Stack Overflow
Assigning values to an array without a loop? (Java)
Videos
So for my homework I have to do a lot of array editing and already have a some code for the original array and for adding one element to the end. Iโm not sure where I should fit this into the code. I know I need the length of the updated array but am not sure which method to use. Browsing the Java API I saw a couple possibilities like copyOf() or addPos()โฆI could add the code but am curious to hear your approach to this and appreciate your help.
Thank you
If you are actually using the array as a stack (and thus, will only add or remove items at the top of the stack) then you could keep in another variable the next free index in the array.
int[] array = new int[21];
int nextIndex = 0;
public void push(int e) {
array[nextIndex] = e;
++nextIndex;
}
public int pop() {
--nextIndex;
return array[nextIndex];
}
If removals can occur anywhere, then I don't see a better solution than iterating over the array to find a free spot.
That is why Listhave been made. Simply use something like this:
List<Integer> negativeIntegers = new ArrayList<Integer>(21);
...
negativeIntegers.add(-127);
The most simple way of doing this is to use an ArrayList<Integer> and use the add(int, T) method.
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
// Now, we will insert the number
list.add(4, 87);
This should do the trick:
public static int[] addPos(int[] a, int pos, int num) {
int[] result = new int[a.length];
for(int i = 0; i < pos; i++)
result[i] = a[i];
result[pos] = num;
for(int i = pos + 1; i < a.length; i++)
result[i] = a[i - 1];
return result;
}
Where a is the original array, pos is the position of insertion, and num is the number to be inserted.
I'm looking for a way to assign values to an array without the need for a for loop. Ideally my array would look like this:
int[] arr = new int[n]
Where n is input by the user. For example, if the user input "3", I want the array elements to have the values of
arr[0] = 0; arr[1] = 1; arr[2] = 2;
...and so on. I know how to do this with a for loop, but I am running into issues because I have to pull the values later, but I can't because they're nested in the loop. I also can't really use {...} because the array size is determined by the user. Is there a way to do this without a loop?