Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

want to know how to pick details from collections

 
anveth kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic