Let’s say I have this array
int[] anArray = {
100, 200, 300};
Now, I want everytime my program execute it prints the next element in order.
For example, first time I execute java myProgram, the program print the first element “100”.
For second time I execute the program, it will print the second element “200”.
and so on.
But the forth time, it will repeat itself. and print the first element again.
I imagine, I could save the last element that I print. But I didn’t know how.
Any ideas?
As people here suggested, you should save in an external file the current index, and reset it when reaching the last index. Here is an example code that might help you:
When you write your code, you should add validation that the file exists etc.