Vlad Starostenko :
import java.util.*;
public class Test {
public static void main(String[] args) {
String[] arr = {"Java", "Champ", "."};
List<String> list = (List<String>) Arrays.asList(arr); // line 1
arr[2] = ".com"; // line 2
for (String word : list) {
System.out.print(word);
}
}
}
Can please someone explain me why we get here "JavaChamp.com"? I thought it shoud be just "JavaChamp." because line 2 after line 1.
Aniket Sahrawat :
Because Arrays#asList
returns the fixed-size List
backed by the array.
Here is the doc of Arrays#asList
:
Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with
Collection.toArray()
. The returned list is serializable and implementsRandomAccess
.
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=304822&siteId=1