Hi,

I wanted to know how I can add data to an ArrayList if the collection is not: Private ArrayList<String> files; and instead: Private ArrayList<People> files. Would the code look like this:

   public void addPerson(String name){

    files.add(name);

    }

or:

public void addPerson(String name) {

People people; // Declare an object
people = new People(name);  // Create an object

files.add(people);

}

Recommended Answers

All 8 Replies

Does the code compile without errors? The intent of generics was to allow the compiler to check if the correct data types are being added to the ArrayList.

The first code line doesn't work.

Is your question answered now?

No, not really.

Please explain what your problem is.
Ask some specific questions about what you are trying to do.

In above question do you want to add name in people object? instead of string?
please explain your question further.

It would probably llok like this:

 public void addPerson(Person p){
    files.add(p);
 }

As james said it is right.just go and try with this.

also mark this question as solved,if your doubt is cleared.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.