Results 1 to 20 of 21
- 12-31-2011, 12:19 AM #1
Member
- Join Date
- May 2011
- Posts
- 56
- Rep Power
- 0
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?
- 12-31-2011, 12:55 AM #2
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.
- 12-31-2011, 05:57 AM #3
Re: How to create a fixed size ArrayList with few functionalities as follows..
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.it holds only LATEST 10 objects entered
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
- 12-31-2011, 09:18 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
-
- 12-31-2011, 03:04 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: How to create a fixed size ArrayList with few functionalities as follows..
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? ;-)When people rob a bank they get a penalty; when banks rob people they get a bonus.
-
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.
- 12-31-2011, 03:50 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: How to create a fixed size ArrayList with few functionalities as follows..
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-31-2011, 05:49 PM #9
Member
- Join Date
- May 2011
- Posts
- 56
- Rep Power
- 0
Re: How to create a fixed size ArrayList with few functionalities as follows..
Thanks everyone, and queue implementation does the work.. :)
- 12-31-2011, 05:51 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 01-01-2012, 06:12 PM #11
Member
- Join Date
- May 2011
- Posts
- 56
- Rep Power
- 0
Re: How to create a fixed size ArrayList with few functionalities as follows..
My implementation:
Here is the reference.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; } }
Any improvements in this?
One thing is, i can also use
in place ofJava Code:while (size() > maxSize)
Java Code:if (size() > maxSize)
Last edited by asifzbaig; 01-01-2012 at 06:16 PM. Reason: Code Improvement
- 01-01-2012, 06:15 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-01-2012, 06:27 PM #13
Member
- Join Date
- May 2011
- Posts
- 56
- Rep Power
- 0
Re: How to create a fixed size ArrayList with few functionalities as follows..
Its the outcome that matters Sir. Isn't it?It is not an ArrayList (see your own topic title).
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
- 01-01-2012, 07:49 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-02-2012, 11:56 PM #15
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
- 01-03-2012, 07:46 AM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 01-03-2012, 08:05 AM #17
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.
- 01-03-2012, 01:44 PM #18
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: How to create a fixed size ArrayList with few functionalities as follows..
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-03-2012, 02:09 PM #19
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:
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 plausibleJava 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); } }
but any feedback is welcome. perhaps inside MyArrayList a Queue would be more suitable for this problem.[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
[4, 5, 6]Last edited by j2me64; 01-03-2012 at 02:17 PM.
- 01-03-2012, 02:47 PM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,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); }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
ArrayList size error
By wepwep in forum New To JavaReplies: 9Last Post: 03-29-2011, 09:57 AM -
Read Barcode from Specific Area of Img & Create Fixed Size Barcode
By sherazam in forum Java SoftwareReplies: 0Last Post: 03-01-2011, 07:57 AM -
How to correctly use a fixed size thread pool?
By johann_p in forum Threads and SynchronizationReplies: 1Last Post: 09-19-2010, 02:04 AM -
ArrayList pointer size java
By senorbum in forum Advanced JavaReplies: 6Last Post: 05-20-2010, 09:44 AM -
Limiting size of ArrayList
By ravian in forum New To JavaReplies: 3Last Post: 01-29-2008, 06:37 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks