[精讚] [會員登入]
689

Arrays.asList 後想再add出現 UnsupportedOperationException

這個問題真是搞死我了 List<String> fruits = Arrays.asList("a

分享此文連結 //n.sfs.tw/13240

分享連結 Arrays.asList 後想再add出現 UnsupportedOperationException@igogo
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-24 09:17:23 最後編修
2019-04-10 20:42:41 By igogo
 

 

這個問題真是搞死我了

 

 List<String> fruits = Arrays.asList("apple", "orange");
 fruits.forEach(fruit-> System.out.println(fruit));

原本這樣過得好好的,誰知客人想再多點

所以我很直覺的操作

fruits.add("water");

杯具就發生了

Caused by: java.lang.UnsupportedOperationException: null

搞了半天, 才看到這一句, 上網查了一下

原來是Arrays.asList() 回傳是固定值  不能再增刪

https://stackoverflow.com/questions/30174623/arrays-aslist-give-unsupportedoperationexception

但是客人喜歡怎麼辦? 改這樣處理

List<String> fruits = new ArrayList<>();
Arrays.asList("apple", "orange").forEach(fruit->fruits.add(fruit));
fruits.add("water");

fruits.forEach(fruit-> System.out.println(fruit));

 

END

你可能感興趣的文章

spring boot 使用jdbc連接mariadb spring boot, mariadb, jdbc, rowmapper

keycloak 透過java client lib新增user 使用keycloak-admin-client lib maven <dependency> <gro

Spring Boot Maven wrappers maven, mvnw, build command

使用poi 解析 docx 原先是想解析出在docx 中的文字跟圖片, 但是, 有些我們認為是圖片, 其實是用方程式表示, 實在是太麻煩了 就記錄一

Thread java thread

java lambda files filter java, files filter, lambda

隨機好文

download a file from spring boot controllers ownload a file from spring boot controllers

centos 7 移机出現 dracut-initqueue timeout centos 7 移机出現 dracut-initqueue timeout 處理

vue.js component 在parent與child 傳值 component 在parent與child 傳值

windows ad 如何得知 dn 值 如何得知 windows ad 上的使用者dn 值 https://support.symantec.com/en_US

臺中市雲端校務系統與Windows AD帳號整合(7) 設定校端更改密碼程式及執行