• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays copy : Clean way or API availaible  RSS feed

 
Sahil Reddy
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends

I tried finding the way to achieve the following but could not, could you please assist me in doing the following

1. I have two arrays Target and Source
2. I want to copy Source Array into Target Array starting from the given indices of Target Array.

Assume Target Array size >= Source Array

Fill method in Arrays class gives me an opportunity to add like this
Arrays.fill(target, 3, 6, 50);
which will add with all 50s starting from position 3

I want something like

Arrays.fill(target, 3, source);
so that it copies all the elements of Source to target starting from position 3;

Thanks and Warm Regards

 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the System class.
 
Sahil Reddy
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbelll

It is System.arrayCopy();

Dont you think it is misplaced ?
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is System#arraycopy.
I wouldn't have put it in the system class, but it is a bit late to do anything about it now.
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Historical note: The java.util.Arrays class was added to Java in 1.2, whereas the System.arraycopy method has been in Java since the beginning.

It would have been possible to move the method from System to Arrays as part of Java 1.2, but that would have broken uncountable programs. The Java people don't like to break people's code so they don't do that sort of change. That's why it's the way it is.

Note however that in 1.6 they added some copyOf methods to the Arrays class; they are vaguely similar to System.arraycopy.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think a lot of utility class methods were given the same names as in C/C++. C was introduced in 1972, when 16kB was a lot of memory. They had small screens and liked to keep the names of functions down to 3 characters it possible. That works beautifully for sin cos tan log but is not so good for maximum or minimum which should have been called larger and smaller really. C had a function called arraycopy (as far as I can remember), so they wanted to find somewhere to put it. As Paul C said, there wasn't an Arrays utility class to put it in in the earlier days, and there is no point in moving such a method.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: . . . They had small screens and liked to keep the names of functions down to 3 characters it possible. . . .
If you find an old (pre‑1980) C book, you can see they went to all sorts of trouble to keep their code short. You can see examples like…which are really difficult to understand, but save a few keystrokes.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!