Jump to content

Problem with making new objects in an ArrayList...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
So I've just gone back to my programming course after a 6 month gap and have forgotten some very basic things apparently. The following code is supposed to create an array list and add new instances of the class People to it:

        ArrayList<Person> people = new ArrayList();

        people.add(new Person ("Andrew", "Shearer", "AS10101"));

        people.add(new Person("Gordon", "Eccleston", "GE10101"));

        people.add(new Person("Ben", "Allen", "BA10101"));

        people.add(new Person("Ryan", "May", "RM10101"));

        for (int i = 0; i < people.size(); i++) {

            System.out.println(Person.toString());

        }

When running I get an error concerning the toString method saying I can't use a non-static method in a static context. To me it looks like this is because I haven't actually created instances of the object and I'm trying to envoke the toString method on the Person class rather than the objects. However, I can't seem to find how to fix this. Have I done something wrong in the .add lines or is it in the for loop?

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
It's in the for loop:
for (int i = 0; i < people.size(); i++)
{
    System.out.println(people.get(i).toString());
}
Person is a class name, not the instance of an object. That's why it gave you that error, since toString() isn't a static function.
Wow I changed my sig!

#3
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts

ZipOnTrousers said:

To me it looks like this is because I haven't actually created instances of the object

Actually, you did create them!

As of the error, in Java it is a little different from other languages, you should use the get and set methods.. and Persons is just a class, so you need to first get the instance of the class to actually have an output :)

#4
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
Yeah I see what I've done now, I didn't tell the program which part of the array I was using the method on either, I tried this method just after posting but forgot to use .get(i).

However, when i run the new code I don't actually seem to get any output, it just compiles correctly...

package playtime;

import java.util.ArrayList;

/**

 *

 * @author Gordon

 */

public class Main {


    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here


        ArrayList<Person> people = new ArrayList();

        people.add(new Person ("Andrew", "Shearer", "AS10101"));

        people.add(new Person("Gordon", "Eccleston", "GE10101"));

        people.add(new Person("Ben", "Allen", "BA10101"));

        people.add(new Person("Ryan", "May", "RM10101"));

        for (int i = 0; i < people.size(); i++) {

            System.out.println(people.get(i).toString());

        }

    }

}


#5
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
That is because.. what do you want to get? if you want to get the name you should create the getName method.. and then make

for (int i = 0; i < people.size(); i++) {
            System.out.println(people.get(i).getName());
        }

getName should return a string (the name of the person)

or something similar.. sorry, I haven't used java for a couple months now.

#6
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
I'm using an overloaded toString method that returns the three fields firstName, secondName and personID from each Person object. This overload method is written int he Person class. I don't know if this is how it would be done int he "realwolrd" but this is for a lab on my course, so it may just be to demonstrate overloading methods.

    @Override

    public String toString() {

        return "First Name: " + this.getFirstName() + "\n" +

                "Second Name: " + this.getSecondName() + "\n" +

                "Person's ID: " + this.getPersonID() + "\n" + "\n";

    }


#7
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
It's your ArrayList initialization. Since it's a Generic class, you need to specify for what type it is in the new initialization. Change this:
        ArrayList<Person> people = new ArrayList();
To This:
        ArrayList<Person> people = new ArrayList<Person>();
That should work. Javac also should have spit out a warning saying it wouldn't compile unless you used "-Xlint=unchecked", which means it wouldn't check for that.
Wow I changed my sig!

#8
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
Thanks guys, think I have this working now. The main thing that was confusing me was the syntax of choosing one part of the ArrayList so thanks for reminding me about using .get(i)!

#9
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Zeke, good eyes mate... didn't even notice that :)

@Zip: good luck :)