toArray() method in Graph Data Structure

Why are we mentioning as Node [0] in all.toArray(newNode[0])[0]), when I checked for the syntax it is suggesting as size of array but we are passing 0 in new Node[0]. Please clarify on this

Course: The Ultimate Data Structures and Algorithms: Part 3
Topic: Graphs (Cycle Detection in directed graphs)

The toArray() method without parameters returns an array of Objects. Usually you want a more specific return type and can specifiy that return type as the first argument. In this case you want an array of Nodes so you provide an array of Nodes as an argument. Since this is just used to determine the result type it’s ok to supply an empty array of the desired type.

And yes, that is a very ugly construction. That’s why Mosh shows you a better way just a few seconds later. And don’t worry if that looks weird to you, too. All you need to understand for this course is that the code returns the first element of the set.

1 Like

Thanks for the explanation.