-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayOperation.java
More file actions
41 lines (35 loc) · 1.02 KB
/
Copy pathArrayOperation.java
File metadata and controls
41 lines (35 loc) · 1.02 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
package com.programs;
import java.util.Arrays;
import java.util.Collections;
class ArrayOperation {
public static void main(String args[]) {
// Matching elements in an integer array
int[] array1 = { 1, 2, 3, 4, 5, 1, 2, 6, 7 };
for (int i = 0; i < array1.length; i++) {
for (int j = i + 1; j < array1.length; j++) {
if (array1[i] == array1[j]) {
System.out.println(array1[i]);
}
}
}
// Sum of all the elements in an array
int[] x = { 1, 2, 3, 4, 5 };
int sum = 0;
for (int i = 0; i < x.length; i++) {
sum += x[i];
}
System.out.println("Result " + sum);
// Reversing an array - 1
Integer[] b = { 10, 20, 30, 40, 50 };
Collections.reverse(Arrays.asList(b));
System.out.println(Arrays.asList(b));
// Reversing an array - 2
Integer[] a = { 10, 20, 30, 40, 50, 60 };
for (int i = 0; i < a.length / 2; i++) {
int temp = a[i];
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;
}
System.out.println(Arrays.asList(a));
}
}