• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the practical usage of arraycopy method in real world scenarios  RSS feed

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

while dabbling with array's came across arraycopy method. Wrote a small program while practicing the method which is as follows..



the out put is : caffein(where str1 is the source,2 is the position from which the char data is to be picked up,str is the destination,destination position,length)

What is the practical implementation of this method in real-world problems.

Thanks



 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you want to handle string manipulation yourself (want to have full control over string generation), rather than handling the control to internal JDK API method....
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be due to Performance issue ... you may have a piece of code which call internal JDK API method (or any 3rd party API method ) for string generation and that method is taking too much time ... so you want to write your own String generation method to overcome that performance bottleneck ...
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Abhay,

Thanks for the reply.

I was just using(calling) the arraycopy method which is an inbuilt method of java.util.Array . Here also just like the example you have mentioned, i am calling the method or rather i am using the method from the class. What could be the difference.
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Writing your own implementation (for which there is already an established method in JDK API) can have disadvantages - your method may have bugs where as JDK API method is already tested by Java API creators....
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:
What is the practical implementation of this method in real-world problems.


Its simple, you can use it anywhere you want a subset of an array. Its a utility method intended to help you with a common task.
Why is it that you want an actual real world example for this ?

At the back of my head the only example comes to mind is that suppose you had an array of integers or similar and you wanted the top 5 elements from a list ...
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:Hi

Abhay,

Thanks for the reply.

I was just using(calling) the arraycopy method which is an inbuilt method of java.util.Array . Here also just like the example you have mentioned, i am calling the method or rather i am using the method from the class. What could be the difference.


System.arrayCopy() provides the basic copying mechanism. You can use it to copy values from one array to another. It's faster than doing it manually with a loop, because it uses native code that probably interacts with the memory directly.

Arrays.copyOf() and Arrays.copyOfRange() are convenience methods that copy your values to a new array. you should prefer this method if you don't have a target array yet, as it's more clear.

You can also call clone() on an array if you want a copy of the array without resizing it.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:. . . You can also call clone() on an array if you want a copy of the array without resizing it.
But you only get a shallow clone.
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but that's no different from Arrays.copyOf() or System.arrayCopy().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!