diff --git a/src/DynArray.java b/src/DynArray.java index 9ff05ff3e7e97115f2d7ee436004beb813e77e5f..3343d8c16087d2b1e272af4ea69b552692ffab01 100644 --- a/src/DynArray.java +++ b/src/DynArray.java @@ -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()); diff --git a/src/Main.java b/src/Main.java index 54f3aea7c6f1e8d18e2953cdfdc286eb20234444..9bd88b87211f3f9007838a7d094ebbb593443636 100644 --- a/src/Main.java +++ b/src/Main.java @@ -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()); } }