ArrayList Unexpected Return
I'm attempting to return all the values in the array list I created and don't seem to be successful.
Here is my Student class.
public class Student
public String sName = JOptionPane.showInputDialog("Please enter a name.");
Here is my Main method.
public class Main
public static void main(String args)
ArrayList<Student> students = new ArrayList<Student>();
System.out.println("The size of the array is " + students.size());
When run, I am prompted for the name and told the correct size of the array. However, the name is always outprinted as [Student@9fef6f], no matter what I type in. I always get the correct array size, even if I add more students, so I don't think I am storing them wrong, but I don't know.
I have tried using the students.get(0);, but it still results in the same error. Perhaps the name I type in isn't even getting stored and only incrementing the array? :-?
Re: ArrayList Unexpected Return
When trying to print an Object using the method you are using
System.out.println(object);//where object is something that extends Object
the toString method is called. This by default returns : getClass().getName() + '@' + Integer.toHexString(hashCode())
which is usually - as in this case - meaningless to a user. To overcome this, you could override this method in your Student class to return a string that is more meaningful. This won't help with printing the ArrayList directly, but will help you print each object that the ArrayList returns.