File tree Expand file tree Collapse file tree 2 files changed +72
-0
lines changed
collections/linkedlist/convert-linkedlist-to-array Expand file tree Collapse file tree 2 files changed +72
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .gaurav .ExProject .LinkedList ;
2+
3+ import java .util .LinkedList ;
4+
5+ /**
6+ * A java program to convert linkedlist to an array
7+ * using the toArray() method.
8+ *
9+ * @author coderolls.com
10+ *
11+ */
12+ public class LinkedListToArray {
13+
14+ public static void main (String [] args ) {
15+
16+ LinkedList <String > linkedList = new LinkedList <String >();
17+ linkedList .add ("Walmart" );
18+ linkedList .add ("Amazon" );
19+ linkedList .add ("Apple" );
20+ linkedList .add ("Microsoft" );
21+ linkedList .add ("Google" );
22+ linkedList .add ("Uber" );
23+ linkedList .add ("Tesla" );
24+
25+ System .out .println ("Printing the linkedList: " );
26+ System .out .println (linkedList );
27+
28+ // convert linkedList to Object[]
29+ Object [] arr = linkedList .toArray ();
30+
31+ System .out .println ("\n Printing the object array elements: " );
32+ for (Object obj :arr ) {
33+ System .out .println (obj .toString ());
34+ }
35+ }
36+ }
Original file line number Diff line number Diff line change 1+ package com .gaurav .ExProject .LinkedList ;
2+
3+ import java .util .LinkedList ;
4+
5+ /**
6+ * A java program to convert linkedlist to an array
7+ * using the toArray(T[] a) method.
8+ *
9+ * @author coderolls.com
10+ *
11+ */
12+ public class LinkedListToArray2 {
13+
14+ public static void main (String [] args ) {
15+
16+ LinkedList <String > linkedList = new LinkedList <String >();
17+ linkedList .add ("Walmart" );
18+ linkedList .add ("Amazon" );
19+ linkedList .add ("Apple" );
20+ linkedList .add ("Microsoft" );
21+ linkedList .add ("Google" );
22+ linkedList .add ("Uber" );
23+ linkedList .add ("Tesla" );
24+
25+ System .out .println ("Printing the linkedList: " );
26+ System .out .println (linkedList );
27+
28+ // convert linkedList to String array
29+ String [] arr = linkedList .toArray (new String [linkedList .size ()]);
30+
31+ System .out .println ("\n Printing the String array elements: " );
32+ for (String str :arr ) {
33+ System .out .println (str );
34+ }
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments