views:

54

answers:

1

Hi, I need to implement an ArrayList which can hold like person records. I can only do this so far:


CODE


import java.util.*;

class ArrayListDemo {

public static void main(String args[]) {

    ArrayList al = new ArrayList();
    System.out.println("Initial size of al: "
            + al.size());

    al.add("C");
    al.add("A");
    al.add("E");
    al.add("B");
    al.add("D");
    al.add("F");
    al.add(1, "A2");
    System.out.println("Size of al after additions: "
            + al.size());

    System.out.println("Contents of al: " + al);

    al.remove("F");
    al.remove(2);
    System.out.println("Size of al after deletions: "
            + al.size());
    System.out.println("Contents of al: " + al);
}
}

A member of this website told me to do the following, I really dont get how to get about doing that:


You do

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

and then you can repeatedly

newPerson = new Person("Bruce", "Wayne", 1972, "Gotham City");

myList.add(newPerson);

and you can access folks in the list by doing

int personNumber = 0;

Person retrievedPerson = myList.get(personNumber);

or even

for (Person someone : myList) {
   System.out.println(someone);
}

Any help as to complete a simple program utilizing the above points mentioned by that particular stackoverflow member (ie. answer by Carl Smotricz) would be appreciated.

Thanks alot

A: 

Go through this

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sl4jdemo;

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

/**
 *
 * @author Administrator
 */
public class arrayTest {

    public static class Person{
        String name;
        String SSL;
        String Street;
        Integer age;

        public String getSSL() {
            return SSL;
        }

        public void setSSL(String SSL) {
            this.SSL = SSL;
        }

        public String getStreet() {
            return Street;
        }

        public void setStreet(String Street) {
            this.Street = Street;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

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

        public Person(String name, String SSL, String Street, Integer age) {
            this.name = name;
            this.SSL = SSL;
            this.Street = Street;
            this.age = age;
        }

    }
    public static void main(String[] args) {
        List<Person> persons = new ArrayList<arrayTest.Person>();
        persons.add(new Person("Bob", "FKJHHS87546", "WALLSTREET", 21));
        persons.add(new Person("Tom", "SEFSDF875463", "WALLSTREET", 25));
        for (Person person : persons) {
            System.out.println("Name : "+person.getName());
            System.out.println("SSL : "+person.getSSL());
            System.out.println("Street : "+person.getStreet());
            System.out.println("Age : "+person.getAge());
        }
    }
}
taher
-1 for bad style: Ignoring naming conventions for classes and variables, needlessly nesting classes, not making member variables private.
starblue
The plural of *person* is *people*, not *persons*.
True Soft
this code was really helpful. Going to build up on that. Thks taher and Valentin for the edit
Haxed