This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 3013
    
  35

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.


Founder of ExamLab and Systemup
See how I can help you to become an awesome programmer
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.
 
Consider Paul's rocket mass heater.
 
subject: Loop through the list of objects to get attribute.
 
Similar Threads
Linked List Problem
Anagram Finder program help
getting java.lang.NoSuchMethodError: main error! thanks =)
User input, change into an Array, send to a new method, then output results
Scanner.nextline() Help