java - I am trying to print the name of the list but somehow the reference from the arraylist is not working? -
customer class. class list arraylist of customer. have added customers list when want print customer names list null only.
import java.util.*; public class assignment1 { public static void main(string args[]) { list list = new list(); list.addcustomer("man"); list.addcustomer("man"); //system.out.println(list); list.printx(); } } class customer{ public string name; public customer(string name) { name = this.name; } } class list { arraylist<customer> list = new arraylist<customer>(); public void addcustomer(string name1) { customer x = new customer(name1); list.add(x); system.out.println(list.get(0)); } public void printx() { for(int =0;i < list.size();i++) { system.out.println(list.get(i).name); } } }
inside customer constructor, need set ::
this.name = name; and not other way round! :p
what have done right change function parameter name class parameter name null(default initialization). so, never initialize name variable of customer class, , hence null when print it.
Comments
Post a Comment