File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Loop through the list of objects to get attribute. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loop through the list of objects to get attribute." Watch "Loop through the list of objects to get attribute." New topic
Author

Loop through the list of objects to get attribute.

Kris Danielewski
Greenhorn

Joined: Apr 01, 2009
Posts: 25
I have a class Student


and another class with list.


I am trying to loop through the List to find student == my Entered student number.



How can i list through the list??
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3100
    
  40

Well, you can use a for-each loop for this purpose like below:



But this is not a good practice, because you can use Map classes instead of those List classes, so you can get the student quickly and in a smart-way without having a loop.

Devaka.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Kris Danielewski
Greenhorn

Joined: Apr 01, 2009
Posts: 25
hmm

I have problem , it repeats enterMarkC for every Student in the list, check for student numbber work...
Currently I am trying to set the attribute of Student class (courseworkMark) by user input.
Looking forward for help with it.
Kris Danielewski
Greenhorn

Joined: Apr 01, 2009
Posts: 25
Really need help with it , thanks...
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
I would guess that what number equals after the statement "number = sc.next()" and what is returned by "if(s.getStudentNo().contains(number))" are not behaving the way you would like them to. Add some print statements to double check your input value and expected results. Also, I would add a break in your loop once you find the correct Student.

If this is for a class and you are allowed to go further I would definately use Devaka's suggestion to use a Map to make this much much easier. Each Student has a unique number... so you could skip the loop and find the correct Student in one statement using a Map.

HTH GL


SCJA
~Currently preparing for SCJP6
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Kris,

I'll third Brian and Devaka's suggestion to use one of the Map classes instead of a List. Lists are useful when ordering matters, like in a queue. Maps are much better when it comes to searching.

John.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loop through the list of objects to get attribute.