5

In Java, is there a way to add a value not at 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 types of ints, for example negative and positive. I'm looping through this, and I want it to work something like a stack (I don't know too much about stacks, but to my knowledge you don't go through its index, you just pop/push items onto it) to where if its a negative number, put in the number in the next undeclared index spot in the negative array.

I thought of a way to do this with some extra code. I'd set all values in the array to 0. When checking if the variable is negative or positive, I would loop through the array to the next value that is 0. Once I find it I know what index I'm on. This would require a bit of effort though, are there any simpler ways of doing this?


Edit: Some comments are stating different ways to do this without using a basic array. I was assigned this, and I am required to use an array to get credit for it...

3
  • @assylias Ooh I've never used one of those before, I'll try it out. But the thing is, I am required to use an array because its a school assignment. Commented Feb 15, 2012 at 16:05
  • 1
    Posting questions about homework is fine, but please tag the question as homework:) Commented Feb 15, 2012 at 16:09
  • @ARRG it wasn't homework Commented Nov 15, 2023 at 4:02

4 Answers 4

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What happens to the element at [nextIndex] after you poped it? Would it leak? I think this example appears in Effective Java. Better if we add in the pop method something to nullify the element at nextIndex.
@Random : You are right in the case where we are storing Objects, the object wouldn't be garbage collected if we didn't remove it from the array. This isn't a problem for integers (and other primitive types) though because the "null" (aka 0) that you would write instead takes the same amount of memory.
3

That is why Listhave been made. Simply use something like this:

List<Integer> negativeIntegers = new ArrayList<Integer>(21);
...
negativeIntegers.add(-127);

Comments

1

If you want to create an array with the positive values and one with the negative values, you can do it with the algorithm you propose:

public static void main(String[] args) throws Exception {
    int[] negativeArray = new int[3];
    int[] positiveArray = new int[3];

    int[] test = new int[] {1, -1, 2, -2, 3, -3};

    int posIndex = 0;
    int negIndex = 0;

    for (int i = 0; i < test.length; i++) {
        if (test[i] > 0) {
            positiveArray[posIndex++] = test[i];
        } else if (test[i] < 0) {
            negativeArray[negIndex++] = test[i];
        }
    }

    System.out.println(Arrays.toString(test)); //[1, -1, 2, -2, 3, -3]
    System.out.println(Arrays.toString(positiveArray)); //[1, 2, 3]
    System.out.println(Arrays.toString(negativeArray)); //[-1, -2, -3]
}

Comments

0

Check out http://docs.oracle.com/javase/6/docs/api/java/util/Stack.html

It does basically the same as ARRG's solution, but with fewer lines of code:

Stack<Integer> negative = new Stack<Integer>();
Stack<Integer> positive = new Stack<Integer>();

public void putMyNumber(int number) {
  // Taking 0 as "positive"
  if (number >= 0) {
    positive.push(number); //Auto-boxing
    return;
  }
  negative.push(number);
}

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.