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

Thread: Creating an Intstance in an ArrayList

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating an Intstance in an ArrayList

    Hi I am supposed to create a class and create a constructor and accessors and mutators. then i must create a program that crates 5 instance objects of the PhoneBook class and stores them in an ArrayList. then use a for loop to display all 4 instances in the Array List.

    The Problem i am having is actually hard coding the name and numbers in there and getting them to be displayed. I dont know where to create the instance and how.

    Here is my code for Phone Book class

    HTML Code:
    package Program_4;
    
    public class PhoneBook {
    
    	private String name;
    	private String number;
    	  public PhoneBook() { 
    		  name = null;       
    		  name = null;    
    		  }
    
    	
    public PhoneBook(String phonename, String phonenumber){
    	name=phonename;
    	number=phonenumber;
    }
    public void setname (String phonename){
    	name=phonename;
    
    }
    public String getname(){
    	return name;
    
    }
    public  void setnumber (String phonenumber){
    	number=phonenumber;
    }
    public String getnumber(){
    	return number;
    }
    
    
    }
    Here is my program

    HTML Code:
    package Program_4;
    import java.util.ArrayList;
    
    import javax.swing.JOptionPane;
    public class PhoneBookDemo {
    
    	
    	public static void main(String[] args) {
    	PhoneBook book=new PhoneBook();
    	ArrayList <PhoneBook> entries=new ArrayList<PhoneBook>();
    	entries.add("Greg","478-454-7605");
    	entries.add("Peter","478-448-7975");
    	entries.add("Bob","478-234-7650");
    	entries.add("John","478-423-7905");
    	entries.add("Jim","478-124-7235");
    	
    	
    	for (int x = 0; x < entries.size(); x++){
    	
    	JOptionPane.showMessageDialog(null, entries.get(x));
    	}
    	
    	
    	
    	}
    
    }


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Creating an Intstance in an ArrayList

     
    	ArrayList <PhoneBook> entries=new ArrayList<PhoneBook>();
    	entries.add(new PhoneBook("Greg","478-454-7605"));
    	entries.add(new PhoneBook("Peter","478-448-7975"));
    	entries.add(new PhoneBook("Bob","478-234-7650"));
    	entries.add(new PhoneBook("John","478-423-7905"));
    	entries.add(new PhoneBook("Jim","478-124-7235"));

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating an Intstance in an ArrayList

    Thank you that did work but everytime i try and print the results in JOPtionPane i get this as a result "Program_4.PhoneBook@863399"

    I dont know what this means

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Creating an Intstance in an ArrayList

    You need to write your own toString method. If you do not your class will inherit the toString method from Object and that is the exact output that method produces.
    Improving the world one idiot at a time!

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, 04:44 AM
  2. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM
  5. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM