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

Fixed append issue

parent 3a418c83
No related branches found
No related tags found
No related merge requests found
...@@ -14,11 +14,24 @@ public class DynArray { ...@@ -14,11 +14,24 @@ public class DynArray {
} }
public DynArray() { public DynArray() {
this.capacity = this.basev;
this.array = new Contact[this.capacity]; this.array = new Contact[this.capacity];
this.size = 0; this.size = 0;
} }
public void append(Contact[] 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 append(Contact... a) {
make_sure_size(a); // REALLOC IF NEEDED make_sure_size(a); // REALLOC IF NEEDED
this.size += a.length; this.size += a.length;
int s = this.size; int s = this.size;
...@@ -39,7 +52,7 @@ public class DynArray { ...@@ -39,7 +52,7 @@ public class DynArray {
} }
public void make_sure_size(Contact[] a) { public void make_sure_size(Contact[] a) {
if ((this.size + a.length) > this.capacity) // Overflow? if ((this.size + a.length) >= this.capacity) // Overflow?
{ {
// Realloc new array // Realloc new array
this.capacity *= 2; this.capacity *= 2;
...@@ -50,17 +63,17 @@ public class DynArray { ...@@ -50,17 +63,17 @@ public class DynArray {
} }
} }
public void make_sure_size(Contact a) { // public void make_sure_size(Contact a) {
if ((this.size + 1) > this.capacity) // Overflow? // if ((this.size + 1) > this.capacity) // Overflow?
{ // {
// Realloc new array // // Realloc new array
this.capacity *= 2; // this.capacity *= 2;
Contact[] new_arr = new Contact[this.capacity]; // Contact[] new_arr = new Contact[this.capacity];
copy_content(this.array, new_arr); // copy_content(this.array, new_arr);
this.array = new_arr; // Copy pointer // this.array = new_arr; // Copy pointer
} // }
} // }
public void show_it() { public void show_it() {
System.out.println(this.toString()); System.out.println(this.toString());
...@@ -71,7 +84,7 @@ public class DynArray { ...@@ -71,7 +84,7 @@ public class DynArray {
for (int i = 0; i < this.size; i++) { for (int i = 0; i < this.size; i++) {
s = s.concat(array[i].toShortString()); s = s.concat(array[i].toShortString());
s = s.concat(",\n"); s = s.concat("\n");
} }
return s; return s;
} }
......
...@@ -12,13 +12,14 @@ public class Main { ...@@ -12,13 +12,14 @@ public class Main {
Contact c2 = new Contact(); Contact c2 = new Contact();
Contact c3 = new Contact("fnamekek", "joseph"); Contact c3 = new Contact("fnamekek", "joseph");
c1.show(); // c1.show();
c2.show(); // c2.show();
c3.show(); // c3.show();
System.out.println(c1.toShortString()); // System.out.println(c1.toShortString());
c2.showShort(); // c2.showShort();
//DynArray contacts = new DynArray(); DynArray contacts = new DynArray();
//contacts.append(); contacts.append(c1, c2, c3);
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