File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arraylist and for enchanced loop question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arraylist and for enchanced loop question" Watch "Arraylist and for enchanced loop question" New topic
Author

Arraylist and for enchanced loop question

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486


Hello Ranchers,

have a doubt in above example.
I am using the for enchanced loop. Not understanding the thing that how come students(in this case is arraylist ) assign to Student(which is the class). Can anyone shed some lights on this please :?:


Thanks<br />Dinesh
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
The arraylist is a just a data structure to hold the Student Objects.
Now can't you iterate through a data structure with a data type which is stored in that?

In your case "A student object"

Or alternatively think like this,

You are having a list of Students


Dinesh Tahiliani wrote:

Hello Ranchers,

have a doubt in above example.
I am using the for enchanced loop. Not understanding the thing that how come students(in this case is arraylist ) assign to Student(which is the class). Can anyone shed some lights on this please :?:
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Didn't get you my friend. can you please expalin in simple terms with the example mentioned by me
:?:
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
Ok.

In your code you have created a Arraylist of Students ( think as collection of students ).

With this statement you are saying that you need a arrylist which you'll be using to store Student objects

Arraylist<student> = new Arraylist <student>();

Now you are adding to it Student objects.

# students.add(a);
# students.add(b);
# students.add(c)

If you want to iterate through it then you have to retrieve the objects.

| Student a | Student b | Student c |

Now think , if you remove something from this list, then what will come out? Of course a Student Object.

Is it clear now?

Dinesh Tahiliani wrote:Didn't get you my friend. can you please expalin in simple terms with the example mentioned by me
:?:
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952



for each loop internally uses Iterator of ArrayList, you can assume something like this:





SCJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arraylist and for enchanced loop question