Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Circular functionality in Java Data Structure

Reply
Thread Tools

Circular functionality in Java Data Structure

 
 
Proton Projects - Moin
Guest
Posts: n/a
 
      03-06-2007
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

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      03-06-2007
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/
 
Reply With Quote
 
 
 
 
hvt
Guest
Posts: n/a
 
      03-06-2007
On Mar 6, 8:49 am, "Proton Projects - Moin" <(E-Mail Removed)>
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);
}
}

 
Reply With Quote
 
Hendrik Maryns
Guest
Posts: n/a
 
      03-06-2007
-----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/co...dedBuffer.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+7xMGD3itQRArr2AJ0bzl3FtgpsHIGzok7DHn cuCOoq1QCfaRQu
hAmNIuhlV9mvcVnBzpSNBPU=
=rvta
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Proton Projects - Moin
Guest
Posts: n/a
 
      03-07-2007
On Mar 6, 8:05 pm, Hendrik Maryns <(E-Mail Removed)>
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+7xMGD3itQRArr2AJ0bzl3FtgpsHIGzok7DHn cuCOoq1QCfaRQu
> hAmNIuhlV9mvcVnBzpSNBPU=
> =rvta
> -----END PGP SIGNATURE-----


Thanks all for your valuable suggestions
Moin

 
Reply With Quote
 
Karl Uppiano
Guest
Posts: n/a
 
      03-07-2007

"Proton Projects - Moin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
Karl Uppiano
Guest
Posts: n/a
 
      03-07-2007

"Karl Uppiano" <(E-Mail Removed)> wrote in message
newsYrHh.1501$Ih.445@trnddc02...
>
> "Proton Projects - Moin" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>> 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. ...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple structure and copying data to pointer of the same structure A C++ 27 04-16-2011 11:07 PM
Circular (triangular) structure reference T Koster C Programming 6 02-08-2005 07:22 PM
Semi-circular definitions (plus circular references) Kiuhnm C++ 16 01-03-2005 03:49 AM
name of data structure for circular array? Bob Jenkins C Programming 2 06-29-2004 02:41 PM
Circular Queue data structure .. herrcho C Programming 6 10-02-2003 02:35 PM



Advertisments