简体   繁体   English

list.add(“东西”); 崩溃

[英]list.add(“stuff”); crashes

I'm trying to make a simple list. 我正在尝试制作一个简单的清单。

I've got this: 我有这个:

    String valuesArray[] = {"473", "592", "1774", "341", "355", "473", "950", "500", "44", "946.35", "750", "950"};
    List<String> valueList = Arrays.asList(valuesArray); 

Whenever I try to add something to the list, it force closes. 每当我尝试向列表添加内容时,它都会强制关闭。

    valueList.add("Test");

And it really seems to only happen when I try to add to the list. 它似乎只有在我尝试添加到列表时才会发生。 I'm able to get values from the list, just not add to it. 我可以从列表中获取值,只是不添加它。

As you can see from the docs for Arrays.asList() , the List returned from that method is fixed size. 正如您从Arrays.asList()文档中看到的Arrays.asList() ,从该方法返回的List是固定大小。 If you want something more versatile, you might try: 如果你想要更多样化的东西,你可以尝试:

List<String> valueList = new ArrayList<String>(Arrays.asList(valuesArray));

Arrays.asList() returns a fixed size list . Arrays.asList()返回固定大小的列表 You cannot add to it. 你无法添加它。

另一种选择是循环遍历数组并一次添加一个,然后,它将不是列表的固定大小,您可以执行所需的所有列表操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM