Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree2Likes

Thread: How to create a fixed size ArrayList with few functionalities as follows..

  1. #1
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default How to create a fixed size ArrayList with few functionalities as follows..

    Hello everyone,
    This simple question rolling in my mind without a correct answer,
    I want to have a ArrayList with fixed size of 10,
    I know i can use array in this situation but they don't provide me methods like add(), set(), remove()..
    Now what i want from this particular ArrayList that it holds only LATEST 10 objects entered,
    more precisely i mean that that when the size of list reached to 10 than the LAST element must be discarded in favour of the NEW one.

    How can i achive this?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    Write your own class that extends ArrayList and make it work the way you want.
    Or write your own class using an array to hold the data.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    it holds only LATEST 10 objects entered
    Sounds like you want a stack. A stack will always keep the newest items at the top, and with some custom work, you can limit it's size and make it drop elements off the end. Implementing one from scratch really isn't difficult at all, and would really only involve about a page of code. For something this small, I would use a linked structure as opposed to an array based stack. That eliminates shuffling when you insert or remove elements.

    You could also subclass one of the java stack implementations, but that might actually be more work if you are unfamiliar with the code or if the code does a lot more than you need. If you need pointers on this, I can site you some pseudo code and method descriptions! :D

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    Quote Originally Posted by quad64bit View Post
    Sounds like you want a stack.
    A queue.

    kind regards,

    Josd
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    Quote Originally Posted by JosAH View Post
    A queue.

    kind regards,

    Josd
    You remind me of my school classes.

    I guess its a queue because its first-in first-out. and a stack would be first-in last-out.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    Quote Originally Posted by ozzyman View Post
    You remind me of my school classes.

    I guess its a queue because its first-in first-out. and a stack would be first-in last-out.
    Yep, and if you read the API documentation for the Queue interface you'll see that one of the implementing classes is the LinkedList class; it has a removeFirst/Last and an addFirst/Last method (so you can have a queue both ways). Before insering check its size() method and drop one of the elements if it contains the maximum number of elements already.

    kind regards,

    Jos

    ps. I don't look like one of your teachers do I? ;-)
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    Hah you wish josah. They were both like 20 years younger than you :p.

    But no it just reminded me of my a level lessons when we had to convert pascal to assembler and then trace the stack and we learnt the difference between a stack and a queue.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    Quote Originally Posted by ozzyman View Post
    Hah you wish josah. They were both like 20 years younger than you :p.
    I'm mentally six years old with no noticeable improvement, so I don't know what you're talking about ;-)

    kind regards,

    Jos
    ozzyman likes this.
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    Thanks everyone, and queue implementation does the work.. :)

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    Quote Originally Posted by asifzbaig View Post
    Thanks everyone, and queue implementation does the work.. :)
    Too bad an ArrayList (as per your requirements) doesn't implement the Queue interface.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    My implementation:

    Java Code:
    public class QueueList<E> extends LinkedList<E> {
        private int maxSize;
    
        public QueueList(int maxSize) {
            this.maxSize = maxSize;
        }
        
        @Override
        public boolean add(E e) {
            super.addFirst(e);
            if (size() > maxSize){
                removeLast();
            }
            return true;
        }
    }
    Here is the reference.

    Any improvements in this?

    One thing is, i can also use
    Java Code:
    while (size() > maxSize)
    in place of
    Java Code:
    if (size() > maxSize)
    Last edited by asifzbaig; 01-01-2012 at 06:16 PM. Reason: Code Improvement

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    It is not an ArrayList (see your own topic title).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    It is not an ArrayList (see your own topic title).
    Its the outcome that matters Sir. Isn't it?
    And from the above replies, i came to the conclusion that queue implementation with LinkedList is the correct way to go.
    That is a different thing that I CAN'T USE ANYTHING EXCEPT ArrayList, but here there is no such problem.. :)
    and i am here for your suggestions that help me to go to right way and it is what i got..


    P.S. : Sorry with the title, i'll take care of it next time

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    Ok, if your teacher is happe with a LinkedList, who am I to object?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    I know this is late, I was out of town, but I did in fact mean a stack. The newest item would always be on the top, and older items could drop off the bottom. :D

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    Quote Originally Posted by quad64bit View Post
    I know this is late, I was out of town, but I did in fact mean a stack. The newest item would always be on the top, and older items could drop off the bottom. :D
    That's a queue.

    kind regards,

    Jos
    ozzyman likes this.
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    Queue is FIFO meaning that if I were to insert 5 items, the first out would not be the newest item, but the oldest (the first enqueued). Stack is LIFO meaning the newest item inserted would be the first out. A stack would always give you the most recently inserted items first. If you were keeping a list of 10 newest, popping items off a stack would give them to you newest to oldest. If the stack were implemented with a linked structure, then any items pushed on beyond 10 (the OP's requirement) would also drop the tail element. The resulting list would once again be the 10 latest.

    With a queue, it would be similar, but you would be dropping the head, and would end up with the 10 newest items in reverse, oldest to newest.

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    Quote Originally Posted by quad64bit View Post
    Queue is FIFO meaning that if I were to insert 5 items, the first out would not be the newest item, but the oldest (the first enqueued). Stack is LIFO meaning the newest item inserted would be the first out. A stack would always give you the most recently inserted items first. If you were keeping a list of 10 newest, popping items off a stack would give them to you newest to oldest. If the stack were implemented with a linked structure, then any items pushed on beyond 10 (the OP's requirement) would also drop the tail element. The resulting list would once again be the 10 latest.

    With a queue, it would be similar, but you would be dropping the head, and would end up with the 10 newest items in reverse, oldest to newest.
    That is exactly what the OP wants:

    Quote Originally Posted by OP
    Now what i want from this particular ArrayList that it holds only LATEST 10 objects entered,
    New elements are pushed on one side and older elements pop off from the ther side; i.e. a queue.

    kind regards.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    i've implemented a generic class that should work for any object. the key point of the code is the list.remove(0) where the first element is removed and all elements are moved forward and the new element is added at the end, something like a FIFO aka queue. my test is poor: i added three elements 1, 2 and 3 to the arraylist and then i added 4, 5 and 6 to the list and after add() i prompted the content of the list. here is the code:

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    public class MyArrayList<T> {
    	private List<T> list;
    	private int maxSize;
    	
    	
    	public MyArrayList(int maxSize) {
    	
    	this.maxSize = maxSize;
    	list = new ArrayList<T>(maxSize);
    	}
    	
    	// something like a queue
    	public void add(T t) {
    		if (list.size() == maxSize) {
    		// remove first element
    		list.remove(0);
    		// add the new element at the very back
    		// maxSize is not zero based, so make it zero based by -1
    		  list.add(maxSize - 1, t);
    		} else if (list.size() < maxSize){
    			list.add(t);
    		}
    	}
    	
    	public int size() {
    		return list.size();
    	}
    	
    	public String toString() {
    		return list.toString();
    	}
    	
    	public static void main(String[] args) {
    		// the list can have 3 elements of type integer
    		MyArrayList<Integer> list = new MyArrayList<Integer>(3);
    		list.add(1);
    		list.add(2);
    		list.add(3);
    		System.out.println(list);
    		list.add(4);
    		System.out.println(list);
    		list.add(5);
    		System.out.println(list);
    		list.add(6);
    		System.out.println(list);
    	}
    }
    watch out: the class MyArrayList holds an ArrayList which has also methods like add(), so when you want to use the add() from the ArrayList use list.add(). the output is plausible

    [1, 2, 3]
    [2, 3, 4]
    [3, 4, 5]
    [4, 5, 6]
    but any feedback is welcome. perhaps inside MyArrayList a Queue would be more suitable for this problem.
    Last edited by j2me64; 01-03-2012 at 02:17 PM.

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create a fixed size ArrayList with few functionalities as follows..

    All that juggling in you add( ... ) method isn't necessary; the following will do fine:

    Java Code:
    public void add(T t) {        
       if (list.size() == maxSize)         // remove first element
            list.remove(0);        
       // add the new element at the very back
       list.add(t);        
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. ArrayList size error
    By wepwep in forum New To Java
    Replies: 9
    Last Post: 03-29-2011, 09:57 AM
  2. Replies: 0
    Last Post: 03-01-2011, 07:57 AM
  3. How to correctly use a fixed size thread pool?
    By johann_p in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-19-2010, 02:04 AM
  4. ArrayList pointer size java
    By senorbum in forum Advanced Java
    Replies: 6
    Last Post: 05-20-2010, 09:44 AM
  5. Limiting size of ArrayList
    By ravian in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 06:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •