Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arraylist and for enchanced loop question

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 :?:
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't get you my friend. can you please expalin in simple terms with the example mentioned by me
:?:
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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




 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic