Results 1 to 3 of 3

Thread: ArrayList

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    162

    ArrayList

    Hi
    Im having trouble getting this arraylist code working...

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

    Person.java

    Code:
    /*
     * 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

    Code:
    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

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: ArrayList

    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:
    Code:
    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());
            }
            
        }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    162

    Re: ArrayList

    Thanks it worked obvious mistake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width