0

次のコードがあります。

public void parseAttribs(String attribs){

   //attribs is a comma separated list
   //we are making a List from attribs by splitting the string at the commas

   List<String> attributes = Arrays.asList(attribs.split("\\s*,\\s*"));

   //when I try to add an element to the attributes List if fails
   attributes.add("an element");

このUnable to add a String to an ArrayList: "misplaced construct(s)"が見つかり、サブクラスを作成しようとしましたが、リストをサブクラスにも渡す必要がありましたが、それでも機能しませんでした。

誰でもこれに光を当てることができますか?

どうもありがとう

4

2 に答える 2

9

Arrays.asList固定サイズの を返しますList。あなたが使用することができます

new ArrayList<String>(Arrays.asList(...)))

これによりList、要素を追加できる場所が表示されます。

于 2013-03-11T13:44:00.470 に答える
6

Arrays.asList()によって返されるリストは不変リストであるため、このコードは機能しません。

ArrayList コンストラクターから構築して、それを機能させることができます。

List<String> attributes = new ArrayList<String>(Arrays.asList(attribs.split("\\s*,\\s*")));
于 2013-03-11T13:43:55.837 に答える