forked from LaunchCodeEducation/java-web-dev-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListPractice.java
More file actions
58 lines (48 loc) · 1.54 KB
/
ArrayListPractice.java
File metadata and controls
58 lines (48 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayListPractice {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(2);
numbers.add(5);
numbers.add(10);
numbers.add(7);
numbers.add(8);
numbers.add(13);
numbers.add(6);
numbers.add(4);
numbers.add(9);
numbers.add(12);
int sumOfEvens = sumOfEvenNumbers(numbers);
System.out.println("Sum of even numbers: " + sumOfEvens);
ArrayList<String> name = new ArrayList<>();
name.add("Sonal");
name.add("Antim");
name.add("Solina");
name.add("Simi");
/*System.out.println("enter a name length:");
int = name.nextInt();*/
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the word length to search: ");
int wordLength = scanner.nextInt();
printWordsWithLength(name,wordLength);
}
public static void printWordsWithLength(ArrayList<String> list, int length) {
System.out.println("Words with length " + length + ":");
for (String word : list) {
if (word.length() == length) {
System.out.println(word);
}
}
}
// Now print the ArrayList
public static int sumOfEvenNumbers(ArrayList<Integer> list) {
int sum = 0;
for (int num : list) {
if (num % 2 == 0) {
sum += num;
}
}
return sum;
}
}