• Post Reply Bookmark Topic Watch Topic
  • New Topic

Workings of System.arraycopy  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why the System.arrayCopy() is not working for array of arrays

like in this code




it is showing an error that arrayIndexOutOfBounds Exception?

 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool. Glad you could work it out.

Also remember that arrays of arrays are also objects. So you can also have arrays of arrays of arrays as follows.


And so on...
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to copy arrays of array to another arrays of array

int[][] a={{1,2,3,4},{2,6,7,8}};

to
int[][] b=new int[5][5]

?
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:why the System.arrayCopy() is not working for array of arrays

like in this code




it is showing an error that arrayIndexOutOfBounds Exception?



If you read my response again along with the Javadoc of the System.arraycopy method, you might get an idea. The javadoc is here. In that page scroll down to the description of the System.arraycopy method.

Read that whole thing.
It explains each of those parameters and it also says that

Javadoc of System.arraycopy wrote:
Otherwise, if any of the following is true, an IndexOutOfBoundsException is thrown and the destination is not modified:

The srcPos argument is negative.
The destPos argument is negative.
The length argument is negative.
srcPos+length is greater than src.length, the length of the source array.
destPos+length is greater than dest.length, the length of the destination array.


In your case what is the size of the source array? And what is srcPos + length?
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:How to copy arrays of array to another arrays of array

int[][] a={{1,2,3,4},{2,6,7,8}};

to
int[][] b=new int[5][5]

?


We will take this one after we've sorted your question 2. Ok?

And let's not have more than a single question in one forum post. It makes the forum posts very unreadable.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks to me like a new question so I have made a new thread for it.
Instead of System#arraycopy consider this.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok hi there

after following all your posts and reading
http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int)

i made this code



but it is printing folowing log



Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at org.kishor.oops.MultiArrayDemo.main(MultiArrayDemo.java:30)




It is showing error at line

System.arraycopy(arrFrom, 0, arrTo, 0,6);

any suggestions?


 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int) says

public static void arraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length)
Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest. The number of components copied is equal to the length argument. The components at positions srcPos through srcPos+length-1 in the source array are copied into positions destPos through destPos+length-1, respectively, of the destination array.


Now based on what we learned in your previous question ( remember we learnt how to calculate the size of an array in that question ), what should be the size/length of the array arrFrom ( arrFrom is your source array).

The last parameter of the method System.arraycopy is the length argument. So since you want to copy the whole source array into the destination array, this parameter's value should be equal to the length of the source array.

Now do you see the error?

The Javadoc of the method System.arraycopy also says that

Otherwise, if any of the following is true, an IndexOutOfBoundsException is thrown and the destination is not modified:

The srcPos argument is negative.
The destPos argument is negative.
The length argument is negative.
srcPos+length is greater than src.length, the length of the source array.
destPos+length is greater than dest.length, the length of the destination array.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:



but it is printing folowing log



Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at org.kishor.oops.MultiArrayDemo.main(MultiArrayDemo.java:30)




It is showing error at line

System.arraycopy(arrFrom, 0, arrTo, 0,6);

any suggestions?


The "from" array has a length of 2. The "to" array has a length of 4. Yeah, we know that there is another dimension to the arrays, but since it isn't the dimension being copied, we don't care about it.

And... you are trying to copy starting from the zeroth element (for both from and to), and for 6 elements. Can you not see why this won't work?

Henry
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

Here is I made few Change on my code I think Now value is copy




but I am getting following value on Output printed


[I@2207b0fb
[I@26f44031



How to print actual value in readable format?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:
but I am getting following value on Output printed


[I@2207b0fb
[I@26f44031



How to print actual value in readable format?


Array objects don't override the toString() method, they just print out the type and identity hashcode (which is done by the toString() method of the Object class). If you want it in another format, you will have to print it yourself. Cast the object back to an int[] and loop through it.

Henry
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
although arrayCopy works with Array but not with ArrayCopy.

In case of Array it is printing right value.


Please any more suggestion to sort out this?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:although arrayCopy works with Array but not with ArrayCopy.

In case of Array it is printing right value.



Please any more suggestion to sort out this?


Not sure what you are saying... can you elaborate a bit?

Henry
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry Problem Sorted out

here is code for future reader of this thread




and it works well and display actual values.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:How to print actual value in readable format?

Arrays.toString(array).

And while you're at it, have a look at the entire API docs for java.util.Arrays (←click), because it will also show you an entirely different way to copy arrays, multi-D or otherwise (actually several ways).

In fact, Kishor, I'd suggest that you have a good read of the tutorials, because they will guide you through the process of learning Java far better than we can by answering a pile of unrelated questions one at a time.

Winston
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… or Arrays.deepToString(arr) for arrays of arrays of any depth.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!