Java Java Data Structures Exploring the Java Collection Framework Lists

Can anyone explain? How did Craig use it to explain in the video?

1 -.toArray (new String [0])

2- Arrays.asList()

Steven Parker Brendan Whiting Gavin Ralston Gabbie Metheny

1 Answer

Daniel Phillips
Daniel Phillips
23,313 Points
  1. He was showing that if you have a List of Strings you can use the toArray() method to generate an array of Strings. Using toArray() with no arguments produces an Object which is an array of Strings. If you need an actual String array, you can pass the toArray() method an argument which is a String array (Craig just created a new one inline with new String[0]).
  2. Working the other way, Craig was showing that if we have some Strings (or any Objects of the same type) that we want to convert into a List of Strings (or a List of Objects of the same type), we can use the Arrays.asList() method. Just pass the objects as a comma-separated list of arguments.