From 1f01c3d90f898817603b82f3d8ed3066f6beca60 Mon Sep 17 00:00:00 2001 From: Daniele <44429736+dbarani@users.noreply.github.com> Date: Thu, 27 Jun 2019 21:21:41 +0200 Subject: [PATCH] Arrays.asList() explanation (#33522) Updated with a brief explanation of the Arrays.asList() function with example. --- guide/english/java/arraylist/index.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/guide/english/java/arraylist/index.md b/guide/english/java/arraylist/index.md index 0ee5ab321c..f1cb39c80e 100644 --- a/guide/english/java/arraylist/index.md +++ b/guide/english/java/arraylist/index.md @@ -100,6 +100,17 @@ for(Object obj : arr) { } ``` +**Split a delimited string into an list of String** + +Let's say we have a string variable called ``` String stringToSplit = "PHP,Java,C++"; ```. +Now, we want to split it into a List of String, so we can iterate over the List. + +Since ArrayList implements the interface *List*, we can use the function ```asList``` to obtain a fixed-size List of String out of the splitted string ```stringToSplit```: + +```java + List list = new ArrayList(Arrays.asList(stringToSplit.split(","))); +``` + An ArrayList allows us to randomly access elements. ArrayList is similar to *Vector* in a lot of ways, but it is faster than Vectors. The main thing to note is that - Vectors are faster than arrays but ArrayLists are not. So when it comes down to choosing between the two - if speed is critical then Vectors should be considered for a small set of items, otherwise ArrayLists are better when it comes to storing large number of elements and accessing them efficiently. Vectors are synchronized and arraylist are not, thats why choose vector for frequent update of elements otherwise Arraylist is efficient for traversing.