0

My program has an Object Class Person. A Person object consists of a String name (which is the name of a person) and a 6 element string array choices. The array holds the values that the user enters to rank 6 different sports from most preferred to least preffered.

For example, a Person object called testPerson could consist of a String set as "Bob" and an array with each element as follows {3,1,4,2,5,6}. (The second sport that was listed was Bob's most preferred sport and the last sport listed was his least preferred).

That example object can be created with the following code:

String[] tempArray = new String[]{"3","1","4","2","5","6"};
Person newPerson = Person("Bob",tempArray);

Here is the 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 the choices array
    public String[] getChoices(){
        return(choices);
    }
    //returns the name
    public String getName(){
        return(name);
    }
}

I create a new object every time a new user enters their name and choices. Since I don't know how many times a new user will enter their details, I store each new object within an ArrayList called "people". (Yes, I know... An array within an object within an ArrayList)

After all (unknown number of) users have entered their information a button is pressed indicating that there are no more users to enter information. At this point I need to access both the name and the choices array for each person object To do this I would obviously use a for each loop. This is how I set it up:

String[] myChoices = new String[6];
String myName;
for(Person getPerson: people){
    myChoices = getPerson.getChoices();
    myName = getPerson.getName();
    //print name and choices
    System.out.println(myName);
    for(int i = 0; i < 6; i ++){
        System.out.println(myChoices[i]);
    }
}

That "for each" loop should print the name and each element of the 6 element array and it will do it for each object that was added to the ArrayList of people. In other words, if 10 people input their name and their choices, it would print each person's name along with their choices in the order they entered them.

However, when I run the code, it displays each different name but under each name it displays the choices that belong only to the last person to enter their choices. For example, if Bob enters 3,2,1,6,5,4 ; Tom enters 1,3,5,2,4,6; and Bill enters 2,1,4,3,6,5, the following will be displayed:

Bob 2 1 4 3 6 5

Tom 2 1 4 3 6 5

Bill 2 1 4 3 6 5

Obviously that isn't right. But I don't understand why it's displaying each name properly but it only displays the most recently entered choices for each person. It's as though the getName() method in the Person class returns the respective name for each object but the getChoices() method returns the most recent choices array rather than the respective choices array for each object. Could anyone help? Let me know if I need to clarify anything. Thanks in Advance!

1
  • You didn't show how you create all the Person objects expect the first one. I suspect you are using the same choices array for all of them.
    – Eran
    Jun 1, 2015 at 5:24

2 Answers 2

1

Use below code in your 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 all was happening due to your object referencing. Basically, you are using same array object to initialize all of your objects, which means that if that original object change, the array objects in all the Person objects changes. In above code, you are making a fresh copy of that array, and then storing that new array in your Person object.

Hope this helps.

1
  • Worked Perfectly! Any chance you can explain to this java noob why that works?
    – bloo
    Jun 1, 2015 at 5:34
1

Most probably this is an assignment problem, you're using the same array for all persons, so the array reflects the last value entered. Either create a new array for each entry or copy the array into the object during construction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.