Movatterモバイル変換


[0]ホーム

URL:


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

Commitcfce324

Browse files
author
jsquared21
committed
Add Ex_27.11
1 parent9d1a503 commitcfce324

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Binary file not shown.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*********************************************************************************
2+
* (setToList) Write the following method that returns an ArrayList from a set. *
3+
* *
4+
* public static <E> ArrayList<E> setToList(Set<E> s) *
5+
*********************************************************************************/
6+
importjava.util.ArrayList;
7+
importjava.util.Set;
8+
9+
publicclassExercise_27_11 {
10+
publicstaticvoidmain(String[]args) {
11+
// Create a set
12+
Set<String>set =newjava.util.HashSet<>();
13+
set.add("Mark");
14+
set.add("Smith");
15+
set.add("Anderson");
16+
set.add("Sally");
17+
System.out.println("Elements in the set: " +set);
18+
19+
// Convert the set to an ArrayList
20+
ArrayList<String>list =setToList(set);
21+
System.out.println("Elements in the ArrayList: " +list);
22+
System.out.println("Elements at ArrayList index 2: " +list.get(2));
23+
24+
}
25+
26+
/** Return an ArrayList from a set */
27+
publicstatic <E>ArrayList<E>setToList(Set<E>s) {
28+
// Create an ArrayList
29+
ArrayList<E>list =newArrayList<>();
30+
31+
// Add elements from set
32+
for (Ee:s) {
33+
list.add(e);
34+
}
35+
36+
returnlist;
37+
}
38+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp