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

ArrayList conversion

 
Jonathan Pengelly
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a question about how to build my return array from my business layer search method. This method has the following basic structure:
1. Find all records that match the criteria (Using the Data class method and
so this returns an array of record numbers)
2. Read each of these records and check to see that they match the criteria exactly. If they do then add this contractor's details (as a string array) to an arrayList.
3. Convert this arrayList of string arrays to an array of string arrays (String [][]).
4. Return this string array.
My problem is that I am sure that there is an easy way to build the string [][] from the ArrayList of String []'s.
I thought I could do the following but it seems to throw a ClassCastException.
ArrayList matchingContractors;
String [][] returnArray;
.
.
// If a matching contractor then add the string details to the arraylist
matchingContractors.add(contractor.getDetailsAsStringArray);
.
.
// Convert ArrayList of matchingContractors to string array
returnArray = (String [][]) matchingContractors.toArray();
return returnArray;
Can anybody help me out?
 
Charlie Goth
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
 
Jonathan Pengelly
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep. I thought about this but then what happens if another client updates the object after it has passed the criteria check. The update might mean that the object doesn't pass the criteria anymore and I wouldn't want to return a non-matching record. That's why I thought I should add the string details of the contractor to the ArrayList.
Actually, now that you mention it this could be the case with my design anyway (An update occurs after the criteria check but before writing the contractor's details out to a string array). Perhaps I should lock the record before reading it.
Maybe this is too much? Thoughts from anyone?
 
Javini Javono
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
matchingContractors is an ArrayList. It is an array of Objects, though
you have decided to place in it a String[] for each of those objects,
which is fine.
So, since you already have a collection of String[], I'm not following
why you would need to convert it into String[][].
As far as the ArrayList is concerned, it is dumb, and it thinks that it
holds an array of Object's: you have placed one String[] object
into each element.

The above is a rough draft, untested, but it might give you
the rough idea to continue.
Thanks,
Javini Javono
 
Jonathan Pengelly
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Javini.
This is currently what I am doing. It just seems a bit long-winded and complex for such a simple task. That's why I have the feeling that there must be a nice way to do it. But maybe I am wrong and I need to review other parts of the design to avoid this issue.
Regards,
Jonathan
 
Charlie Goth
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jonathan Pengelly:

Actually, now that you mention it this could be the case with my design anyway (An update occurs after the criteria check but before writing the contractor's details out to a string array). Perhaps I should lock the record before reading it.
Maybe this is too much? Thoughts from anyone?

Surely this would mean that when searching every record would need to be locked (albeit 1 at a time), then kept locked (if it matches) until the data is returned to the client. The benefit is minimal too, as by the time the client uses the information any update that was locked out will have run, leaving the data out of date anyway.
Would you lose marks for return data that didn't match (as it was changed mid search)?
EDIT: If you were locking all records, searches could take ages as some records will already be locked, so you'd have to wait. The most common sense thing would be to return records that could potentially be non-matches. As long as you document the decision you should be fine *.
* I have no authority to say it'll be fine
[ February 29, 2004: Message edited by: Charlie Goth ]
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12012
216
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jonathan,
I think what you want is these three lines:

Funnily enough I remember having exactly the same issue when I did my assignment. I knew that I could manually add the items to an array in a loop (as has been suggested) but I felt that was too ugly. So I worked at it until I found the solution above. However when I went back to look at my code, I find that I do not have this code anymore. So whatever I needed for, I later decided was unnecessary, and I removed it.
Anyway, here is a test program so you can see that code in action. Just to confuse things, I have made the number of items in each String array variable, and I have added a recursive printing method, which prints the elements in the array of arrays in reverse order.

Confused yet?
Regards, Andrew
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course!

[ March 01, 2004: Message edited by: Vish Kumar ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic