0

I need to store data in a string array, where I can store thousands of elements, cycle through them continually, but then delete the oldest and add more to it continually while deleting the oldest.

What data structure would be best in terms of access and memory use?

Also, I need 10 of these (essentially a 2-dimensional) and set the size of the array (number of elements in each), how would I structure the initialization for it?

I'm currently using this, but I doubt it's best suited and I can't figure out how to set the number of elements in each:

private List[] chanValues = new List[10];

Thanks.

MartinH
  • 33
  • 3
  • Will it support random access? – shanfeng Jul 22 '22 at 01:23
  • Lists contain as many elements as you put in them. You can't "set the number of elements," you just have to add the elements in (maybe nulls). – Louis Wasserman Jul 22 '22 at 01:29
  • I'll need to read through the array sequentially to pull the data out each cycle. – MartinH Jul 22 '22 at 01:30
  • 1
    That said, it sounds like what you want is just an [ArrayDeque](https://docs.oracle.com/javase/9/docs/api/java/util/ArrayDeque.html). – Louis Wasserman Jul 22 '22 at 01:30
  • I don't see a method to read through the elements for an ArrayDeque, so I guess that's what you implied by random access. I need to read through the elements continually. Thanks. – MartinH Jul 22 '22 at 01:43
  • @AlexReinking, thanks, don't need a ring, just need to remove the first while adding to the end. The List works, I'm just not sure if it's the best for memory usage? Maybe it is? – MartinH Jul 22 '22 at 01:58
  • @MartinH ArrayDeque.iterator() will give you an iterator which you can traverse the elements. – Cheng Thao Jul 22 '22 at 02:24
  • @ChengThao, you mean to use the forEach? That could work. Thanks, understand now. – MartinH Jul 22 '22 at 03:05
  • You want *either* a String array *or* another data structure. Not both at the same time. Please clarify. – user207421 Jul 22 '22 at 05:32
  • @user207421, yes one or the other. Upon reflection of the code, at times I do need to randomly access it as I skip chunks of data based on the situation, so the List structure seems best. Thanks. – MartinH Jul 22 '22 at 16:31

0 Answers0