aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes ArrayList conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "ArrayList conversion" Watch "ArrayList conversion" New topic
Author

ArrayList conversion

Jonathan Pengelly
Greenhorn

Joined: Jan 17, 2004
Posts: 19
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

Joined: Feb 26, 2004
Posts: 60
Try this:


SCJP (77%)
Jonathan Pengelly
Greenhorn

Joined: Jan 17, 2004
Posts: 19
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

Joined: Dec 03, 2003
Posts: 286
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

Joined: Jan 17, 2004
Posts: 19
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

Joined: Feb 26, 2004
Posts: 60
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

Joined: Mar 28, 2003
Posts: 11479
    
  94

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


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
Of course!

[ March 01, 2004: Message edited by: Vish Kumar ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList conversion