Skip to content
This repository was archived by the owner on Sep 22, 2024. It is now read-only.

Commit 1202ce8

Browse files
committed
products and sums of arraylist
1 parent 8330b7b commit 1202ce8

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

Array.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import java.util.ArrayList;
2+
13
public class Array {
24
public static void changeElement(int n) {
35
n += 3;
@@ -7,11 +9,65 @@ public static void changeArrayElement(int[] n) {
79
n[0] += 3;
810
}
911

12+
public static int getProductSum(ArrayList<Integer> list, int[] arr) {
13+
int fin = 0;
14+
int index = 0;
15+
for (int num : list) {
16+
if (index < arr.length) {
17+
fin += num * arr[index];
18+
index++;
19+
}
20+
}
21+
return fin;
22+
}
23+
24+
public static int[] getProducts(ArrayList<Integer> list, int[] arr) {
25+
int num = list.size();
26+
if (list.size() < arr.length) {
27+
num = arr.length;
28+
}
29+
int[] fin = new int[num];
30+
int index = 0;
31+
if (num == list.size()) {
32+
for (int n : list) {
33+
if (index < arr.length) {
34+
fin[index] = n * arr[index];
35+
} else {
36+
fin[index] = arr[index];
37+
}
38+
index++;
39+
}
40+
} else {
41+
for (int n : arr) {
42+
if (index < list.size()) {
43+
fin[index] = n * list.get(index);
44+
} else {
45+
fin[index] = list.get(index);
46+
}
47+
index++;
48+
}
49+
}
50+
return fin;
51+
}
52+
1053
public static void main(String[] args) {
1154
int[] list = { 1, 2, 3, 4 };
1255
changeElement(list[0]);
1356
System.out.println(list[0]); // 1
1457
changeArrayElement(list);
1558
System.out.println(list[0]); // 4
59+
ArrayList<Integer> list1 = new ArrayList<Integer>();
60+
list1.add(2);
61+
list1.add(1);
62+
list1.add(4);
63+
int[] arr = { 5, 0, 3 };
64+
for (int product : getProducts(list1, arr))
65+
System.out.println(product); // { 10, 0, 12 }
66+
System.out.println(getProductSum(list1, arr)); // 22
67+
ArrayList<Integer> list2 = new ArrayList<Integer>();
68+
int[] arr2 = { 2, 3, 7 };
69+
for (int product : getProducts(list2, arr2))
70+
System.out.println(product); // { 10, 0, 12 }
71+
System.out.println(getProductSum(list2, arr2)); // 22
1672
}
1773
}

0 commit comments

Comments
 (0)