i want to limit the arraylist to 50. but i dont know how.
here’s my code
Object1.java
public class Object1 {
public String seat_no, id;
public Object1(String seat_no, String id) {
this.seat_no = seat_no;
this.id = id;
}
}
main.java
private ArrayList<Object1> list;
list = new ArrayList<Object1>();
As tempting as it may sound, I’d advise against extending
ArrayListor any otherListimplementation, because then you’ll be limiting yourself to using only one type of list. What happens if next time you want to limit aLinkedListand not anArrayList? would you extendLinkedListtoo? what happens if you want to limit, basically, an arbitrary implementation ofList?What you’re looking for already exists. You can use Commons Collections and wrap a list with a predicate:
Using the design above, you can use the same
Predicateinstance to limit any sort of list. You can also extend it to handle any sort of collection…… Without the need to extend any JDK class.