Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store an object in ArrayList? [closed]

Tags:

java

I'm trying to store an object person in an ArrayList.

I have an ArrayList<String> customer = new ArrayList<String>();

The object 'person' has two variables: name (String) and pNr (long). I have tried to simply add it: customer.add(person) but that doesn't seem to work.

To clarify: I don't want to store the variables individually. I want to store the entire object person in the ArrayList.

I simply can't figure out how to engage this problem or even if it can be done.

Can someone point me in the right direction?

like image 423
tssmid Avatar asked Feb 01 '13 14:02

tssmid


6 Answers

Change your List such that it accepts a Person object instead of a String.

List<Person> customer = new ArrayList<Person>();
Person person = ......;
customer.add(person);
like image 51
PermGenError Avatar answered Oct 31 '22 06:10

PermGenError


Person Class

public class Person {       
    String name;
    long pNr;    

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getPNR() {
        return pNr;
    }

    public void setPNR(long pNr) {
        this.pNr = pNr;
    }   
}

Creating a Person Object

Person person = new Person();
person.setName("Name");
person.setPNR(pNr);

Adding to ArrayList

ArrayList<Person> personList = new ArrayList<Person>();
personList.add(person);

Retrieving Data

String selectedName = personList.get(i).getName();
like image 25
ThePCWizard Avatar answered Oct 31 '22 07:10

ThePCWizard


You need to use a List with a different generic type. Since the generic type specified on your current List is String it will only allow you to add objects of type String into the list.

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

I would also recommend using the List interface in case the need would arise to switch to a different implementation such as LinkedList.

like image 40
Kevin Bowersox Avatar answered Oct 31 '22 07:10

Kevin Bowersox


Look at this

import java.util.ArrayList;
import java.util.List;

public class Tester4 {

    public static void main(String args[]) {

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

        Person person = new Person();
        person.setName("Tester");
        person.setPersonNr(1245L);
        persons.add(person);

        person = new Person();
        person.setName("Tester 2");
        person.setPersonNr(1299L);
        persons.add(person);
    }
}

class Person {

    private String name;

    private Long personNr;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getPersonNr() {
        return personNr;
    }

    public void setPersonNr(Long personNr) {
        this.personNr = personNr;
    }

}
like image 26
Festus Tamakloe Avatar answered Oct 31 '22 07:10

Festus Tamakloe


ArrayList<String> customer = new ArrayList<String>();

in this way you defined that the arrayList customer accept type String only.

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

this will only accept object with Person type (and subType of Person).

like image 2
Kent Avatar answered Oct 31 '22 05:10

Kent


define a class Person

public class Person(){
    private String name;
    private long pNr;
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
    public long getPNR(){
        return pNr;
    }
    public void setPNR(long pNr){
        this.pNr = pNr;
    }
}

then you use a:

ArrayList<Person> customer = new ArrayList<Person>();
like image 1
Ste Avatar answered Oct 31 '22 07:10

Ste