I'm facing the problem and I did not find for any exact explanation for it. I am running the program in debug mode, I see that when the variable add to g_temNodes arraylist: g_tempNodes.add(p_dept_cd); , that variable also automatically add to g_nodes before the running g_nodes.add(g_tempNodes); .
Pls give me some ways to solve it. Thanks in advance.
my code is as the followings.
ArrayList<ArrayList<String>> g_nodes = new ArrayList<ArrayList<String>>();
ArrayList<String> g_tempNodes = new ArrayList<String>();
...
...
private String GetDeptCd(String x_dept_cd, int x_flag) {
...
...
while (p_sql.next()) {
String p_dept_cd = p_sql.getString("dept_cd");
if(x_flag == 0){
g_tempNodes.removeAll(g_tempNodes);
}
g_tempNodes.add(p_dept_cd);
System.out.println("g_tempNodes = "+g_tempNodes);
g_nodes.add(g_tempNodes);
System.out.println("g_nodes = "+g_nodes);
GetDeptCd(p_dept_cd, 1);
g_tempNodes.remove(g_tempNodes.size()-1);
}
return null;
}
This is the output in console.
g_tempNodes = [100]
g_nodes = [[100]]
g_tempNodes = [100, 999]
g_nodes = [[100, 999], [100, 999]]
g_tempNodes = [100, 101]
g_nodes = [[100, 101], [100, 101], [100, 101]]
Since you're using the same g_tempNodes
always, the values are getting overwritten. Declare a new g_tempNodes
in the loop always
ArrayList<ArrayList<String>> g_nodes = new ArrayList<ArrayList<String>>();
// ArrayList<String> g_tempNodes = new ArrayList<String>(); // not needed here
...
while (p_sql.next()) {
ArrayList<String> g_tempNodes = new ArrayList<String>(); // new list created always
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.