• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

how to access the values

hi All,
I have a java bean as shown below:
package mybean;

public class MyBean

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

     public String getName()
     {
          return name;

    }

}

I have ajava class as shown below:
import java.io.*;
import java.util.*;
import java.util.Iterator;
class TestA

{
   public static void main(String args[])
   {
      mybean.MyBean mb1 = new mybean.MyBean("wilson");
       mybean.MyBean mb2 = new mybean.MyBean("john");
       mybean.MyBean mb3 = new mybean.MyBean("steve");

       java.util.ArrayList alt = new java.util.ArrayList();

       alt.add(mb1);
       alt.add(mb2);
       alt.add(mb3);

       System.out.println("arrayList:"+alt);
       
       Iterator itr = alt.iterator();
       
       while(itr.hasNext())
       {
             System.out.println("a:"+itr.next());
            
       }


   }


 }

how to print name ?
Can you guys plz help me out with this?
0
prk_usa
Asked:
prk_usa
  • 4
  • 2
1 Solution
 
contactkarthiCommented:

             System.out.println("a:"+((MyBean)itr.next()).getName());
0
 
contactkarthiCommented:
oops missed the package name


             System.out.println("a:"+((mybean.MyBean)itr.next()).getName());
0
 
prk_usaAuthor Commented:
Hi contactkarthi,
Thank for u r response.
when I tried I am getting an error as shown below:

java.util.NoSuchElementException
      at java.util.AbstractList$Itr.next(AbstractList.java:426)
      at TestA.main(TestA.java:34)
Exception in thread "main"
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
prk_usaAuthor Commented:
Hi contactkarthi,

my bad , I tried it again it works fine.

Thanks for your help.

Can you please explain why  are we supposed to  type cast ?

Thanks.
0
 
contactkarthiCommented:
arraylist stores it as an object
when you retreive it you have to type cast it as the particular type. then you can access the method inside
0
 
contactkarthiCommented:
if you use the latest version of java then the code will be like
between the <> we specify the exact type of object
java.util.ArrayList<mybean.MyBean> alt = new java.util.ArrayList<mybean.MyBean>();
 
       alt.add(mb1);
       alt.add(mb2);
       alt.add(mb3);
 
       System.out.println("arrayList:"+alt);
       
       Iterator<mybean.MyBean> itr = alt.iterator<mybean.MyBean>();
       
       while(itr.hasNext())
       {
             System.out.println("a:"+(itr.next().getName());
             
       }

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now