This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have an integer array size of ten. Im trying to create a method that each time, will store in array the ten recent calculations. For example if i do 40 calculations in array i want to each time storing the 10 most recent. How i can achieve this?
If each time reset the counter, that's mean the next time i will add something, will go to array right? I looking something like if array is full and want to add one more, very first element deleted, move rest of elements one place up and and the new element at the last place.
You don't reset the counter to 0 each time ... just every 10th time.
The remainder operator lets you cycle through the array without checking the value of the counter and setting it back to 0 when it gets too big. That's because 10 % 10 is 0, 11 % 10 is 1 and so on. Any non-negative integer % 10 will be a an integer from 0 to 9, just like the indexes of your array.
If you want to shift the results every time, then loop thru the indexes from 1 to 9 and copy the array values to indexes 0 to 8, then put the new result in the array at index 9.