Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: ArrayList Unexpected Return

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

    import javax.swing.JOptionPane;
                public class Student 
                {
                     public String sName = JOptionPane.showInputDialog("Please enter a name.");
    	    }

    Here is my Main method.

    import java.util.*;
               public class Main 
               {
     
    	   public static void main(String[] args)
    	   {
    		ArrayList<Student> students = new ArrayList<Student>();
    		students.add(new Student());
    		System.out.println("The size of the array is " + students.size());
                    System.out.println(students);
     
    	  }
     
    }

    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?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,334
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default 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.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 03:44 AM
  2. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM
  3. Prime number generator program missing return statement
    By 03EVOAWD in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 10th, 2009, 09:17 AM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. Error of "Class has no return statement"
    By mdstrauss in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 2nd, 2009, 12:00 PM