java - Accessing an array within an Object that consists of a string and the array -


my program has object class person. person object consists of string name (which name of person) , 6 element string array choices. array holds values user enters rank 6 different sports preferred least preffered.

for example, person object called testperson consist of string set "bob" , array each element follows {3,1,4,2,5,6}. (the second sport listed bob's preferred sport , last sport listed least preferred).

that example object can created following code:

string[] temparray = new string[]{"3","1","4","2","5","6"}; person newperson = person("bob",temparray); 

here person class:

public class person{     private string[] choices = new string[6];     private string name;     //constructor     public person(string myname, string[] myinput){         choices = myinput;         name = myname;     }     //returns choices array     public string[] getchoices(){         return(choices);     }     //returns name     public string getname(){         return(name);     } } 

i create new object every time new user enters name , choices. since don't know how many times new user enter details, store each new object within arraylist called "people". (yes, know... array within object within arraylist)

after (unknown number of) users have entered information button pressed indicating there no more users enter information. @ point need access both name , choices array each person object use each loop. how set up:

string[] mychoices = new string[6]; string myname; for(person getperson: people){     mychoices = getperson.getchoices();     myname = getperson.getname();     //print name , choices     system.out.println(myname);     for(int = 0; < 6; ++){         system.out.println(mychoices[i]);     } } 

that "for each" loop should print name , each element of 6 element array , each object added arraylist of people. in other words, if 10 people input name , choices, print each person's name along choices in order entered them.

however, when run code, displays each different name under each name displays choices belong last person enter choices. example, if bob enters 3,2,1,6,5,4 ; tom enters 1,3,5,2,4,6; , bill enters 2,1,4,3,6,5, following displayed:

bob 2 1 4 3 6 5

tom 2 1 4 3 6 5

bill 2 1 4 3 6 5

obviously isn't right. don't understand why it's displaying each name displays entered choices each person. it's though getname() method in person class returns respective name each object getchoices() method returns recent choices array rather respective choices array each object. help? let me know if need clarify anything. in advance!

use below code in constructor.

 //constructor     public person(string myname, string[] myinput){         string copy = new string[6];         system.arraycopy(myinput, 0, copy, 0, myinput.length);         choices = copy;         name = myname;     } 

it happening due object referencing. basically, using same array object initialize of objects, means if that original object change, array objects in person objects changes. in above code, making fresh copy of array, , storing new array in person object.

hope helps.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -