| Author |
System.arraycopy VS toArray()
|
Charith Fernando
Ranch Hand
Joined: Sep 12, 2005
Posts: 67
|
|
hi all, i have a requirement where i get a set of id's and i have to search for the results and make an array out of the results... 1) one way to do this is by adding the results into a List and then at the end convert the list into an array by using the toArray() method. 2) the other way is to define the array to be the size of the ids array and then add the results into it and remove the unused elements of the result array at the end by doing an arraycopy... which method is more efficient?
|
Charith I Fernando<br />SCJP5, SCWCD, SCBCD, BSc(Hons) IS<br />+94 773 263 222 (mobile)
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
This doesn't seem to have anything to do with SCJP, so let's move it to Java in General (Intermediate)... [ October 28, 2006: Message edited by: Jim Yingst ]
|
"I'm not back." - Bill Harding, Twister
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
|
Seems like the two techniques are pretty close. The first seems a little more natural to me, probably takes a little less code. Having to "remove" the unused array slots in the second technique seems like you're giving yourself an extra opportunity to make a silly coding mistake along the way - why bother, when ArrayList already takes care of such details?
|
 |
Charith Fernando
Ranch Hand
Joined: Sep 12, 2005
Posts: 67
|
|
so does that mean in performance wise both are same? as in you dont have a performance hit in either way? would like to propose to open up a seperate section for performance tuning discussions... because we have to promote the most efficient way to do certain processes.. Thank you,
|
 |
Charith Fernando
Ranch Hand
Joined: Sep 12, 2005
Posts: 67
|
|
|
sorry... i think there's one thread available for that under the name "Performance"
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Hi Charith, the very first rule to learn about performance optimization is that you should only optimize code that you have identified to be a performance bottleneck. Everything else will just take away time from caring about the important aspects of development.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
 |
|
|
subject: System.arraycopy VS toArray()
|
|
|