Circular functionality in Java Data Structure

Discussion in 'Java' started by Proton Projects - Moin, Mar 6, 2007.

  1. Hi all,

    I need to have a circular functionality.

    If i construct a datastructure for size 2000.....then if i try to add
    the 2001th object...it should automatically remove the first element
    and adds the 2001th element to retain the size to 2000.

    Can anybody help me in this regard
    Thanks
    Moin
    Proton Projects - Moin, Mar 6, 2007
    #1
    1. Advertising

  2. Proton Projects - Moin wrote:
    > Hi all,
    >
    > I need to have a circular functionality.
    >
    > If i construct a datastructure for size 2000.....then if i try to add
    > the 2001th object...it should automatically remove the first element
    > and adds the 2001th element to retain the size to 2000.
    >
    > Can anybody help me in this regard
    > Thanks
    > Moin
    >


    Look at LinkedBlockingDeque. You will have to implement the removal of
    the first element but that should be too complicated.

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Mar 6, 2007
    #2
    1. Advertising

  3. Proton Projects - Moin

    hvt Guest

    On Mar 6, 8:49 am, "Proton Projects - Moin" <>
    wrote:
    > Hi all,
    >
    > I need to have a circular functionality.
    >
    > If i construct a datastructure for size 2000.....then if i try to add
    > the 2001th object...it should automatically remove the first element
    > and adds the 2001th element to retain the size to 2000.
    >
    > Can anybody help me in this regard
    > Thanks
    > Moin


    Hi Moin,
    your required functionality can be obtained with some logic on arrays
    or lists. Find the below code which implements the above logic with
    array list:

    import java.util.ArrayList;

    public class FixQueue<T> {

    public static final int QUEUE_SIZE = 2000; //Fixing the Queue Size
    private ArrayList fixQueue;

    public FixQueue(int queue_size2) {
    fixQueue = new ArrayList<String>(QUEUE_SIZE);
    }

    public void add(String element){

    if(fixQueue.size() == QUEUE_SIZE)
    fixQueue.remove(0); // Remove first element when queue is full

    //adds the element in the last position,
    //when queue is full, the elements will be added at
    2000th position
    fixQueue.add(element);
    }

    public String toString(){
    return fixQueue.toString();
    }

    public static void main(String[] args) {
    FixQueue<String> queue = new FixQueue<String>(QUEUE_SIZE);
    for(int i=1; i<=2010; i++)
    queue.add("e"+i);

    System.out.println(queue);
    }
    }
    hvt, Mar 6, 2007
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Proton Projects - Moin schreef:
    > Hi all,
    >
    > I need to have a circular functionality.
    >
    > If i construct a datastructure for size 2000.....then if i try to add
    > the 2001th object...it should automatically remove the first element
    > and adds the 2001th element to retain the size to 2000.


    Have a look at
    http://jakarta.apache.org/commons/c...commons/collections/buffer/BoundedBuffer.html
    and the Buffer package in general.

    If you want a version that uses generics, contact me.

    H.
    - --
    Hendrik Maryns
    http://tcl.sfs.uni-tuebingen.de/~hendrik/
    ==================
    http://aouw.org
    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)

    iD8DBQFF7YNFe+7xMGD3itQRArr2AJ0bzl3FtgpsHIGzok7DHncuCOoq1QCfaRQu
    hAmNIuhlV9mvcVnBzpSNBPU=
    =rvta
    -----END PGP SIGNATURE-----
    Hendrik Maryns, Mar 6, 2007
    #4
  5. On Mar 6, 8:05 pm, Hendrik Maryns <>
    wrote:
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > Proton Projects - Moin schreef:
    >
    > > Hi all,

    >
    > > I need to have a circular functionality.

    >
    > > If i construct a datastructure for size 2000.....then if i try to add
    > > the 2001th object...it should automatically remove the first element
    > > and adds the 2001th element to retain the size to 2000.

    >
    > Have a look athttp://jakarta.apache.org/commons/collections/apidocs/org/apache/comm...
    > and the Buffer package in general.
    >
    > If you want a version that uses generics, contact me.
    >
    > H.
    > - --
    > Hendrik Marynshttp://tcl.sfs.uni-tuebingen.de/~hendrik/
    > ==================http://aouw.org
    > Ask smart questions, get good answers:http://www.catb.org/~esr/faqs/smart-questions.html
    > -----BEGIN PGP SIGNATURE-----
    > Version: GnuPG v1.4.2 (GNU/Linux)
    >
    > iD8DBQFF7YNFe+7xMGD3itQRArr2AJ0bzl3FtgpsHIGzok7DHncuCOoq1QCfaRQu
    > hAmNIuhlV9mvcVnBzpSNBPU=
    > =rvta
    > -----END PGP SIGNATURE-----


    Thanks all for your valuable suggestions
    Moin
    Proton Projects - Moin, Mar 7, 2007
    #5
  6. Proton Projects - Moin

    Karl Uppiano Guest

    "Proton Projects - Moin" <> wrote in message
    news:...
    > Hi all,
    >
    > I need to have a circular functionality.
    >
    > If i construct a datastructure for size 2000.....then if i try to add
    > the 2001th object...it should automatically remove the first element
    > and adds the 2001th element to retain the size to 2000.


    Historically, these things have been implemented as "ring buffers": An array
    of the appropriate size and type, with an index that is incremented modulo
    the array size whenever an element is added. The compute overhead with this
    type of extremely low, and no conditional logic is required at all. The
    newest element simply replaces the oldest element at the insertion point.

    http://en.wikipedia.org/wiki/Ring_buffer
    Karl Uppiano, Mar 7, 2007
    #6
  7. Proton Projects - Moin

    Karl Uppiano Guest

    "Karl Uppiano" <> wrote in message
    news:eek:YrHh.1501$Ih.445@trnddc02...
    >
    > "Proton Projects - Moin" <> wrote in message
    > news:...
    >> Hi all,
    >>
    >> I need to have a circular functionality.
    >>
    >> If i construct a datastructure for size 2000.....then if i try to add
    >> the 2001th object...it should automatically remove the first element
    >> and adds the 2001th element to retain the size to 2000.

    >
    > Historically, these things have been implemented as "ring buffers": An
    > array of the appropriate size and type, with an index that is incremented
    > modulo the array size whenever an element is added. The compute overhead
    > with this type of extremely low, and no conditional logic is required at
    > all. The newest element simply replaces the oldest element at the
    > insertion point.
    >
    > http://en.wikipedia.org/wiki/Ring_buffer


    Sorry, my proofreader fell asleep. That should read: ... The compute
    overhead with this approach is extremely low. ...
    Karl Uppiano, Mar 7, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kiuhnm
    Replies:
    16
    Views:
    741
    Jonathan Mcdougall
    Jan 3, 2005
  2. herrcho

    Circular Queue data structure ..

    herrcho, Oct 1, 2003, in forum: C Programming
    Replies:
    6
    Views:
    20,527
    BruceS
    Oct 2, 2003
  3. Bob Jenkins

    name of data structure for circular array?

    Bob Jenkins, Jun 29, 2004, in forum: C Programming
    Replies:
    2
    Views:
    370
    Thomas Matthews
    Jun 29, 2004
  4. T Koster

    Circular (triangular) structure reference

    T Koster, Feb 8, 2005, in forum: C Programming
    Replies:
    6
    Views:
    373
    infobahn
    Feb 8, 2005
  5. A
    Replies:
    27
    Views:
    1,596
    Jorgen Grahn
    Apr 17, 2011
Loading...

Share This Page