public class DynArray { private final int basev = 10; private int size = 0; private Contact[] array; private int capacity; public DynArray(Contact[] a) { this.size = a.length; this.capacity = this.basev; this.array = new Contact[this.capacity]; copy_content(a, this.array); } public DynArray() { this.capacity = this.basev; this.array = new Contact[this.capacity]; this.size = 0; } // public void append(Contact[] a) { // make_sure_size(a); // REALLOC IF NEEDED // this.size += a.length; // int s = this.size; // // Add the new values // for (int i = s; i < s + a.length; i++) { // // System.out.println("arr[" + (i - a.length) + "] = " + a[i - s]); // this.array[i - a.length] = a[i - s]; // } // } 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; int s = this.size; // Add the new values for (int i = s; i < s + a.length; i++) { // System.out.println("arr[" + (i - a.length) + "] = " + a[i - s]); this.array[i - a.length] = a[i - s]; } } public void pop() { if (this.size < 1) { System.err.println("Cannot remove from an epmty list"); return; } this.size--; } public void make_sure_size(Contact[] a) { if ((this.size + a.length) >= 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 } } 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 } } public void show_it() { System.out.println(this.toString()); } public String toString() { String s = ""; for (int i = 0; i < this.size; i++) { s = s.concat(array[i].toShortString()); s = s.concat("\n"); } return s; } public boolean isEmpty() { return size == 0; } private static void copy_content(Contact[] source, Contact[] dest) { // System.out.println("Source lenght" + source.length); // System.out.println("Dest lenght" + dest.length); if (source.length > dest.length) { throw new Error("Cannot copy : second array smaller than first"); } for (int i = 0; i < source.length; i++) { dest[i] = source[i]; } } }