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 3 of 3

Thread: How do I return an array list?

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question How do I return an array list?

    Ok, so to ensure everything is working ok, i want to be able to return my data from my array list. The code is below and if anyone can tell me how i could return the data (preferabley by printing it. Thanks

    import java.util.ArrayList;
     
    public class ContactList
    {
        private ArrayList<Contact> contactList;
        private ArrayList<Contact> aContact;
        private String contactType;
     
        public ContactList()
        {
     
            contactList = new ArrayList<>();
            aContact = new ArrayList<Contact>();
            ArrayList<Contact> contactList = new ArrayList<Contact>();
        }
         public void addPersonalContact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC, String phonenumberEmail)
        {
            contactList.add(new AddPersonalContact(fName, sName, street, town, partOnePC, partTwoPC, phonenumberEmail));
     
     
        }
     
     
     
        //public void addBusinessContact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC, String phonenumberEmail)
        //{
          //  contactType = "Business";
            //aContact.add(new Contact(fName, sName, street, town, partOnePC, partTwoPC, phonenumberEmail));
     
     
     
        //}
     
     
     
    }
    Last edited by 93tomh; July 26th, 2012 at 12:12 AM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How do I return an array list?

    All you have to do is go through the list and System.out.println each one, which is how i used to do it long ago, but most of the time these days i find myself watching things in the debugger

    If you don't know how to go through each element in your array list, look up the iterator class

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: How do I return an array list?

    public ArrayList/*<type>*/ methodName(/* parameters */)
    	{
    		ArrayList arraylist = new ArrayList();
    		/**
    		 * code here
    		 */
     
    		return arraylist;
    	}
     
    	/**
    	 * or
    	 */
     
    	public ArrayList arrayList = new ArrayList();
     
     
    	public ArrayList/*<type>*/ method2Name(/* parameters */)
    	{
    		return arrayList;
    	}

    You should read up on returning something. If you are using a method, the second word typed is what is returned. Void means it returns nothing, boolean means you have to return a boolean. int means you have to return an int and so on. It can be done with any object you create. Try experimenting with methods that return something. For instance, in ArrayList, the size() method returns an int of how long the ArrayList is.


    public void name(String name){System.out.println(name);}
     
    //could be
     
    public String name(String name)
    {
        return name;
    }

    What is returned is completely up to you and the purposes of the method you are writing.

Similar Threads

  1. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 05:12 PM
  2. method to return the position of the smallest item in an array
    By izzahmed in forum Java Theory & Questions
    Replies: 5
    Last Post: November 4th, 2011, 04:18 AM
  3. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 10:50 AM
  4. Search for number in array and return index
    By Kevinius in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2011, 12:00 AM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM