-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkingWith_1D_Array.java
133 lines (118 loc) · 4.48 KB
/
WorkingWith_1D_Array.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import java.util.Scanner;
public class WorkingWith_1D_Array {
public static int[] createArray() {
Scanner input = new Scanner(System.in);
System.out.print("Enter Array Size : ");
int len = input.nextInt();
int[] myList = new int[len];
return myList;
}
public static void inputArray(int[] x) {
Scanner input = new Scanner(System.in);
for (int i = 0; i < x.length; i++) {
System.out.print("Enter value at " + i + " location: ");
x[i] = input.nextInt();
}
}
public static void displayArray(int[] x) {
for (int i = 0; i < x.length; i++) {
System.out.print(x[i] + " ");
}
}
public static int searchArray(int[] x, int y) {
for (int i = 0; i < x.length; i++) {
if (x[i] == y) return i;
}
return -1;
}
public static int largestElement(int[] x) {
int largest = x[0];
for (int i = 1; i < x.length; i++) {
if (x[i] > largest) largest = x[i];
}
return largest;
}
public static void smallestElement(int[] x) {
int smallest = x[0];
for (int i = 1; i < x.length; i++) {
if (x[i] < smallest) smallest = x[i];
}
System.out.print("Smallest Element in [ ");
displayArray(x);
System.out.println("] is " + smallest);
}
public static int[] copyArray(int[] x) {
int[] array = new int[x.length];
for (int i = 0; i < x.length; i++) array[i] = x[i];
return array;
}
public static void main(String[] args) {
System.out.println("----------------------------------------");
System.out.println("| HERE THE MENU BEGINS |");
System.out.println("----------------------------------------");
int[] myList = null;
while (true) {
Scanner input = new Scanner(System.in);
System.out.println("1 : To create an Array");
System.out.println("2 : To input Array values");
System.out.println("3 : To display Array values");
System.out.println("4 : To search element in an Array");
System.out.println("5 : To find largest element in an Array");
System.out.println("6 : To find smallest element in an Array");
System.out.println("7 : To copy data to another Array");
System.out.println("0 : To Exit");
System.out.print("Enter a number from Menu: ");
int option = input.nextInt();
if (option == 0) break;
switch (option) {
case 1:
myList = createArray();
System.out.println();
System.out.println();
break;
case 2:
inputArray(myList);
System.out.println();
System.out.println();
break;
case 3:
displayArray(myList);
System.out.println();
System.out.println();
break;
case 4:
System.out.print("Enter Key : ");
int key = input.nextInt();
int index = searchArray(myList, key);
if (index == -1) System.out.println(" NOT FOUND ");
else System.out.println(key + " is at Index: " + index);
System.out.println();
System.out.println();
break;
case 5:
int lar = largestElement(myList);
System.out.print("Largest Element in [ ");
displayArray(myList);
System.out.println("] is " + lar);
System.out.println();
System.out.println();
break;
case 6:
smallestElement(myList);
System.out.println();
System.out.println();
break;
case 7:
int[] copy = copyArray(myList);
System.out.print("ORIGINAL ");
displayArray(myList);
System.out.println();
System.out.print("COPIED ");
displayArray(copy);
System.out.println();
System.out.println();
break;
}
}
}
}