• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 118
Android Objective C Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Objective C Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Saloon Keeper
Posts: 14263
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14263
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but that's no different from Arrays.copyOf() or System.arrayCopy().
 
There is no "i" in denial. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic