Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-06-01
4
Medium Priority
?
216 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:muthiahmerchant
4 Comments
 
LVL 14

Accepted Solution

by:
hoomanv earned 1000 total points
ID: 16809513
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
 
LVL 14

Expert Comment

by:StillUnAware
ID: 16809665
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
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 16810118
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
 
LVL 1

Author Comment

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

Thanks to all
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month10 days, 5 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question