This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

System.arraycopy VS toArray()  RSS feed

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry... i think there's one thread available for that under the name "Performance"
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!