import java.util.*;
import java.lang.*;
import java.io.*;
class Testing
{
class Person implements Comparable<Person> {
int cpr=200193;
int age=21;
public Person() {}
public Person
(String name,
String Country,
int cpr,
int age
) { this.cpr=cpr;
this.age=age;
this.name=name;
this.Country=Country;
}
return "Person [Country= " + Country + ", Name:" + name + ", Cpr: "+cpr+ ", age: "+age+"]";
}
public int compareTo(Person p) {
if(p != null) {
return this.age - p.age;
}
}
Person[] sortedArray = new Person[7];
int size = -1;
boolean addToArray(Person item) {
if (item == null) {
return false;
} else if (size == -1) {
size++;
sortedArray[size] = item;
return true;
} else {
// find the correct position using Binary Search
.binarySearch(sortedArray, 0, size+1, item);
if (insertionPoint >= 0) {
// duplicate value
return false;
}
// set the insertionPoint to proper value
insertionPoint = (-(insertionPoint) - 1);
// shift
for (int i = size + 1; i > insertionPoint; i--) {
sortedArray[i] = sortedArray[i - 1];
}
// insert
sortedArray[insertionPoint] = item;
size++;
return true;
}
}
void test() {
addToArray(new Person());
addToArray(new Person("Pete","Germany",111111,86));
addToArray(new Person("John","Denmark",123456,75));
addToArray(new Person("Michael Jackson", "America",112345,49));
for (int i = 0; i <= size; i++) {
System.
out.
println(sortedArray
[i
]); }
}
{
Testing t = new Testing();
t.test();
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgpjbGFzcyBUZXN0aW5nCnsKCWNsYXNzIFBlcnNvbiBpbXBsZW1lbnRzIENvbXBhcmFibGU8UGVyc29uPiB7CgkgICAgaW50IGNwcj0yMDAxOTM7CgkgICAgaW50IGFnZT0yMTsKCSAgICBTdHJpbmcgbmFtZT0iSm9obiBEb2UiOwoJICAgIFN0cmluZyBDb3VudHJ5PSJVemJla2lzdGFuIjsKCgkgICAgcHVibGljIFBlcnNvbigpIHt9CgkgICAgcHVibGljIFBlcnNvbihTdHJpbmcgbmFtZSwgU3RyaW5nIENvdW50cnksaW50IGNwcixpbnQgYWdlKSB7CgkgICAgIC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fQp9
Person [Country= Uzbekistan, Name:John Doe, Cpr: 200193, age: 21]
Person [Country= America, Name:Michael Jackson, Cpr: 112345, age: 49]
Person [Country= Denmark, Name:John, Cpr: 123456, age: 75]
Person [Country= Germany, Name:Pete, Cpr: 111111, age: 86]