Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 15f92a1

Browse files
Add linkedlist to array example
1 parent 08e3655 commit 15f92a1

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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("\nPrinting the object array elements: ");
32+
for(Object obj:arr) {
33+
System.out.println(obj.toString());
34+
}
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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("\nPrinting the String array elements: ");
32+
for(String str:arr) {
33+
System.out.println(str);
34+
}
35+
}
36+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /