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
LVL 1
muthiahmerchantAsked:
Who is Participating?
 
hoomanvCommented:
Object obj = arr.get(index);
((Student) obj).firstname;

it could be more elegant to use Generic ArrayList --> eliminating the cast

ArrayList<Student> arr = new ArrayList<Student>();
arr.get(index).firstname;
0
 
StillUnAwareCommented:
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
0
 
mukundha_expertCommented:
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;
      }
}
0
 
muthiahmerchantAuthor Commented:
solution provided by hoomanv although same as others was the first and solved my problem.

Thanks to all
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.