package fixedsizelist.exemple; import java.util.Arrays; import java.util.List; import org.apache.commons.collections.list.FixedSizeList; public class FixedSizeListExemple { public static void main(String[] args) { List fixedList = FixedSizeList.decorate(Arrays.asList(new String[5])); fixedList.set(0, "[email protected]"); fixedList.set(1, "[email protected]"); fixedList.set(2, "[email protected]"); fixedList.set(3, "[email protected]"); fixedList.set(4, "[email protected]"); System.out.println("\tDisplay list values...\n"); for (String obj : fixedList) { System.out.println(obj.toString()); } try { System.out.println("\n\tTrying to modify our fixed list...\n"); fixedList.add("[email protected]"); fixedList.remove(3); fixedList.clear(); } catch (Exception e) { System.out.println (" The add, remove, clear and retain operations are unsupported." + "\nThe set method is allowed (as it doesn't change the list size).\n"); } } }
From http://e-blog-java.blogspot.com/2012/02/how-to-create-fixed-list-in-java-using.html
Comments
Adedayo Ominiyi replied on Fri, 2012/03/02 - 3:38am
Jean-Baptiste Nizet replied on Fri, 2012/03/02 - 1:19pm
in response to:
Adedayo Ominiyi
Neal Johnson replied on Sun, 2012/03/04 - 4:44am
Apache Collections does have some nice features but I find this one a little redundant. You can do exactly the same thing with standard collections and to some extent its a little bit more elegant: