2015-10-04 25 views
0

這裏是拋出異常的strList5的subList.clear()調用的代碼:的Java 8 UnsupportedOperationException異常的subList.clear()

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

public static List<String> removeRange(List<String> strList, int   maxElementsToBeAllowedInList) { 
    if (strList.size() > maxElementsToBeAllowedInList) { 
     System.out.println("Clearing out sublist: [" 
       + strList.subList(maxElementsToBeAllowedInList, strList.size()) + "]"); 
     strList.subList(maxElementsToBeAllowedInList, strList.size()).clear(); 
    } 
    return strList; 
} 

public static void main(String[] args) { 
    List<String> strList1 = new ArrayList<>(); 
    List<String> strList2 = Arrays.asList("one", "two", "three"); 
    List<String> strList3 = Arrays.asList("one", "two", "three", "four", "five"); 
    List<String> strList4 = Arrays.asList("one", "two", "three", "four", "five", "six"); 
    List<String> strList5 = Arrays.asList("one", "two", "three", "four", "five", "six", "seven", "eight"); 

    try { 
     System.out.println("Before modification strList1: [" + strList1 + "]"); 
     ListTruncateEndTest.removeRange(strList1, 5); 
     System.out.println("After modification strList1: [" + strList1 + "]"); 

     System.out.println("Before modification strList2: [" + strList2 + "]"); 
     ListTruncateEndTest.removeRange(strList2, 5); 
     System.out.println("After modification strList1: [" + strList2 + "]"); 

     System.out.println("Before modification strList3: [" + strList3 + "]"); 
     ListTruncateEndTest.removeRange(strList3, 5); 
     System.out.println("After modification strList1: [" + strList3 + "]"); 

     System.out.println("Before modification strList4: [" + strList4 + "]"); 
     ListTruncateEndTest.removeRange(strList4, 5); 
     System.out.println("After modification strList4: [" + strList4 + "]"); 

     System.out.println("Before modification strList5: [" + strList5 + "]"); 
     ListTruncateEndTest.removeRange(strList5, 5); 
     System.out.println("After modification strList5: [" + strList5 + "]"); 
    } catch (Exception e) { 
     System.out.println("Error: [" + e.toString() + "]"); 
    } 
} 

Before modification strList1: [[]] 
After modification strList1: [[]] 
Before modification strList2: [[one, two, three]] 
After modification strList2: [[one, two, three]] 
Before modification strList3: [[one, two, three, four, five]] 
After modification strList3: [[one, two, three, four, five]] 
Before modification strList4: [[one, two, three, four, five, six]] 
Clearing out sublist: [[six]] 
Error: [java.lang.UnsupportedOperationException] 

是不是有什麼毛病我怎麼了清子列表?或者這是Java 8中的一個Bug?我的Java版本的Mac版本是: Java版本「1.8.0_45」 Java™SE運行時環境(版本1.8.0_45-b14) Java HotSpot™64位服務器VM(版本25.45-b02 ,混合模式)

+0

您可能還會考慮打印出異常'ex.printStackTrace()'的堆棧跟蹤,而不是僅顯示消息。 – hotzst

回答

2

Arrays.asList返回的列表是固定長度的,所以在subList上調用clear將拋出UnsupportedOperationException

您需要改用ArrayList

List<String> strList2 = new ArrayList<>(Arrays.asList("one", "two", "three")); 
+0

感謝Paul,當我使用新的ArrayList <>並添加了元素時,我沒有得到與您的答案匹配的錯誤。 U在我發現之前張貼了答案,所以你得到它的榮譽。 – user3688090

相關問題