Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Brain Twister

 
GD Deepz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I got a problem that cannot seem to be solved, a real brain twister. Consider the following:

public List findByCriteria(String[] criteria)
throws RemoteException, RecordNotFoundException {

//Executes properly
long[] recNos = db.findByCriteria(criteria);
List arrayList = new ArrayList();
String[] records = null;

for (int i = 0; i < recNos.length; i++) {
// executes properly
records = db.readRecord(recNos[i]);

// The following loop is a test and produces the result I want
// for (int count = 0; count < records.length; count++) {
// System.out.println(records[count]);
// }
arrayList.add(records);
}

//This test loop fails-- why? the ArrayList which stores records array only stores the last record. The ArrayList is of size 29 but each record array is composed of the "same" record i.e the last record
// Iterator i = arrayList.iterator();
// while (i.hasNext()) {
// String[] test = (String[]) i.next();
// for (int count = 0; count < test.length; count++) {
// System.out.println(test[count]);
// }
// }

return arrayList;
}

HELP!!
 
Dieskun Koper
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem is in your readRecord method.
I think you are not allocating a new String[] to put the fields in.

So the following is happening:

You load a record and put it in String[] records (you call it records, but it is just one record: one room or flight or contractor, right?).
You copy a reference to this String[] into your arrayList.
You change the elements of this String[] in your readRecord method, so the record in your arraylist changes too.

Solution:
Well, you can figure that out for yourself.

Good luck!
Dies
 
Inuka Vincit
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your read method is not returning the same string. Sollution is before you assign the string in the read method to make sure you call new and creat a new String array for each call. Remember java passes all object by referance (actually by value but value of the address). Used to do this the first few days I switched from c++ .
[ November 01, 2004: Message edited by: Inuka Vincit ]
 
GD Deepz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys and it was a stupid mistake, see too much of code makes one crazy
 
GD Deepz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry guys, even when I do this

//Executes properly
long[] recNos = db.findByCriteria(criteria);
List arrayList = new ArrayList();


for (int i = 0; i < recNos.length; i++) {

//New statement, it still does not work
String [] records = String [number of fields];

// executes properly
records = db.readRecord(recNos[i]);

arrayList.add(records);
}
 
Dieskun Koper
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see you can't figure it out for yourself.
Let us give you another hint.
There was nothing wrong with the code you showed us.
The problem is in the code you did not show us. Fix it there.

Good luck!
Dies
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are still stuck.
Give a
System.out.println("recNos.len=" + recNos.length);
after
long[] recNos = db.findByCriteria(criteria);

From your code it seems like the problem is inside db.findByCriteria

Jiju
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic