-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayfunctions.java
More file actions
60 lines (48 loc) · 1.73 KB
/
Copy patharrayfunctions.java
File metadata and controls
60 lines (48 loc) · 1.73 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
59
60
import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;
import java.util.ArrayList;
public class arrayfunctions {
public static void main(String[] args) {
Scanner c = new Scanner(System.in);
int n = c.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = c.nextInt();
}
System.out.println("array" + Arrays.toString(arr));
// 1. Sorting Arrays
Arrays.sort(arr);
System.out.println("array" + Arrays.toString(arr));
// 2. Searching for Elements
System.out.print("what do you want to find Enter : ");
int x = c.nextInt();
int index = Arrays.binarySearch(arr, x);
System.out.println(index);
if (index >= 0)
System.out.print("found " + x + "index : " + index);
// 3. Copying Arrays
int[] desec = Arrays.copyOf(arr, n);
System.out.println("this is copied array : " + Arrays.toString(desec));
// 4. Updating/Modifying Array Elements
desec[3] = 56;
System.out.println(Arrays.toString(desec));
// 5. Dynamic Arrays with ArrayList
ArrayList<String> flower = new ArrayList<>();
flower.add("lily");
flower.add("water lily");
flower.add("Sunflower");
System.out.println("ArrayList : " + flower);
// update
flower.set(2, "potato");
System.out.println("ArrayList : " + flower);
// Remove
flower.remove("Sunflower");
// check existance
if (flower.contains("lily"))
System.out.println("yes");
// sort arrayList
Collections.sort(flower);
System.out.println("Sorted ArrayList : " + flower);
}
}