PDA

Click to See Complete Forum and Search --> : ArrayList


carstenht
Sep 16th, 2007, 03:19 AM
Hi
Im having trouble getting this arraylist code working...

Have 2 classes, a person class and a main class.

Person.java


/*
* Person.java
*
* Created on September 16, 2007, 8:28 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package javaapplication1;

/**
*
* @author carsten
*/
public class Person {
private String name;
private int alder;

/** Creates a new instance of Person */
// Empty constructor
public Person()
{
name = "<ukendt>";
alder = 0;

}

// Specific constructor

public Person(String newName, int newAge)
{
name = newName;
alder = newAge;

}

/**
*get & set methods
*/

public String getName()
{
return name;
}

public int getAlder()
{
return alder;
}

public void setName(String newName)
{
name = newName;
}

public void setAlder(int newAlder)
{
alder = newAlder;
}

}



Main.java


package javaapplication1;
/*
* Main.java
*
* Created on September 16, 2007, 8:27 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.util.ArrayList;


/**
*
* @author carsten
*/
public class Main {

// Create new person
private static Person newPerson;
private static ArrayList<Person> person;
/** Creates a new instance of Main */
public Main() {

}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

newPerson = new Person();

person = new ArrayList<Person>();

// add persons

addPerson("Carsten Thomsen", 24);
addPerson("Maria Pedersen" ,21);

// print persons
printInfo();
}

public static void addPerson(String pName, int pAlder)
{

newPerson.setName(pName);
newPerson.setAlder(pAlder);

person.add(newPerson);

}

public static void printInfo()
{
for(Person p : person)
{
System.out.println("Info: " + p.getName() + ", " + p.getAlder());
}

}
}


the printInfo() method prints
Info: Maria Pedersen, 21
Info: Maria Pedersen, 21

instead of in the order i added. The error must be in the add but can figure it out.

Thx in advance

ComputerJy
Sep 16th, 2007, 12:47 PM
What you are doing wrong, is that you add the reference (Objects in java are references) to the arraylist then you add the same reference again.

When printing, the object only has it's latest values.

To correct that do something like:
public class Main{

// Create new person
private static ArrayList<Person> person;
/** Creates a new instance of Main */
public Main() {

}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

person = new ArrayList<Person>();

// add persons

addPerson("Carsten Thomsen", 24);
addPerson("Maria Pedersen" ,21);

// print persons
printInfo();
}

public static void addPerson(String pName, int pAlder)
{
Person newPerson=new Person();
newPerson.setName(pName);
newPerson.setAlder(pAlder);

person.add(newPerson);

}

public static void printInfo()
{
for(Person p : person)
{
System.out.println("Info: " + p.getName() + ", " + p.getAlder());
}

}
}

carstenht
Sep 16th, 2007, 01:39 PM
Thanks it worked ;) obvious mistake :p