Link to home
Start Free TrialLog in
Avatar of muthiahmerchant
muthiahmerchant

asked on

How do i access a variable in an object which is in a arraylist

Hi,

I am arraylist say results. This list holds students object. each student object has a firstname, lastname.

how do i access the value of firstname of student object inside the arraylist.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of hoomanv
hoomanv
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If You don't use generics, then You'll have to cast the type of Object to the one You had before putting it into ArrayList. Here how it is used:

ArrayList al = new ArrayList();
Integer int = new Integer(4);
al.add(int);
Object o = al.get(0); //this returns an object, without the real type of it
Integer trueValue = (Integer)o; //so You must to cast the type back to what it was before

another option is to use generics since java 1.5:

ArrayList<Integer> al = new ArrayList<Integer>(); //only Integer objects will be allowed, including primitive type 'int'. This is possible because of autoboxing feature.
al.add(4);
Integer i = al.get(0); //and this works.

to get more info on generics:
    http://java.sun.com/docs/books/tutorial/extra/generics/index.html
autoboxing:
    http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html
casting:
    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/other.html
Avatar of mukundha_expert
mukundha_expert

If your class variables are private then you can have getters and setters for the variables, How
ever if they are not private you can acces them with the instance of the student. ex. stud.firstName , stud.lastName.

import java.util.ArrayList;
import java.util.ListIterator;

public class Lis {
      public static void main(String[] args){
            ArrayList list = new ArrayList();
            list.add(new Student("jack","mukundh"));
            list.add(new Student("john","Abraham"));
            ListIterator iterator = list.listIterator();
            while(iterator.hasNext()){
                  Student stud = (Student) iterator.next();
                  System.out.println("First Name -->"+ stud.getFirstName());
                  System.out.println("Last Name -->"+ stud.getLastName());
            }
      }
}
class Student
{
      private String firstName;
      private String lastName;
      
      public Student(String firstName,String lastName){
            this.firstName = firstName;
            this.lastName = lastName;
      }
      public String getFirstName() {
            return firstName;
      }
      public void setFirstName(String firstName) {
            this.firstName = firstName;
      }
      public String getLastName() {
            return lastName;
      }
      public void setLastName(String lastName) {
            this.lastName = lastName;
      }
}
Avatar of muthiahmerchant

ASKER

solution provided by hoomanv although same as others was the first and solved my problem.

Thanks to all