Java add items into class as an Array -
i have below class structure , want add item it.
public class person implements serializable { private string name; private string mobile; public person(string n, string e) { name = n; mobile = e; } public string getname() { return name; } public string getmobile() { return mobile; } @override public string tostring() { return name; } }
i want add item this:
people = new person[]{ new person("hi" , " programmer"), new person("hello", " world") };
my code , want add items while()
code don't correct.
people = new person[]{}; while (phones.movetonext()) { people = new person("hi" , " programmer"); people = new person("hello", " world") }
you have error in source code trying put object of person array gives compilation error overcome problem first take list of type person , convert array , business logic on array better use list instead of array
list<person> personlst = new arraylist<person>(); while (phones.movetonext()) { personlst.add(new person("hi" , " programmer")); personlst.add(new person("hello", " world")); } object[]arryper = personlst.toarray(); person[]people = new person[arryper.length]; (int j = 0; j < arryper.length; j++) { people[j] = (person) arryper[j]; }
above code of block give array of type people
Comments
Post a Comment