Arrays.asList()
时间: 2023-08-15 15:10:23 浏览: 127
The `Arrays.asList()` method in Java is used to convert an array into a List. It takes an array as a parameter and returns a fixed-size List backed by the array passed.
Here is an example of how to use `Arrays.asList()`:
```java
String[] array = {"apple", "banana", "orange"};
List<String> list = Arrays.asList(array);
System.out.println(list);
```
In the above code, we have an array of strings called `array`. We pass this array to `Arrays.asList()` method, which returns a List of strings. We assign this list to the variable `list`.
The output of the code will be `[apple, banana, orange]`, which is the string representation of the list.
It's important to note that the returned List is a fixed-size list, meaning that you cannot add or remove elements from it directly. If you try to do so, it will throw an `UnsupportedOperationException`. If you need a mutable list, you can create a new ArrayList from the returned list:
```java
List<String> mutableList = new ArrayList<>(list);
```
Now, `mutableList` is a mutable ArrayList that you can modify as needed.
阅读全文