From 854aed3a152f8bd128f9ab2f69dcc85b759148d3 Mon Sep 17 00:00:00 2001 From: "jonas.stirnema" <jonas.stirnemann@etu.hesge.ch> Date: Thu, 27 Oct 2022 17:44:54 +0200 Subject: [PATCH] Fixed append issue --- src/DynArray.java | 39 ++++++++++++++++++++++++++------------- src/Main.java | 15 ++++++++------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/DynArray.java b/src/DynArray.java index 10f7339..9ff05ff 100644 --- a/src/DynArray.java +++ b/src/DynArray.java @@ -14,11 +14,24 @@ public class DynArray { } public DynArray() { + this.capacity = this.basev; this.array = new Contact[this.capacity]; 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 this.size += a.length; int s = this.size; @@ -39,7 +52,7 @@ public class DynArray { } 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 this.capacity *= 2; @@ -50,17 +63,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()); @@ -71,7 +84,7 @@ public class DynArray { for (int i = 0; i < this.size; i++) { s = s.concat(array[i].toShortString()); - s = s.concat(",\n"); + s = s.concat("\n"); } return s; } diff --git a/src/Main.java b/src/Main.java index 816b63a..54f3aea 100644 --- a/src/Main.java +++ b/src/Main.java @@ -12,13 +12,14 @@ public class Main { Contact c2 = new Contact(); Contact c3 = new Contact("fnamekek", "joseph"); - c1.show(); - c2.show(); - c3.show(); - System.out.println(c1.toShortString()); - c2.showShort(); + // c1.show(); + // c2.show(); + // c3.show(); + // System.out.println(c1.toShortString()); + // c2.showShort(); - //DynArray contacts = new DynArray(); - //contacts.append(); + DynArray contacts = new DynArray(); + contacts.append(c1, c2, c3); + System.out.println(contacts.toString()); } } -- GitLab