1
+ import java .util .ArrayList ;
2
+
1
3
public class Array {
2
4
public static void changeElement (int n ) {
3
5
n += 3 ;
@@ -7,11 +9,65 @@ public static void changeArrayElement(int[] n) {
7
9
n [0 ] += 3 ;
8
10
}
9
11
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
+
10
53
public static void main (String [] args ) {
11
54
int [] list = { 1 , 2 , 3 , 4 };
12
55
changeElement (list [0 ]);
13
56
System .out .println (list [0 ]); // 1
14
57
changeArrayElement (list );
15
58
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
16
72
}
17
73
}
0 commit comments