Java ArrayList.add

Associate
Joined
27 Jul 2009
Posts
590
Location
UK
Hi, I am working on a couple of Java classes to be used with Selenium RC.

We've got some old Access databases and tables full information to be used in tests cases so I thought it would be a good idea to have a class with methods to make pulling information from Access easier and quicker.

All was going well until I made a method to provide an ArrayList containing String[] arrays (the String[]'s contain the fields in the row) to the TestNG DataProvider method.

Code:
[SIZE=2]String[] result = new String[rs.getMetaData().getColumnCount() + 1];[/SIZE]
[SIZE=2]ArrayList<String[]> results = new ArrayList<String[]>();[/SIZE]
[SIZE=2]while (rs.next()) {[/SIZE]
[SIZE=2]for (int colCounter = 1; colCounter <= columnCount; colCounter++) {[/SIZE]
 
[SIZE=2]if (rs.getString(colCounter) == null) {[/SIZE]
[SIZE=2]result[colCounter] = "";[/SIZE]
[SIZE=2]} else {[/SIZE]
[SIZE=2]result[colCounter] = rs.getString(colCounter);[/SIZE]
[SIZE=2]}[/SIZE]
 
 
[SIZE=2]}[/SIZE]
 
[SIZE=2]results.add(rowCounter, result); [/SIZE]
 
[SIZE=2]rowCounter++;[/SIZE]
[SIZE=2][B]System.out.println(result[3]);[/B][/SIZE]
 
[SIZE=2]}[/SIZE]
 
[SIZE=2]System.out.println("getRows Query: " + results.size() + " Results Returned.");[/SIZE]

System.out.println(result[3]);

Will output the third element, in this case 'firstName', correctly e.g.
GARY
JERRY
JO

However it seems all the String[] in my ArrayList contain the same array - the last one added.

If I access one using:

Code:
[SIZE=2]String[] rowToCheck = null;[/SIZE]
[SIZE=2]for (int i = 0; i < results.size(); i++){[/SIZE]
 
[SIZE=2]rowToCheck = results.get(i);[/SIZE]
[SIZE=2]System.out.println(rowToCheck[3]);[/SIZE]
 
[SIZE=2]}[/SIZE]

The same name is always displayed e.g JO, JO, JO.

Any ideas what I'm doing wrong?
 
After a quick scan I can't see the issue to be honest.

results.add(rowCounter, result);

I don't normally pass an integer when adding an object to an arraylist. That's the only thing I can think of :/
 
Thanks for the reply. I only added the int to the add call when I discovered the issue. The same is true of rowCounter as the while(rs.next) was taking care of the iteration.
 
Thanks for the reply. I only added the int to the add call when I discovered the issue. The same is true of rowCounter as the while(rs.next) was taking care of the iteration.

Well my first suggestion would be to add to replace

System.out.println(result[3]);

with
System.out.println(result[3]+"\t"+rowCounter);

So you can see if rowCounter is being incremented correctly (even though I think it is :))
 
Thanks again. Just to clarify rowCounter was a leftover from a previous method, I only used rowCounter when I encountered the issue. My original statement was

results.add(result);
 
Your are adding the same object "result" to your arraylist. When you change the value all the values in your arraylist are changed as they refer to the same object. Before adding the next result you need to create a new object to add.

So you need the line where you "new" your result object inside the for loop.
 
Your are adding the same object "result" to your arraylist. When you change the value all the values in your arraylist are changed as they refer to the same object. Before adding the next result you need to create a new object to add.

So you need the line where you "new" your result object inside the for loop.

:)

Thank you.
 
Back
Top Bottom