0

I have a class called "Transaction.java", and I have an ArrayList with the newest five transactions. So the size of this ArrayList should always be between 0 and 5.

When there's a new transaction, I have to rotate all the entries in this ArrayList. Entry 4 becomes entry 3, entry 3 becomes entry 2, entry 2 becomes entry 1, entry 1 becomes entry 0 and entry 0 becomes the new transaction. The oldest one (the object with the index 4) is overwritten.

When trying to code this, I'm in trouble with ArrayIndexOutOfBoundsExceptions or NPEs because I do transactions.set(4, transactions.get(3));, even if there's nothing in transaction.get(3), and transaction.get(4) is empty as well!

This is a little snippet of my code:

for (int i =  4; i >= 0; i--) {
    try {
        if (i == 0) {
            latestTransactions.set(i, newTransaction);
        }
        else {
            latestTransactions.set(i, latestTransactions.get(i - 1));
        }
    } catch (Exception e) { // NPE / AIOOBE
        e.printStackTrace();
    }

}

The exception is thrown 5 times (that's how big the for-loop is) and nothing's set in the ArrayList.

Please try to keep calm and understandable, I'm new in the buiseness ^^

4
  • latestTransactions.get(i - 1) - what will happen in the last iteration where i is 0? Commented Jun 2, 2015 at 16:23
  • 1
    You should have a look at Queue interface and implementation: stackoverflow.com/questions/9580457/fifo-class-in-java Commented Jun 2, 2015 at 16:23
  • Any particular reason you are using ArrayList over bounded queues? They will be less prone to errors of sizing and easier to manage elements in your use case Commented Jun 2, 2015 at 16:26
  • An ArrayList is a very inefficient data structure for what you are trying to do with it. You'd be better off using a LinkedList instead. Commented Jun 2, 2015 at 16:28

2 Answers 2

1

You can do it without the loop.

Assuming the ArrayList already contains 5 elements :

oldestElement = latestTransactions.remove(0); // this will remove the element at the 0 index
                                              // and decrement the index of all the others

Then

latestTransactions.add(newElement); // will add the new element at the end (index 4)

If the ArrayList can have less than 5 elements, you can write :

if (latestTransactions.size () >= 5) {
    oldestElement = latestTransactions.remove(0);
}
latestTransactions.add(newElement);

This will only remove the oldest element if the ArrayList already contains 5 elements, and add the new element at the end.

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

1 Comment

I have to assume that there's 0 up to 5 elements in the ArrayList. It does not always contain 5 elements though.
-1
Queue queue = new LinkedList();
queue.push(newTransaction);
setLatestTransaction(queue.pop());

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.