-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListUtil.java
More file actions
35 lines (27 loc) · 729 Bytes
/
ListUtil.java
File metadata and controls
35 lines (27 loc) · 729 Bytes
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
package exam01;
import java.util.ArrayList;
import java.util.List;
public class ListUtil {
public static List < Integer > partialProducts(List < Integer > list) {
if (list == null || list.size() == 0) {
throw new IllegalArgumentException("Bad input.");
}
List<Integer> newList = new ArrayList<>();
// For testing
System.out.print("List: ");
for (int i = 0; i<list.size(); i++) {
System.out.print(list.get(i) + " ");
}
System.out.println();
int product = list.get(0);
newList.add(product);
for (int i=1; i<list.size(); i++) {
product = list.get(0);
for (int j=1; j<=i; j++) {
product *= list.get(j);
}
newList.add(product);
}
return newList;
}
}