Best way to deal with infinite elements?

Hello,

When I use some type of element that can be infinite (games that only finish when the player dies) I always put everything inside of two Arrays. So, when the first Array has more than X elements I start to fill the second Array, when the second Array has more than X elements I clean the first Array and start to fill again.

I can save memory for not renderer a lot of stuff and only renderer the lasts. In games with automatically movement is good, because the elements will disappear with time every time I clean one of arrays, and the lasts will always renderer.

e.g: Array rectArray;
I can use this to control and renderer a lot of rectangles created automatically in the game and “clean” the old rectangles when the array is cleaned, but everytime I need to do a loop with for (i=0 ; i< array.size; i++) to verify collision and other things, so this can be bad for performance.

So, this is a good practice? Is there any other better way to deal with “infinite stuff” created in the game?

Here a small example of what I said before:


Array<Rectangle> listRec1 = new Array<Rectangle>();
Array<Rectangle> listRec2 = new Array<Rectangle>();
Rectangle playerRec = new Rectangle();

.....

render(float delta)
{
	if (currentTime - previousTime > 5) //5 seconds
	{
		if (listRec1.size <= 10)
		{
			listRec1.add(new Rectangle(anyX, anyY, anyW, anyH);
				if (listRec1.size == 9 && listRec2.size == 9)
				listRec2.clear; //clear listRec2.
		}
		else if (listRec2.size < 10)
		{
			listRec2.add(new Rectangle(anyX, anyY, anyW, anyH);
		}
		else if (listRec1.size <=10 && listRec2.size < 10)
		{
			listRec1.clear; //Clear listRec1
			listRec2.add(new Rectangle(anyX, anyY, anyW, anyH); //Add last Rec to listRec2
		}
	}			
		
	if (listRec1.size > 0)
		for (int i=0; i<listRec1.size; i++)
		{
			//code to draw all rectangle inside listRec1.
			//code to verify collision with player and Recs.
		}
		
	if (listRec2.size > 0)
			for (int i=0; i<listRec2.size; i++)
		{
			//code to draw all rectangle inside listRec2.
			//code to verify collision with player and Recs.
		}
}