-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyIntsCollection.java
79 lines (70 loc) · 2.26 KB
/
MyIntsCollection.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
import java.util.Arrays;
public class MyIntsCollection {
private MyInt[] myInts;
// constructor
public MyIntsCollection(MyInt[] integers) {
this.myInts = integers;
}
// empty constructor
public MyIntsCollection() {
this.myInts = new MyInt[1];
}
// This method sort the array of MyInts
public MyInt[] sort(boolean isAsc) {
MyInt[] sortedMyInts = Arrays.copyOf(this.myInts, this.myInts.length);
int compareKey = isAsc ? +1 : -1;
for (int i = 0; i < sortedMyInts.length - 1; i++) {
for (int j = 1; j < sortedMyInts.length - i; j++) {
if (sortedMyInts[j - 1].compareTo(sortedMyInts[j]) == compareKey) {
// swap
MyInt temp = sortedMyInts[j - 1];
sortedMyInts[j - 1] = sortedMyInts[j];
sortedMyInts[j] = temp;
}
}
}
return sortedMyInts;
}
// This method returns the index of MyInt in the array
public int indexOf(MyInt other) {
for (int i = 0; i < this.myInts.length; i++) {
if (this.myInts[i].isEqual(other))
return i;
}
return -1;
}
// This method add element to the array
public void append(MyInt other) {
if (this.myInts[0] == null) {
this.myInts[0] = other;
} else {
MyInt[] newArray = new MyInt[this.myInts.length + 1];
for (int i = 0; i < this.myInts.length; i++)
newArray[i] = this.myInts[i];
newArray[this.myInts.length] = other;
this.myInts = newArray;
}
}
// This method sum MyInts
public MyInt sumAll() {
MyInt sum = new MyInt("0");
for (int i = 0; i < this.myInts.length; i++)
sum = sum.add(this.myInts[i]);
return sum;
}
public String toString() {
String str = "";
for (int i = 0; i < this.myInts.length; i++)
str += this.myInts[i].toString() + "\n";
return str;
}
public static void main(String[] args) {
MyInt[] a = {new MyInt("23"), new MyInt("16"), new MyInt("51")};
MyIntsCollection collection = new MyIntsCollection(a);
MyInt[] sortedArray = collection.sort(false);
System.out.println(Arrays.toString(a));
System.out.println("sorted array: " + Arrays.toString(sortedArray));
System.out.println("index of 51: " + collection.indexOf(new MyInt("51")));
System.out.println("sum = " + collection.sumAll());
}
}