[b}Error : How to Avoid repeated data in ArrayList

Hi all ,

I am using List in my application to add and display data on the webpage. this part of code i am using in my application but i am getting result that is repeating the last row from the data base to the number of rows returned ....

////

List <tablejoins> join = new ArrayList <tablejoins> ();

List resul = new ArrayList();

for(int i =0 ;i<dept.size();i++){

curjoin.setDname(dept.get(i).getDname());

curjoin.setDeptno(dept.get(i).getDeptno());

curjoin.setEname(dept.get(i).getEmpCollection().get(i).getEname());

curjoin.setEmpno(dept.get(i).getEmpCollection().get(i).getEmpno());

join.add(curjoin);

// if(!resul.contains(join))

resul.addAll(i,join);

}

return resul;

//

Result is .....

If for example there are 3 rows returned from the database with different values then it is returning data with all 3 values of last row returned...

the result should be like this...

1, development , tom, 22

2,testing, john, 23

3 finance , xyz ,4

but i am getting result as :

3, finance , xyz, 4

3,finance, xyz ,4

3,finance, xyz ,4

Can any body help me to solve the problem

Thanks in advance....

Adil>

[1310 byte] By [Ready_to_learna] at [2007-11-26 16:55:30]
# 1
You only have a single "curjoin" object. You add 3 references to the same object to your list and you repeatedly reset its members.You need to create a new "curjoin" object each time around the loop (i.e. just inside the for statement)
dannyyatesa at 2007-7-8 23:23:12 > top of Java-index,Core,Core APIs...
# 2
hi dannyyates, Thanks for your reply i got the output ........ Your suggesstion just worked out ..thanks again..... Adil
Ready_to_learna at 2007-7-8 23:23:12 > top of Java-index,Core,Core APIs...
# 3
Dont do Add all ... thats the trouble
Goofy_111a at 2007-7-8 23:23:12 > top of Java-index,Core,Core APIs...
# 4
ya i just deleted the addAll code from the function and i am getting the output but i am getting an error of Array out of bound exception if i am getting rows more than 4 rows i dont know the problem .....
Ready_to_learna at 2007-7-8 23:23:12 > top of Java-index,Core,Core APIs...
# 5
First, start by awarding me your dukes for solving your original problem... :-)Second, there are no arrays in the code you post, so what you have posted is not the same code that is giving you AIOOBE.Re-post your current code (and use the code tags so it's readable).
dannyyatesa at 2007-7-8 23:23:12 > top of Java-index,Core,Core APIs...
# 6

hey dannyyates

My Code is as follows :public List <CursorJoins>joinstabs(){

System.out.println("Calling DEPT ...");

List <Dept> dept= cursorUsingWhere();

System.out.println("Res got "+ dept);

List <CursorJoins> join = new ArrayList();

List resul = new ArrayList();

for(int i =0 ;i<dept.size();i++){

/// New object created ....

CursorJoins curjoin=new CursorJoins();

try{

///Earlier i was getting 4 rows now after making changes i am getting 23 but it is giving an //error as Array out of boundException

curjoin.setDname(dept.get(i).getDname());

curjoin.setDeptno(dept.get(i).getDeptno());

curjoin.setEname(dept.get(i).getEmpCollection().get(i).getEname());

curjoin.setEmpno(dept.get(i).getEmpCollection().get(i).getEmpno());

join.add(curjoin);

}

catch(Exception u ){

System.out.println("error"+u.toString());

}

}

System.out.println("Returning");

return join;

}

Earlier i was getting 4 rows now after making changes in my PL/SQL Codei am getting 23 rows but it is giving an error as ArrayoutofboundException...

Any Help ....

Thanks ..

I am sorry i am a new to the forums i have less Dukes with me .... ;)>

Ready_to_learna at 2007-7-8 23:23:12 > top of Java-index,Core,Core APIs...
# 7
Any one to help me out...
Ready_to_learna at 2007-7-8 23:23:12 > top of Java-index,Core,Core APIs...
# 8

There is no such thing as an ArrayOutOfBoundsException. There is an ArrayIndexOutOfBoundsException, and an IndexOutOfBoundsException.

You need to be absolutely precise about the exact exception and the text of the exception, and, ideally, a stack trace.

I suggest you cut-and-paste it.

dannyyatesa at 2007-7-8 23:23:12 > top of Java-index,Core,Core APIs...
# 9
Yeah i did that post hurriedly yes it is errorjava.lang.ArrayIndexOutofBoundException: Array index out of range :3thanks
Ready_to_learna at 2007-7-8 23:23:12 > top of Java-index,Core,Core APIs...
# 10

Still seems a bit odd because the standard list implementations don't throw that exception, and there are no arrays in your code. However, could this be the problem:

> curjoin.setEname(dept.get(i).getEmpCollection().get(i).getEname());

> curjoin.setEmpno(dept.get(i).getEmpCollection().get(i).getEmpno());

Here, you're saying 'get the i-th record from the result set and then get the i-th employee' (by the looks of it). If the given department doesn't have enough employees, this will fail. Should the second get() on each line actually be get(0) or something like that? What if a department has no employees registered in the database?

dannyyatesa at 2007-7-8 23:23:12 > top of Java-index,Core,Core APIs...