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?
prk_usaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.