-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperationOnArrayLists.java
More file actions
49 lines (33 loc) · 1.23 KB
/
OperationOnArrayLists.java
File metadata and controls
49 lines (33 loc) · 1.23 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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class OperationOnArrayLists {
/* Solution for Question 1.*/
static int searchXunsortedArray(ArrayList arr, int search)
{
/* indexOf() return the first occruence of an elemnet in the array.
*/
return arr.indexOf(search);
}
public static void main(String[] args) {
/* 1. Given a set of numbers in an UNSORTED ARRAY.
We need to find a number X in the array. If X is present in the array, we need to return the
first index of X. In Array Lists we have indexOf()
*/
ArrayList<Integer> arr = new ArrayList<>();
Scanner sc = new Scanner(System.in);
int n = 0;
System.out.println("Enter the number of elements: ");
n = sc.nextInt();
System.out.println("Enter the elements:");
for(int i = 0 ; i < n ; i++)
{
int x = sc.nextInt();
arr.add(x);
}
System.out.println("Enter the number you want to search: ");
int search = sc.nextInt();
int index = searchXunsortedArray(arr, search);
System.out.println("Index of the searched number is: " +index);
}
}