0

I want to chain mylist11 and mylist12. I have error. Thanks you.

Chain https://code.sololearn.com/ceI2an2X2Fg6/?ref=app

28th Sep 2020, 4:59 AM
Malick Diagne
Malick Diagne - avatar
7 odpowiedzi
+ 3
There is a lot I can't follow with your code but I changed enough to your code make some sense. The following code runs without error. I changed a lot which probably deviates from what you want, though. My changes would be more understandable to most developers who worked with linked lists, though: // list and chain list public class Program { public static void main(String[] args) { // first tower Liste mylist1 = new Liste ("Malick "); Liste mylist2 = new Liste ("Nicole"); mylist1.next = mylist2 ; mylist2.next = null ; Liste mylist3 = new Liste ("Yvette "); // fourth round Liste mylist4 = new Liste ("Sonta"); mylist3.next = mylist4 ; mylist4.next = null ; System.out.println ("My list: " + mylist1.toString() ); System.out.println ("My list: " + mylist3.toString() ); } } class Liste { String name; Liste next ; Liste (String myname){ name = myname ; } @Override public String toString() { if (next == null) return name; else return name + ", " + next.toString(); } } I'd like to share more detail on my areas of confusion with the original code. I'm not sure what you're trying to do with the MyList class. It looks like "Liste" is short for "List Element". Normally an individual element in a linked list is called a node. I don't understand the MyListe class at all. The constructor does nothing but print just 2 values. A node will commonly have a next property that you have but an individual node usually won't maintain a "head". If there's a dedicated class to representing the entire linked list, it'll have a head or a first node reference. I just can't tell if Myliste or Liste is for that purpose. The head1, head2, next2 also makes very little sense to me.
28th Sep 2020, 5:18 AM
Josh Greig
Josh Greig - avatar
+ 2
Josh Greig I think Liste is French for List. I also have a variant version of this code, but I'm unsure what specifically Malick Diagne is trying to accomplish. https://code.sololearn.com/ctztXkqockB6/?ref=app Including code here for inline review: ---- public class Program { public static void main(String[] args) { var a = new Node("Malick"); a.next = new Node("Nicole"); var b = new Node("Yvette"); b.next = new Node("Sonta"); System.out.println("My list: " + a); System.out.println("Yor list: " + b); } } class Node { String name; Node next; Node head; Node (String name){ this.name = name; head = this; } public String toString() { var s = ""; var current = this; do { s = s == "" ? current.name : s + ", " + current.name; current = current.next; } while (current != null); return s; } } ----
28th Sep 2020, 7:51 AM
David Carroll
David Carroll - avatar
+ 1
Josh Greig Dont forget to override toString()
28th Sep 2020, 5:23 AM
Odyel
Odyel - avatar
+ 1
Josh Greig yeah I saw that with David's code
28th Sep 2020, 2:26 PM
Odyel
Odyel - avatar
0
Odyel wrote "Josh Greig Dont forget to override toString()". Sure. I edited the @Override annotation into the previous answer based on your feedback. It works regardless but the annotation can make the intent to override more clear to the compiler and other developers so it helps.
28th Sep 2020, 5:38 AM
Josh Greig
Josh Greig - avatar
0
Josh Greig I override toString() because instead of writing System.out.println(mylist3.toString()); you can just write System.out.println(mylist3); You won't have to explicitly write the .toString() when printing
28th Sep 2020, 5:42 AM
Odyel
Odyel - avatar
0
Odyel, explicitly calling toString() was also unneeded regardless of the @Override annotation. I wanted to be explicit about calling toString() because Malick seems new to programming and the more explicit things are the better. By calling toString() explicitly, it is clear that toString() is getting called even if they didn't know that toString() is always called when a string representation is needed. Try this code and see that it behaves exactly the same with or without the @Override annotation. You're still overriding without the annotation. The annotation is basically a redundantly explicit way of saying that this must override a method or the compiler should scream. // list and chain list public class Program { public static void main(String[] args) { // first tower Liste mylist1 = new Liste ("Malick "); Liste mylist2 = new Liste ("Nicole"); mylist1.next = mylist2 ; mylist2.next = null ; Liste mylist3 = new Liste ("Yvette "); // fourth round Liste mylist4 = new Liste ("Sonta"); mylist3.next = mylist4 ; mylist4.next = null ; System.out.println ("My list: " + mylist1 ); System.out.println ("My list: " + mylist3 ); } } class Liste { String name; Liste next ; Liste (String myname){ name = myname ; } public String toString() { if (next == null) return name; else return name + ", " + next; } }
28th Sep 2020, 2:19 PM
Josh Greig
Josh Greig - avatar