0

I have a 'double' array that has a length of 10. When I first initiate the array, all the values inside are '0.0' until I start replacing them. Once I replace all the values with something besides 0, how would I go about making it so if I decided to 'add' another number to the array, it would start back at the beginning at index 0 and do the process over again, replacing the old numbers in order.

Working on a Bank Account transaction program. So, you have an Array of 10 transactions, all starting at 0.0. Then as you start to withdraw or deposit money, it "logs" into each slot the amount withdrawn/deposited. Once you do your 11th transaction, it is suppose to go back and replace the first transaction, the 12th replaces the 2nd, etc.

Edit: Not allowed to use Array Lists. Must be a normal static-lengthed Array.

2
  • 1
    Could you provide an example of what you're trying to do? I find the current explanation rather unclear. Commented Feb 8, 2012 at 16:35
  • Working on a Bank Account transaction program. So, you have an Array of 10 transactions, all starting at 0.0. Then as you start to withdraw or deposit money, it "logs" into each slot the amount withdrawn/deposited. Once you do your 11th transaction, it is suppose to go back and replace the first transaction, the 12th replaces the 2nd, etc. Commented Feb 8, 2012 at 16:45

2 Answers 2

2

This is a simple modulus problem. Mathy solution would look as follows

 Wrap = currentArrayPos % arrayLength - 1 //need this since arrays start at 0 and length starts counting at 1.
 If Wrap == 1 --> currentArrayPos = 0
 Else --> currentArrayPos = currentArrayPos + 1

As you replace values, you iterate to the next array position and replace the value. When you've replaced the last value in the array, the modulus tells you to loop back to the front and replace the first value.

Easy Java solution would be:

for(int i = 0; i < array.length; i++) { 
//logic for calculating new values and exit condition goes here. 
    array[i] = newValue
    if(i ==> array.length - 1)
        i = 0 //resets the loop to keep going

    if(exitCondition) 
        break; //if there's some criteria, get out.  }

If it's not a constantly recurring thing, then you can just keep track of the last place you replaced in the array and do the modulus thing to determine if you need to reset or not.

mre's suggestion of using a list would also be worth looking into as well, but you would need to track positions.

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

Comments

0

If "i" is the array index you increment after each writing, an easy way to wrap around the array when you come to its edge is to use the modulo operator :

int index = 0;
int arraySize = 10;
long[] data = new long[arraySize];

for (int i=0; i<100; i++) {
    data[ index++ % arraySize ] = i; // or some other value
}

This way, your array will be filled in a circular way, always beginning at index 0 again after reaching the array's boundary.

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.