0

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


}
6
  • 2
    Are you getting an error or do you just want to understand the underlying workings of an arraylist? Commented Apr 6, 2013 at 23:08
  • 1
    I suggest you check out this official tutorial from Oracle for more information about Lists in general. Commented Apr 6, 2013 at 23:15
  • You haven't written any constructor that takes only an int/Integer as its param (you passed 5 in its constructor in the 2nd line of main). Were you trying to make a PhoneBook object array? Plus the fact that based on what you printed "Add Entries to the list", you should be calling setter methods and not getters. In the for-loop condition check, you should check for the ArrayList name.size(). Commented Apr 6, 2013 at 23:24
  • Well, I am getting several Errors. I know my code is crap. I fundamentally dont understand how to use the Array List in this way. Calling on an object from another class, and setting the inputs to each part of the object and adding it to the array. Its easy if you just want to predefine all the values and declare it all in the main class, but it becomes tricky, and requires (i think), more experience/knowledge than I have yet come to master. Unfortunately the java book I have, and the class I am taking dont force us to write test classes... So now when I have to do it this way, I am lost. :( Commented Apr 6, 2013 at 23:31
  • Avik, yes, I am trying to make a PhoneBook Object array. Commented Apr 6, 2013 at 23:44

2 Answers 2

1

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 );
    }
  }
}
Sign up to request clarification or add additional context in comments.

8 Comments

we have not been introduced to 'Lists' yet. This is simply using the Array List. So I am trying to just get an understanding of that at this time. I appreciate the comment though.
Not a problem. You can use ArrayList everywhere I put List. I'm just used to refering to interfaces instead of classes. You'll get there eventually!
Interesting... The program ran, but it never asks me for input. I am guessing 1 of two things... Should I create a new object every time the code runs, then use the 'add' function to then add those elements to that part of the array after getting input from the user? Is that what I am missing here?
new ArrayList<PhoneBook>( 5 ) isn't enough to make the ArrayList have 5 entries. You'll have to declare a size variable and use that when constructing the list and when looping over the inputs.
I see what you mean. If I run it as is, it seems to just run on forever, unless I hit ctrl-z and it bugs out. I just adjusted the counter to only run 5 times. where I have placed the value (5) is only an initial allocation?
|
0

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

4 Comments

No offense, but referring me to the definition of classes and iteration is not helping me to understand their implementation within a class as I have stated above. The majority of my confusion lies within how these get called/referenced. Their simply isnt the documentation to show this. Most of the java docs are too abstract for me to understand with my limited knowledge. The java tutorial provided above declares individual objects statically (1 at a time). whereas the program above is trying to create objects dynamically. I really think this should be a part of the java tutorial documents...
I thought you might be trying to actually learn Java. My mistake.
I am not trying to be ungrateful, I appreciate your input.
However, the java tutorial and the definitions stated dont answer my fundamental questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.