In Java, is there a way to add a value not to a specific index, but to the next undeclared index? Say:
int[] negativeArray = new int[21]; int[] positiveArray = new int[21];
There are two arrays for two different int types, for example, negative and positive. I focus on this and I want it to work like a stack (I donβt know too much about stacks, but as far as I know, you donβt look at its index, just click objects on it) where, if it's a negative number, enter the number in the next undeclared index spot in the negative array.
I was thinking of a way to do this with extra code. I would set all the values ββin the array to 0. When I check if the variable is negative or positive, I would iterate over the array to the next value equal to 0. As soon as I find it, I know in which index I am included. It will take a little effort, but are there any simpler ways to do this?
Edit: some comments indicate different ways to do this without using a base array. I was assigned this, and I have to use an array to get a loan for it ...
source share