[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>
# 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 .... ;)>
# 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.
# 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?