Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a fixed-size list in Java

Is it possible to define a list with a fixed size that's 100? If not why isn't this available in Java?

like image 837
fastcodejava Avatar asked Mar 05 '11 22:03

fastcodejava


People also ask

How do you declare a list size in Java?

The java. util. ArrayList. size() method returns the number of elements in this list i.e the size of the list.

Can we set the size of list in Java?

To create an ArrayList of specific size, you can pass the size as argument to ArrayList constructor while creating the new ArrayList. Following the syntax to create an ArrayList with specific size. myList = new ArrayList<T>(N);

Is ArrayList fixed in Java?

ArrayList's size and capacity are not fixed. This is managed separately from its physical storage size.

What is the size of list in Java?

The size() method of the List interface in Java is used to get the number of elements in this list. That is, this method returns the count of elements present in this list container. Parameters: This method does not take any parameters. Return Value: This method returns the number of elements in this list.


2 Answers

This should do it if memory serves:

List<MyType> fixed = Arrays.asList(new MyType[100]); 
like image 185
McDowell Avatar answered Oct 01 '22 12:10

McDowell


A Java list is a collection of objects ... the elements of a list. The size of the list is the number of elements in that list. If you want that size to be fixed, that means that you cannot either add or remove elements, because adding or removing elements would violate your "fixed size" constraint.

The simplest way to implement a "fixed sized" list (if that is really what you want!) is to put the elements into an array and then Arrays.asList(array) to create the list wrapper. The wrapper will allow you to do operations like get and set, but the add and remove operations will throw exceptions.

And if you want to create a fixed-sized wrapper for an existing list, then you could use the Apache commons FixedSizeList class. But note that this wrapper can't stop something else changing the size of the original list, and if that happens the wrapped list will presumably reflect those changes.


On the other hand, if you really want a list type with a fixed limit (or limits) on its size, then you'll need to create your own List class to implement this. For example, you could create a wrapper class that implements the relevant checks in the various add / addAll and remove / removeAll / retainAll operations. (And in the iterator remove methods if they are supported.)

So why doesn't the Java Collections framework implement these? Here's why I think so:

  1. Use-cases that need this are rare.
  2. The use-cases where this is needed, there are different requirements on what to do when an operation tries to break the limits; e.g. throw exception, ignore operation, discard some other element to make space.
  3. A list implementation with limits could be problematic for helper methods; e.g. Collections.sort.
like image 37
Stephen C Avatar answered Oct 01 '22 13:10

Stephen C