-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListTesting.java
More file actions
110 lines (82 loc) · 2.54 KB
/
Copy pathArrayListTesting.java
File metadata and controls
110 lines (82 loc) · 2.54 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
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
// Jacob Schnettler
// 2/12/24
import java.util.ArrayList;
public class ArrayListTesting
{
public static void outputListHorizontally(ArrayList a)
{
for (int i = 0; i < a.size(); i++)
System.out.print(a.get(i) + " ");
}
public static void outputListVertically(ArrayList a)
{
for (int i = 0; i < a.size(); i++)
System.out.println(a.get(i) + " ");
}
public static int getArrayListSum(ArrayList<Integer> a)
{
int sum = 0;
for (int i = 0; i < a.size(); i++)
sum = sum + a.get(i);
return sum;
}
public static int getArrayListMeanAverage(ArrayList<Integer> a)
{
return getArrayListSum(a) / a.size();
}
public static int findMin(ArrayList<Integer> list)
{
int min = list.get(0);
for (int i = 0; i < list.size(); i++)
if (min > list.get(i))
min = list.get(i);
return min;
}
public static int linearSearch(ArrayList<Integer> list, Object thing)
{
for (int i = 0; i < list.size(); i++)
if (list.get(i).equals(thing))
return i;
return -1;
}
public static int binarySearch(ArrayList<Integer> list, Integer thing)
{
int high = list.size() - 1;
int low = 0;
int middle = (low + high) / 2;
while (list.get(middle) != thing)
{
if (thing < list.get(middle))
{
high = middle - 1;
} else if (thing > list.get(middle))
{
low = middle + 1;
}
middle = (low + high) / 2;
}
return middle;
}
public static void bubbleSort(ArrayList<Integer> list)
{}
public static void main()
{
ArrayList<Integer> a = new ArrayList<Integer>();
// Fill ArrayList with 1, 2, 3 .. 10
for (int i = 0; i < 10; i++)
a.add(i + 1);
// System.out.println(a.toString());
// Method output horizontally
// outputListHorizontally(a);
// Method output vertically
// outputListVertically(a);
// Method sum
// System.out.println(getArrayListSum(a));
// Method mean avg.
// System.out.println(getArrayListMeanAverage(a));
// Find min
// System.out.println(findMin(a));
// Linear search
// System.out.println(linearSearch(a, 3));
}
}