Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Circular functionality in Java Data Structure

Thread view: 
Proton Projects - Moin - 06 Mar 2007 03:49 GMT
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
Knute Johnson - 06 Mar 2007 04:24 GMT
> Hi all,
>
[quoted text clipped - 7 lines]
> Thanks
> Moin

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

Signature

Knute Johnson
email s/nospam/knute/

hvt - 06 Mar 2007 09:15 GMT
On Mar 6, 8:49 am, "Proton Projects - Moin" <mohd.mohid...@gmail.com>
wrote:
> Hi all,
>
[quoted text clipped - 7 lines]
> 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);
    }
}
Hendrik Maryns - 06 Mar 2007 15:05 GMT
Proton Projects - Moin schreef:
> Hi all,
>
[quoted text clipped - 3 lines]
> 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/collections/apidocs/org/apache/commons/collect
ions/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
Proton Projects - Moin - 07 Mar 2007 03:21 GMT
On Mar 6, 8:05 pm, Hendrik Maryns <hendrik_mar...@despammed.com>
wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 26 lines]
> =rvta
> -----END PGP SIGNATURE-----

Thanks all for your valuable suggestions
Moin
Karl Uppiano - 07 Mar 2007 05:20 GMT
> Hi all,
>
[quoted text clipped - 3 lines]
> 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 - 07 Mar 2007 05:26 GMT
>> Hi all,
>>
[quoted text clipped - 12 lines]
>
> http://en.wikipedia.org/wiki/Ring_buffer

Sorry, my proofreader fell asleep. That should read: ... The compute
overhead with this approach is extremely low. ...


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.