array copy. Simple question.

R

Roberto Gallo

Say we have:

byte buf1[] = {1, 2, 3..};
byte buf2[];

And I want to make buf2 a copy of buf1 istead to reference it.
What would buf2 = buf1 do?
How to copy from one array to another?

Thank you.
 
C

Christophe Vanfleteren

Roberto said:
Say we have:

byte buf1[] = {1, 2, 3..};
byte buf2[];

The naming convention in java prefers declaring arrays as such (notice the
position of the square braces):

byte[] buf1 = {1,2,3...};
And I want to make buf2 a copy of buf1 istead to reference it.
What would buf2 = buf1 do?

That would make buf2 just another reference to the same array buf1 is
referencing (remember, variables != objects, they are just references to an
object).
How to copy from one array to another?

Thank you.

- Iterate over the original array in a for loop, and fill the second one
with the values of the first.

- Use System.arrayCopy(), which is very fast, but as it is written in native
code, don't use it for small arrays (the overhead of calling a native
method doesn't make up for the extra speed of the actual copying of the
array).
 
P

Pat Ryan

Roberto Gallo said:
Say we have:

byte buf1[] = {1, 2, 3..};
byte buf2[];

And I want to make buf2 a copy of buf1 istead to reference it.
What would buf2 = buf1 do?
that just makes buf2 a reference to the original array
How to copy from one array to another?
have a look at http://www.javapractices.com/Topic3.cjp
- it shows a number of ways including
cloning : int[] copy = (int[])aArray.clone();
using a system utility: System.arraycopy( aArray, 0, copy, 0,
aArray.length );
and a simple for loop

it also has a discussion on relative performance of the methods
 
C

Chris Smith

Roberto said:
Say we have:

byte buf1[] = {1, 2, 3..};
byte buf2[];

And I want to make buf2 a copy of buf1 istead to reference it.
What would buf2 = buf1 do?

It would make the two references point to the same array (i.e., what you
don't want).
How to copy from one array to another?

Here's one way:

buf2 = buf1.clone();

Here's another:

buf2 = new byte[buf1.length];
System.arraycopy(buf1, 0, buf2, 0, buf1.length);

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Matt Humphrey

Roberto Gallo said:
Say we have:

byte buf1[] = {1, 2, 3..};
byte buf2[];

And I want to make buf2 a copy of buf1 istead to reference it.
What would buf2 = buf1 do?

This makes the variable buf2 refer to the same array object that buf1 refers
to.
How to copy from one array to another?

buf2 = new byte [buf1.length];
buf2 = System.arraycopy (buf1, 0, buf2, 0, buf1.length);

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
D

Dale King

Chris Smith said:
Roberto said:
Say we have:

byte buf1[] = {1, 2, 3..};
byte buf2[];

And I want to make buf2 a copy of buf1 istead to reference it.
What would buf2 = buf1 do?

It would make the two references point to the same array (i.e., what you
don't want).
How to copy from one array to another?

Here's one way:

buf2 = buf1.clone();


Except that you need to cast the result:

buf2 = (byte[])buf1.clone();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top