Just coming back to Java after a few years break. I am trying to select elements from one array and store them in another in Java. I have created a new array of the same type with a fixed number of elements. The array that I am copying from is not null I have printed it out. But when I try to display the new array the values are not there - just a reference to the element. There probably something that I have overlooked. I have been searching for the last day but am not getting anywhere. I would really appreciate some help. Code below:
PersonDetails user = new PersonDetails(userName,userGender,userAge,userInterests);
PersonDetails [] userArray = new PersonDetails [numberOfDaters];
PersonDetails [] dateArray = new PersonDetails [numberOfDaters];
userArray = user.getArray("datingdata.txt", numberOfDaters);
dateArray = Arrays.copyOf(userArray, userArray.length);
char [][] interestArray = new char[numberOfDaters][5];
for (int z =0;z<userArray.length; z++) {
interestArray[z] =
userArray[z].getAllInterests( userArray[z].getInterests());
}
String remove = user.getOnes(interestArray);
System.out.print(remove);
StringTokenizer st = new StringTokenizer(remove);
int num = st.countTokens();
PersonDetails [] userRemoveArray = new PersonDetails [num];
while(st.hasMoreTokens()) {
int token = Integer.parseInt(st.nextToken());
for(int x =0;x<userRemoveArray.length;x++) {
userRemoveArray[x] = userArray[token];
}
System.out.println(userRemoveArray);
}
The output is as follows:
[LPersonDetails;@a8c488
[LPersonDetails;@a8c488
[LPersonDetails;@a8c488
[LPersonDetails;@a8c488
[LPersonDetails;@a8c488
[LPersonDetails;@a8c488
[LPersonDetails;@a8c488
[LPersonDetails;@a8c488
[LPersonDetails;@a8c488
[LPersonDetails;@a8c488
[LPersonDetails;@a8c488
Thanks in advance
You can use the .addAll(...)
method or one of the many methods found here .
As for printing out the elements of the array... you will have to implement your own .toString()
method to avoid displaying the object reference.
Here is a small example to make it more easier for you since you had noted that you are just coming back to Java after sometime and i hope this helps you out :)
public class PersonDetails {
private String userName;
private String gender;
@Override
public String toString() {
return "PersonDetails [userName=" + userName + ", gender=" + gender
+ "]";
}
}
Now when you print out the Array, the String created within the toString() method will print for each PersonDetail object.
You should use System.out.println(Arrays.asList(userRemoveArray));
. or
for(int i = 0; i < userRemoveArray.length;i++) {
System.out.print(userRemoveArray[i]+", ");
}
System.out.println();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.