Arrays.asList分析

2385 ワード

詳細
問題1:
コードは次のとおりです.
 
    int [] arr = new int[5];
    arr[0]=1;
    arr[1]=2;
    arr[2]=3;
    arr[3]=4;
    arr[4]=5;
    List list  =  Arrays.asList(arr);
    list.remove(0);

実行後:
 
 
Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.remove(AbstractList.java:144)
	at test.testArr23.main(testArr23.java:18)

原因分析
 
 
Arrays.asList    List    Arrays         : private static class ArrayList extends AbstractList

最も簡単に実現されるインタフェースと抽象クラスだけで、
AbstractList             UnsupportedOperationException.
    :
 public E set(int index, E element) {
	throw new UnsupportedOperationException();
  }



  public void add(int index, E element) {
	throw new UnsupportedOperationException();
  }
    public E remove(int index) {
	throw new UnsupportedOperationException();
  }
    :
    :
int[] arr = new int[5];
    arr[0] = 1;
    arr[1] = 2;
    arr[2] = 3;
    arr[3] = 4;
    arr[4] = 5;
    String[] strArr = new String[3];
    strArr[0] = "11";
    strArr[1] = "22";
    strArr[2] = "33";

List strlist = Arrays.asList(strArr); List arrlist = Arrays.asList(arr);

for (String j : strlist) { System.out.println(j); } for (int j : arrlist.get(0)) { System.out.println(j); }