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

NX: URLy Bird 1.3.1 - ArrayList to Array Conversion

 
Bharat Ruparel
Ranch Hand
Posts: 493
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Does anyone know an efficient way of converting an ArrayList which stores Integer objects to an array of ints?
I think I have a pretty cool
int [] find(String [] criteria)
method implementation for the Data class. Except, I cannot covert the ArrayList which gathers the "good" records stored in the ArrayList to int [] array. Here is my code fragment which makes me kinda mad!

You can see my sledge-hammer conversion. I know, performance is not the criteria, but there is got to be a better way than this. retVal is the ArrayList in the above example.
Thanks.
Bharat
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bharat,
I don't think you are going to get much improvement, after all an ArrayList can contain any sort of object, so there is no implicit way of converting an ArrayList of Integers into an array of ints.
Combining the two lines in your 'for' statement's block should give a tiny performance increase (no need for the intermediate object). But this would potentially make the code a little harder to read. And unless this is where you have determined that a bottleneck occurs, then you shouldnt bother trying to save the few milliseconds that this might give you.
Regards, Andrew
 
Bharat Ruparel
Ranch Hand
Posts: 493
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Andrew,
Thanks for reviewing the code fragment for me. I spent two hours last night trying to make this work with retVal.toArray() conversion. But it returns an array of Objects! I was just trying to make the code a bit more elegant. Most of the other pieces of code are, especially locking stuff. But I see your point; "keep your eyes on the ball and don't get carried away too much."
Regards
Bharat
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic