aspose file tools*
The moose likes Beginning Java and the fly likes get name[i] from the for loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "get name[i] from the for loops" Watch "get name[i] from the for loops" New topic
Author

get name[i] from the for loops

Ween Li
Greenhorn

Joined: Aug 30, 2010
Posts: 4
Hi

How do i get the list of name[i] from the loops?

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Your question isn't clear. Can you elaborate a bit?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Virendrasinh Gohil
Ranch Hand

Joined: Jun 09, 2004
Posts: 46
Ween Li wrote:Hi

How do i get the list of name[i] from the loops?

From the code, it seems like, you are suffering from C-style-coding-syndrome. These detail can instead be wrapped into single clss and can make it easy to track. (May be array of objects of that class having details like, name, scholarship (boolean), gpa, programme, gender, etc...)

However, if I have to just answer your question, create another array of strings and copy the name from original array as and when your condition is satisfied. (for instance)
So, by end of the loop you will have all names of the people with qualifying criteria.

Or may be if I am understanding your business requirement right, you are interested in those i'th values of satisfying criteria so that you can get detail of that ith person using that 'i' (and that's where you should use more object oriented paradiam instead of tracking information of a person using 'i'th value in each arrays), you better define int array to store the ith position which matches the criteria. By end of the 'for' loop, you will have all 'i'th value stored in array. You can utilize that later to retrive whatever information you want from those arrays.

But believe me that is real real bad practice. (tracking using 'i'th value of an array) That makes your code vulnerable. What if one of the value in one of the array is rearranged? or one of the value is missing for one of the person? you will have complete mismatch.

OBJECT-ORIENTATION is the only answer.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Virendrasinh Gohil wrote:OBJECT-ORIENTATION is the only answer.

Hardly, but it's appropriate in some cases.
Virendrasinh Gohil
Ranch Hand

Joined: Jun 09, 2004
Posts: 46
David Newton wrote:
Virendrasinh Gohil wrote:OBJECT-ORIENTATION is the only answer.

Hardly, but it's appropriate in some cases.

I intended only for current implementation (specially when many arrays are tracked/mapped using single 'i'). I should have said, Object orientation is the only answer to make above problem simple. Happy to know if any better approach/paradigm is suitable on such situation.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Oh, okay. Agreed; each collection of information should be wrapped up in an object. (But still, for stuff like this, I'm still happy with a struct-like-thing rather than an object--there's more than one way to handle OO-like constructs! :)
Virendrasinh Gohil
Ranch Hand

Joined: Jun 09, 2004
Posts: 46
hmm.. right. So I think, to generalize, saying "encapsulation should be used" is better instead of saying Object-Orientated. (though encapsulation is one of the concept of OO).
 
wood burning stoves
 
subject: get name[i] from the for loops