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);
}
}