Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creation of linkedList in java directly if we know the elements

Tags:

java

  List<String> ll = new LinkedList<String>("String1","String2",...);

I want something like above. Is the above line possible in java...?

like image 366
Saurabh Kumar Avatar asked Jun 09 '11 21:06

Saurabh Kumar


People also ask

Can we create our own LinkedList in Java?

Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at the contiguous location, the elements are linked using pointers as shown below. In Java, LinkedList can be represented as a class and a Node as a separate class.

What method of LinkedList should be used when determining if an element exists in the list?

LinkedList. contains() method is used to check whether an element is present in a LinkedList or not. It takes the element as a parameter and returns True if the element is present in the list.


1 Answers

not directly but

List<String> ll = new LinkedList<String>(Arrays.asList("String1","String2",...));

is what you're looking for

like image 61
ratchet freak Avatar answered Sep 19 '22 15:09

ratchet freak