Reputation: 1
I would like to make something where the strings will move up in an orderly fashion. For example:
Iteration 1:
Hello There
I am
Asking a question
To you
Iteration 2:
I am
Asking a question
To you
Next String
How exactly would I go about this, in the least memory-intensive way? Thanks.
Upvotes: 0
Views: 403
Reputation: 86391
An easy way is a circular queue.
A circular queue can be implemented as an array and a pointer to the first index. When you want to change the first element, you need only advance the index. When the index passes the end of the array, it rolls back to index 0.
With a circular queue:
Upvotes: 1
Reputation: 31
If you're not obliged to use an array i would suggest a queue, with it its fairly simple to implement what you wanna do.
Queue<String> foo = new Queue<String>();
foo.offer("Hello"); //first element is hello
foo.offer("world"); //second element is world
String s = foo.poll(); //s = hello and now the first element of the queue is world
Upvotes: 0