0

Can you explain this code to me, what does arrays.aslist(job) mean ?

String jobs[] ={"senior","programmeur","project manager"};
LinkedList<String> links = new LinkedList<String>(Arrays.asList(jobs));

4 Answers 4

1

From the documentation:

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 implements RandomAccess.

Used to create quick list, like:

List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");

See here

Sign up to request clarification or add additional context in comments.

Comments

1

The arrays.aslist(job) is converting your String array to a List. The Javadoc for Arrays.asList says

Returns a fixed-size list backed by the specified array.

What it does is

  • it creates a wrapper that implements List<String>, basically original array is now available as new list
  • when you add it to a new LinkedList<String>() it copies all elements from the original array to new List. It creates a immutable copy of the original array

Comments

0

Also, asList helps to get object based collections out of primitive arrays.

Comments

0

Arrays.asList() creates an immutable List from an array.

To get a mutable List from an immutable List , create a new List (in this case, LinkedList) passing the immutable List in to its constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.