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 arrylist doubt 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 "arrylist doubt" Watch "arrylist doubt" New topic
Author

arrylist doubt

Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

hello,

Say i have class called A and it has variables called

int no;
string name;
int sal;
boolean bol;

and it it contains data for some 50 employees in the back end. and now i want to get the data from the back end and put it into arrylist before displaying to user.

A a;
List list = new ArrayList(a);

like this.

now if i want to disply the first five rows from the list. how to do? and say if i want to change the salary of 6th employee .. may i know how to change this here in list.. then i can directly send to the backend this list.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
List<A> list = new ArrayList<A>(); please.

You can use a for loop to iterate through an ArrayList and get the first 5 elements with the get() method. You can use list.get(5) to get the 6th element. Just don't try to get the first 5 elements if the list only contains 4 elements!
Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

Originally posted by Campbell Ritchie:
List<A> list = new ArrayList<A>(); please.

You can use a for loop to iterate through an ArrayList and get the first 5 elements with the get() method. You can use list.get(5) to get the 6th element. Just don't try to get the first 5 elements if the list only contains 4 elements!


this is not what i am asking about

how the data will be saved in the list object.
say

now i want to loop trough the list and get the 4th number employees detalis and print it on the web page..

please tell me if i want to get the 4th number employee.
A a;
List list = new ArryList(a);

how next???
Jilesh Lakhani
Ranch Hand

Joined: Jul 26, 2006
Posts: 49

Well, I'll suggest you to use the HashMap,

where the key will be the Employee Id and the Value would be the object A

so, if you want to show the details of employee 4,

get the Value from the HashMap which will be the Object A!!


-Jilesh
Universe and Knowledge has no bounderies
Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

Originally posted by Jilesh Lakhani:
Well, I'll suggest you to use the HashMap,

where the key will be the Employee Id and the Value would be the object A

so, if you want to show the details of employee 4,

get the Value from the HashMap which will be the Object A!!


please give me some code.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Originally posted by Rauhl Roy:


please give me some code.
No, please don't give him any code, Jilesh Lakhani. The idea of JavaRanch is for the ranchers to produce their own code, then they can ask for help.
Rauhl Roy, please show us a bit of your code so we can see how it is working.
Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

i want to know how the data is saved in arrylist only not the code.

i mean...
if my emptable is put into Arrylist.

how will it be saved like ?
in mutiple-d array it will be saved like
empno ename sal
[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]

and value of [0][0] equals EMP001
vaule of [0][2] equals 10,000

like that i want to know how in the ArrayList data is saved and how to retrive it?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3419
    
  12
Create an Employee class to hold the data and then store Employee instances in the ArrayList


Joanne
Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

yes this employee example is perfactly fine, say i have saved emp objects like emp1, emp2, emp3


i want to print them from the list only may i know what should i do?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3419
    
  12
Override the toString method to print whatever you need it to print and then loop thru the ArrayList calling
Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

thanks neal,

now i can print the values using toString , but one more doubt is that if i have added 100 emp details using below code and want to get salary of some 80th empployee what should i do? And does this concept apply to the whole collection frame work and objects?

[ March 19, 2008: Message edited by: Rauhl Roy ]
[ March 19, 2008: Message edited by: Rauhl Roy ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
list.get(79).getSalary();
Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

Originally posted by Campbell Ritchie:
list.get(79).getSalary();


i think this code does not work, myeclipse does not have list.get(79).getSal();

is there some thing other than this?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3419
    
  12
Originally posted by Rauhl Roy:


i think this code does not work, myeclipse does not have list.get(79).getSal();

is there some thing other than this?


Answered here
 
Don't get me started about those stupid light bulbs.
 
subject: arrylist doubt
 
Similar Threads
Setting ResultSet into an ArryList
ArryList of Objects
Sorting an Arrylist<MyType>
Converting resultSet to ArryList
what does it mean when an arryList returns []