wood burning stoves*
The moose likes Beginning Java and the fly likes want to know how to pick details from collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "want to know how to pick details from collections" Watch "want to know how to pick details from collections" New topic
Author

want to know how to pick details from collections

anveth kumar
Greenhorn

Joined: Feb 03, 2008
Posts: 1
I had developed a particular student details(i.e id,name,sex) and i add into collection which contains all the student details.
my problem is if i want to pick a particular student detail by submiting student-id.
And this is my code:
ArrayList a1=new ArrayList();
a1.add("125");//student1-ID
a1.add("anveth");//student1-Name
a1.add("male");//Student1-gender
ArrayList a2=new ArrayList();
a1.add("126");//student2-ID
a1.add("divya");//student2-Name
a1.add("female");//Student2-gender
ArrayList a=new ArrayList();
a.addAll(a1);//adding my student1 collection
a.addAll(a2);//adding my student1 collection
if(a.contains("125")==true)
{
System.out.println(a.get(0)+" "+a.get(1)+" "+a.get(2));
}
//i want to print if i change id=126or 127....
please help me so that i can retrive detail for aparticular student
Satish Chilukuri
Ranch Hand

Joined: Jun 23, 2005
Posts: 266
You are doing things in a complicated way. A List generally should contain similar items. Here you are putting id, name and sex, which are three different elements in an ArrayList. A better way would be to create a new class, say Student, with id, name and sex as instance variables and getters/setters for them. Then for each student, create a new instance of Student class, set the attributes and then add that instance to a List.

To find a student with a particular id, loop through all elements of the list, check which one has the id you are looking for. An alternate approach is to use a Map collection (like HashMap) and add Student instances with id as the key. Searching for an element is a simple lookup operation on the Map.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
This has nothing to do with HTML and JavaScript, moving to JIGB.

[follow the huge click here link above to go there]

Eric
Shashank Agarwal
Ranch Hand

Joined: May 20, 2004
Posts: 105
I second Satish's comment. I'd also recommend coding in Java 1.5 or higher. That way, you'll be forced to use Satish's approach.
Satish Chilukuri
Ranch Hand

Joined: Jun 23, 2005
Posts: 266
Originally posted by Shashank Agarwal:
I second Satish's comment. I'd also recommend coding in Java 1.5 or higher. That way, you'll be forced to use Satish's approach.


Avneth is adding only String data types to the list. Even if you use generics, the code will be valid. Also Java 1.5 and higher do not force you to use generics. You can still use collections without generics and ignore compiler warnings or you can use something like ArrayList<Object> which allows all data types. It's up to the developer to the right thing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: want to know how to pick details from collections