Jump to content

Unable to you class as type in ArrayList

OLS
Go to solution Solved by SCHISCHKA,

drop the .getName() function call.

 

persons.add(personOne);

This is my main class 

import java.util.ArrayList;

class PersonListe {
	public static void main(String[] args) {

		Person personOne = new Person("NameOne");
		Person personTwo = new Person("NameTwo");

		ArrayList<Person> persons = new ArrayList<Person>();

		persons.add(personOne.getName());
		persons.add(personTwo.getName());

		System.out.println(persons.size());
		persons.clear();
		System.out.println(persons.size());
	}
}

and this is my person class

public class Person{

    private String name; 

    public Person(String name){
        this.name = name; 
    }

    public String getName(){
        return name; 
    }
}

But I am unable to use the Person as type in the ArrayList, I haven't been doing to much programming and just doing some practice before exams and just want to clear this up before that. 

| Intel i5 4670k @ 4.3GHz | XFX RX 480 8 GB | Asus Z87i-Pro | 8.0 GB Kinston DDR3 | Samsung Evo 120GB SSD |

Link to comment
Share on other sites

Link to post
Share on other sites

You are trying to add strings to an array of Person class

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, SCHISCHKA said:

You are trying to add strings to an array of Person class

I know that is clear but I'm trying to figure out how to solve it, and am I even able to use Person as a type and not use String? Using String as the type works 

| Intel i5 4670k @ 4.3GHz | XFX RX 480 8 GB | Asus Z87i-Pro | 8.0 GB Kinston DDR3 | Samsung Evo 120GB SSD |

Link to comment
Share on other sites

Link to post
Share on other sites

drop the .getName() function call.

 

persons.add(personOne);

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, SCHISCHKA said:

drop the .getName() function call.

 

persons.add(personOne);

Thank you, that solved it. I guess that is because the getName is returning a String and is causing that problem?

| Intel i5 4670k @ 4.3GHz | XFX RX 480 8 GB | Asus Z87i-Pro | 8.0 GB Kinston DDR3 | Samsung Evo 120GB SSD |

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, OLS said:

Thank you, that solved it. I guess that is because the getName is returning a String and is causing that problem?

yes

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

I would advise to use somthing like :

 

List<Person> personList = new ArrayList<>();

 

Instead of using ArrayList as the type

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×