-3

I am generating a list from the Registery using the code below.

  List<String> options = Arrays.asList(Advapi32Util.registryGetStringArray(HKEY_LOCAL_MACHINE, key, value)

Later on in the code I want to add to this list However the add method is not working.

The following code below give me an java.lang.unsupportedOperationException.

  options.add("Test");


  Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:148)
    at java.util.AbstractList.add(AbstractList.java:108)

Any ideas.

user1158745
  • 2,402
  • 9
  • 41
  • 60

2 Answers2

3

Arrays.asList() returns an immutable list. Instead new ArrayList<>() could be used. The code would then look like follows:

List<String> options = new ArrayList<>(Arrays.asList(
    Advapi32Util.registryGetStringArray(HKEY_LOCAL_MACHINE, key, value)));
options.add("Test");
ldz
  • 2,217
  • 16
  • 21
  • Thanks let me give that a try. – user1158745 Dec 02 '19 at 21:28
  • Java doesn't seem to link that. The method add(String) in the type List is not applicable for the arguments (String[]). Is it becuase I am getting it from the reg? – user1158745 Dec 02 '19 at 21:35
  • I overlooked that the method returns an array. As you can pass collections to the `ArrayList` constructor the only change in your code would be wrapping it with `new ArrayList<>()`. See answer update. – ldz Dec 02 '19 at 22:03
0

Arrays.asList creates immutable list which cannot be modified.

Mohamed Sweelam
  • 1,109
  • 8
  • 22