Javaと情熱のあいだ

カステラとドーナツと珈琲

変更(set)しかできないListのサンプル

通常のListは、追加(add)、削除(remove)、変更(set)の処理を操作することがで
きますが。
FixedSizeListは変更(set)以外の処理を実行した場合例外処理とします。
あんまり使い道は無いかも。
材料はこちら
org.apache.commons.collections

    /**
     *
     * 実行。
     * @throws Exception 例外
     */
    public void execute() throws Exception {
        final List list =
FixedSizeList.decorate(Arrays.asList("TEST_00","TEST_01"));

        System.out.println(list.get(0));

        // 変更
        list.set(0, "TEST_02");

        System.out.println(list.get(0));

        try {
            // 追加
            list.add("TEST_03");
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            // 削除
            list.remove(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

実行結果

TEST_02
java.lang.UnsupportedOperationException: List is fixed size
	at
org.apache.commons.collections.list.FixedSizeList.add(FixedSizeList.java:71)
	at
main.java.collections.FixedSizeListExample.execute(FixedSizeListExample.java:55)
	at main.java.example.Example.execute(Example.java:54)
	at main.java.example.Example.main(Example.java:44)
java.lang.UnsupportedOperationException: List is fixed size
	at
org.apache.commons.collections.list.FixedSizeList.remove(FixedSizeList.java:115)
	at
main.java.collections.FixedSizeListExample.execute(FixedSizeListExample.java:61)
	at main.java.example.Example.execute(Example.java:54)
	at main.java.example.Example.main(Example.java:44)