Cyclic array?

J

jimi_usenet

I need a special kind of collection, and I think what I need is a so
called cyclic array. But with some modifications.

I want to be able to set a max size. I want to set and get values at
certain indexes. And if I set a value at an index so that there is a
gap between this index and the last index, I want it to "pad" with
copies of the value of the last index. And I want to be able to insert
values anyware in the the collection and make it the last value.

Example:

I create a collection with the max size 5. I then do this:
myCollection.set(0, a);
myCollection.set(1, b);
myCollection.set(2, c);
myCollection.set(4, e);
Then the collection should look like this: {a, b, c, c, e}

If I then do this:
myCollection.setLast(0, f);
then I want the collection to look like this: {b, c, c, e, f}

Is there such a collection available in java 5.0? Or maybe some third
party package, tested and bug free? Because I know I can write this
myself, but it takes time and probably can contain bugs that I don't
see. And I hate to reinvent the wheel.

Regards
/Jimi
 
P

P.Hill

it takes time and probably can contain bugs that I don't
see. And I hate to reinvent the wheel.

I can't help you with the first or thrid, but the second is
handled well by using unit testing and TDD. Then you
are not required to see bugs just state what the API does
and fix it when it doesn't do what you have defined that
it should do.

-Paul
 
Z

zero

I need a special kind of collection, and I think what I need is a so
called cyclic array. But with some modifications.

I want to be able to set a max size. I want to set and get values at
certain indexes. And if I set a value at an index so that there is a
gap between this index and the last index, I want it to "pad" with
copies of the value of the last index. And I want to be able to insert
values anyware in the the collection and make it the last value.

Example:

I create a collection with the max size 5. I then do this:
myCollection.set(0, a);
myCollection.set(1, b);
myCollection.set(2, c);
myCollection.set(4, e);
Then the collection should look like this: {a, b, c, c, e}

If I then do this:
myCollection.setLast(0, f);
then I want the collection to look like this: {b, c, c, e, f}

Is there such a collection available in java 5.0? Or maybe some third
party package, tested and bug free? Because I know I can write this
myself, but it takes time and probably can contain bugs that I don't
see. And I hate to reinvent the wheel.

Regards
/Jimi

your requirements are quite specific, so I don't think you'll find
something that'll work out-of-the-box. Try looking for a circular
buffer implementation, and subclass or edit it to make it follow your
requirements.

One possible starting point is java.util.LinkedHashMap, where you can
override removeEldestEntry(Map.Entry<K,V>) to make it act as a circular
buffer. The JavaDoc has an example which I believe fits your maximum
size idiom.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top