Alternative to Circular Array?

Now, i‘m not really looking for an alternative for a circular array, But for a System that works similar to it.

A circular array is basically a normal array, that works like [i%array.length] (if i got that right…). What that means is, it repeats itself indefenitely(literally, without becoming bigger, since it just calculates the index to be „resized“. So if it has a length of 10 and you look for Index 103, it will give Index 3).

But what i‘m looking for is an array that works similar, But not quite. What i Need is a fast way to indefinetly run through a Single array and once i got to an Element and worked with it, delete it and set a new one to the end of the array.

Like stacking blocks, Taking the bottommost and putting it on top, after changing it, or Playing with it, then the Rest falls down, and you restart at the bottom(best example i had, Hope it explains it).
Now, Although a circular array Would work for the take bottommost and then go to the Next and repeat it, the Problem is, that it doesn‘t allow me to not get a result, or get 2 results…

Since i Would replace the just selected Index with my result, if the result is 0, then i can‘t work with it anymore and so can‘t get a new one in there. Now if i get 2, i also can‘t just place the Second result in an array higher or Lower than the current Index, because there are other Elements in those indexes, unless they resulted in a null before, But i can‘t rely on that, and i don‘t Even want that to happen…

Now, to avoid this, i could indeed just place each thing after the null index to index-1, to have that Index occupied, But that could result in a lot of operations (the array can reach basically as high as it can handle, so ~a million). And Same for adding an element, Would have to do Index+1 for each afterwards… The only place where this wouldn‘t matter is the last Index…

Now my question, is there something like this? I know it shouldn‘t work, But maybe there is a way i don‘t know, or Even a Special external Device that works like that?

Do you mind adding some linebreaks to your post? Most people find it really hard to read a “wall of text” like this. It’s much easier if you break it up into multiple paragraphs. That will make it much easier for people to help you!

1 Like

Would these sketch example links below be the kinda circular arrays you’re asking about? :man_shrugging:

  1. http://Studio.ProcessingTogether.com/sp/pad/export/ro.90vdKMfkiO$zf
  2. http://Studio.ProcessingTogether.com/sp/pad/export/ro.9ldYvJUyiXGzi
  3. http://Studio.ProcessingTogether.com/sp/pad/export/ro.9GTDpA6dp4tH1
1 Like

Sorry :sweat_smile: i often don‘t realize how much i write.

They look like what i‘m looking for, but i don‘t quite get the Syntax… is it like… no, i just don‘t get it… But that should be what i‘m looking for. Thanks.
Edit: what is that Type of loop called? To find more about it :sweat_smile:

Actually, sorry, i took a closer Look, and noticed that it‘s not what i meant. I was looking at the for loop and got confused by the NUm variables :sweat_smile:, which i didn‘t quite get, But now i got it and realize, that it‘s just like telling the loop i < array.length.(Although i didn‘t know this way to use it before :blush:)

I am sure that you fully understand the problem but I can’t make head or tail of it from your description. You start out talking about a circular array because you are continuously iterating over the elements but then you talk about removing and inserting elements on a regular basis and an array is not the best data structure for that.

It seems to me you should be considering something like a linked list but without a clearer idea of what you are trying to do I can’t be sure.

Why not try and describe what you want to do with the data without mentioning circular arrays or arrays in general.

Yeah, sorry, i’m not a Great explainer :sweat_smile: i‘ll try to explain it better, and it‘s actually quite simple, just maybe not in code. :sweat_smile:

What i Need to do, is iterate indefinetly over many values. Then, for each value, i Need to run it through some code. This will return me either 0 or more values for this one value.

If the value is 0, i can‘t work with the Element any more, and Need to remove it from this loop. If it is 1, i can just replace it and everythings Fine. And if it’s more, like 2 values, i Need to replace the one i made my calculation with with the first and then put the others after it, But ignoring them for this Turn.

Basically exactly like a Tower where you pick the bottom Stone and place it on top, forever. Just, that if the Stone is Red, you don‘t put it back, if it‘s Green, it has a number on it, and you add as many as the number, and if it‘s Blue, you just put one ontop…

It sounds like a FIFO structure (First In First Out). You can only access the first element and you can only add new elements at the end.

For your case, you would simply need a while loop that check if it is not empty and if not it takes the first element and you can perform all the tests you described.

2 Likes

The Tower description sounds like you are looking for a data structure called Queue or FIFO (as @jb4x already pointed out). Try LinkedList or ArrayDeque which implement Queue: they have poll() to remove elements form the front and offer(e) to insert elements to the back.

Another idea would be to have two arrays. Read numbers from one array from first to last. Write new numbers into the second array as needed. When you read all the way to the end of the first array, clear it and swap the arrays. Read array becomes write array and write array becomes read array.

Basically: process one array into another array, swap arrays, repeat.

This will be much easier if your “arrays” are a bit clever: you can use either ArrayList<Integer> + add(), or IntList + append() (which might be faster because working with Integers has some overhead).

2 Likes

Thanks a lot :blush:
I looked for fifo structures and found the Java.util.queue. That should do what i need, though i‘ll still check the source code of there aren‘t any parts where the whole array gets used in any way (not that it is just a normal array that iterates over every Element and puts them one forward. Cause my array will get way to Long for that^^). But it Sounds like what i need.

1 Like

Thanks :blush:
The Queue should be what i was looking for (hopefully). Though the 2 Array approach is not usable in my case, since i‘d have to iterate over every Element to swap arrays + some other things that all cost Time. And with an array the size of ~ a million(Or Even a lot more), Thats 2 seconds for a Full iteration… Though doable, it still would probably take aeons longer ^^.

If you are trying to get the best performance, use ArrayDeque or arrays: running a million elements through it will take only a few milliseconds. It is what you do with the elements that will take most of the time.

2 Likes

Thanks ^^ then i‘ll try that one. The faster the better :smile:

1 Like