aspose 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
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply 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
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2701
    
    3

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 (Download) - the free mock exam kit for SCJP / OCPJP
Home Page -- Twitter Profile -- JavaRanch FAQ -- 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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Loop through the list of objects to get attribute.
 
Similar Threads
getting java.lang.NoSuchMethodError: main error! thanks =)
Anagram Finder program help
Linked List Problem
User input, change into an Array, send to a new method, then output results
Scanner.nextline() Help