user2253304
user2253304

Reputation: 3

Using Array Lists and Creating Objects

I am struggling with being able to understand how adding objects to an array list works, and the associated syntax.

Reviewing array lists in the Java, "How to Program" 9th edition. It doesnt clearly state how you add objects to an array list from a test class. I simply dont understand how they are passed/added.

In my case, I am using a class Phonebook.java to define a default and non default constructor, and using a Test class to add those objects to an array list.

My question is on, what is the process for adding those objects in the Test Class, and how do you use array lists to work with or initialize those objects from the PhoneBook class?

My Code below so far.

Phonebook.java ->

public class PhoneBookTest {

public static void main (String [] args)
{

    Scanner input = new Scanner (System.in);

    ArrayList < PhoneBook > directory = new ArrayList <PhoneBook>(5);

    System.out.println ("Welcome to your Phone Book");
    System.out.println ("Add Entries to the list");
    System.out.println ();

    PhoneBook x;
    String num = null;
    String name = null;

    for (int i = 0; i < 5 ; i++)
    {

        System.out.println ("Enter Name: ");
        name = input.nextLine();
        System.out.println();

        System.out.println ("Enter Number: ");
        num = input.nextLine();
        System.out.println();

        PhoneBook newEntry = new PhoneBook (name, num);
        directory.add (newEntry);
    }


}

Upvotes: 0

Views: 4141

Answers (2)

mconlin
mconlin

Reputation: 8733

In your loop you are reference

Phonebook.getName() in an effort to set it.

Your code needs to get to an instance of Phonebook, not reference it statically. You also need to loop the list, not the class Phonebook.

   for (int i = 1; i < = directory.size(); i++)
   {
   ((Phonebook) directory.get(i)).setName("setting name to this text!");

You can also iterate the list like this:

   for(Phonebook myphonebook : directory)

I think you should read up on the basics of Java classes and Iteration.

Try this: Lessons on Java

Upvotes: 0

Steven Hood
Steven Hood

Reputation: 688

Adding objects to any List (ArrayList is just one implmenetation of list) uses the add method. In your example, adding every entry to the end of the ArrayList, PhoneBookTest would look something like this:

class PhoneBookTest
{
  public static void main( String[] args )
  {
    List<PhoneBook> phoneBooks = new ArrayList<PhoneBook>( 5 );
    Scanner input = new Scanner (System.in);

    System.out.println ("Welcome to your Phone Book");
    System.out.println ("Add Entries to the list");
    System.out.println ();

    for (int i = 1; i < = phoneBooks.size(); i++)
    {
        System.out.println ("Enter Name: ");
        String name = input.nextLine();
        System.out.println();
        System.out.println ("Enter Number: ");
        String number = input.nextLine();
        System.out.println();

        PhoneBook newEntry = new PhoneBook( name, number );
        phoneBooks.add( newEntry );
    }
  }
}

Upvotes: 1

Related Questions