Skip to content
Snippets Groups Projects
Commit 8949f74a authored by jonas.stirnema's avatar jonas.stirnema
Browse files

Add family name sorted ok

parent 854aed3a
No related branches found
No related tags found
No related merge requests found
......@@ -31,6 +31,53 @@ public class DynArray {
// }
// }
public void insert(Contact a, int index) {
//this.show_it();
Contact[] new_arr = new Contact[++this.capacity];
// Before index
for (int j = 0;j < index; j++) {
new_arr[j] = this.array[j];
}
// Add the new item
new_arr[index] = a;
this.size++;
// Add the rest of the items
for (int j = index; j < this.size; j++) {
new_arr[j + 1] = this.array[j];
}
this.array = new_arr; // Copy pointer
}
// SORTED INSERT
// GO TRHOUGH THE ARRAY
// CHECK IF THE NEXT CONTACT FNAME AND NAME IS GRAMATICALLY BIGGER
// IF IT IS, CREATE A NEW ARRAY, ADD ALL THE PREVIOUS VALUES
// ADD THE VALUE TO INSERT, ADD THE REST OF THE VALUES
public void insert(Contact a) {
//System.err.println("Size = " + size);
for (int i = 0; i < this.size; i++) {
// Check if a.fname should be before ith item
// Order by fname
if (a.getFname().compareToIgnoreCase(this.array[i].getFname()) < 0) {
// Should be before when compared by fname
//System.err.println("In if");
this.insert(a, i);
return;
}
}
//System.err.println("Appenned");
this.append(a);
}
public void append(Contact... a) {
make_sure_size(a); // REALLOC IF NEEDED
this.size += a.length;
......@@ -63,17 +110,17 @@ public class DynArray {
}
}
// public void make_sure_size(Contact a) {
// if ((this.size + 1) > this.capacity) // Overflow?
// {
// // Realloc new array
// this.capacity *= 2;
// Contact[] new_arr = new Contact[this.capacity];
// copy_content(this.array, new_arr);
public void make_sure_size(Contact a) {
if ((this.size + 1) > this.capacity) // Overflow?
{
// Realloc new array
this.capacity *= 2;
Contact[] new_arr = new Contact[this.capacity];
copy_content(this.array, new_arr);
// this.array = new_arr; // Copy pointer
// }
// }
this.array = new_arr; // Copy pointer
}
}
public void show_it() {
System.out.println(this.toString());
......
......@@ -11,15 +11,20 @@ public class Main {
"Eboueur");
Contact c2 = new Contact();
Contact c3 = new Contact("fnamekek", "joseph");
// c1.show();
// c2.show();
// c3.show();
// System.out.println(c1.toShortString());
// c2.showShort();
Contact c4 = new Contact("igueule", "monvieux");
Contact c5 = new Contact("monkey", "Lucio");
DynArray contacts = new DynArray();
contacts.append(c1, c2, c3);
//contacts.append(c1, c2, c3);
//contacts.insert(c4, 1);
contacts.insert(c1);
contacts.insert(c2);
contacts.insert(c3);
contacts.insert(c4);
contacts.insert(c5);
System.out.println(contacts.toString());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment