0

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

}

Edited by KRUX17

4
Contributors
8
Replies
9
Views
4 Years
Discussion Span
Last Post by jalpesh_007
0

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.

1

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

Edited by NormR1

0

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

0

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

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.