-1

I am trying to create an empty ADT Bag that can hold up to 100 items.

public class ListArrayListBased implements ListInterface {

   private ArrayList<Object> data;

   public ListArrayListBased() {
        this.data = new ArrayList<Object>();

How do I make an ArrayList limited to 100 objects?

WJS
  • 36,363
  • 4
  • 24
  • 39
  • new ArrayList(100); – towhid Jun 24 '20 at 21:16
  • 4
    @towhid this only sets the initial capacity, it is not a limit – Arnaud Claudel Jun 24 '20 at 21:17
  • 1
    You can create an array (`new Object[100]`) and then use `Arrays.asList` to make a `List` that can't be modified – user Jun 24 '20 at 21:18
  • 2
    You can't limit the size of the list. But you can pre-fill a list and create an unmodifiable collection after you've filled your list by invoking [`Collections.unmodifiableList()`](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Collections.html#unmodifiableList(java.util.List)) – Janez Kuhar Jun 24 '20 at 21:19
  • 1
    ArrayList is not meant to have a limit. You should rather wrap the ArrayList in a class (`ADTBag` for example) and implement yourself that logic – Arnaud Claudel Jun 24 '20 at 21:19
  • It's a possibly a duplicate of https://stackoverflow.com/questions/8969805/arraylist-limit-to-hold-10-values/8969931 – Juninho Cruz Jun 24 '20 at 21:49
  • 1
    What is an “ADT Bag”, and why did you mention that? – Basil Bourque Jun 25 '20 at 06:07

3 Answers3

3

You can't limit the size of an ArrayList directly, but since it's not exposed and can only be added to through the ListArrayListBased's methods, you can check the size explicitly. e.g.:

public void add(Object o) {
    if (data.size() >= 100) {
        throw new IllegalArgumentException("data is full");
    }
    data.add(o);
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

You could override the add method. There may be other methods that also need to be overriden. I believe though that addAll calls add.

List<Integer> list = new ArrayList<>() {
    @Override
    public boolean add(Integer v) {
        if(size() < 10) {
            return super.add(v);
        }
        return false;
    }
};

for (int i = 0; i< 11; i++) {
    System.out.println(list.add(i));
}

Prints

true
true
true
true
true
true
true
true
true
true
false
WJS
  • 36,363
  • 4
  • 24
  • 39
  • Just checked the source code (should have done it sooner). Apparently, the current implementation of `ArrayList.addAll` does not call `add`. – WJS Jun 24 '20 at 22:47
  • 3
    There are four add methods, `add(Integer)`, `add(int,Integer)`, `addAll(Collection extends Integer>)`, and `addAll(int index, Collection extends Integer>)` and there’s no guaranty that either calls the other. Further, there’s `list.listIterator().add(…)` which currently does delegate to `list.add(index, element)`, but this might change. There’s also the possibility that future versions of `ArrayList` offer additional ways to add elements. That’s why you should extend classes designed for extension, i.e. `AbstractList` guarantees that all add operations use `add(int index, E element)`. – Holger Jun 25 '20 at 10:25
1

List.of( array )

You can pass an array to List.of resulting in a non-modifiable List with a fixed size of the array’s size. Elements cannot be added, removed, or replaced.

Object[] arr = new Object[100] ;
… populate array 
List< Object > objs = List.of( arr ) ;

Circular buffer (a.k.a. cyclic buffer, or ring buffer)

Or, perhaps you want a non-blocking queue which automatically evicts elements from the head of the queue when attempting to add new elements onto the queue and it is full.

If so, see my Answer on the Question, Is there a fixed sized queue which removes excessive elements?. There I mention EvictingQueue from Google Guava, and CircularFifoQueue from Apache Commons.

To instantiate an EvictingQueue call the static factory method create and specify your maximum size.

EvictingQueue< Person > people = com.google.common.collect.EvictingQueue.create( 100 ) ;  // Set maximum size to 100. 
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154