Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - array copy. Simple question.

 
Thread Tools Search this Thread
Old 01-27-2004, 07:23 PM   #1
Default array copy. Simple question.




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.




Roberto Gallo
  Reply With Quote
Old 01-27-2004, 07:35 PM   #2
Christophe Vanfleteren
 
Posts: n/a
Default Re: array copy. Simple question.
Roberto Gallo wrote:

>
>
> 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).


--
Kind regards,
Christophe Vanfleteren


Christophe Vanfleteren
  Reply With Quote
Old 01-27-2004, 07:40 PM   #3
Pat Ryan
 
Posts: n/a
Default Re: array copy. Simple question.
"Roberto Gallo" <> wrote in message
news:bv6dqm$3ff$...
>
>
> 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

> Thank you.
>
>





Pat Ryan
  Reply With Quote
Old 01-27-2004, 07:40 PM   #4
Chris Smith
 
Posts: n/a
Default Re: array copy. Simple question.
Roberto Gallo wrote:
> 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


Chris Smith
  Reply With Quote
Old 01-27-2004, 07:41 PM   #5
Matt Humphrey
 
Posts: n/a
Default Re: array copy. Simple question.

"Roberto Gallo" <> wrote in message
news:bv6dqm$3ff$...
>
>
> 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 http://www.iviz.com/




Matt Humphrey
  Reply With Quote
Old 01-27-2004, 11:16 PM   #6
Dale King
 
Posts: n/a
Default Re: array copy. Simple question.
"Chris Smith" <> wrote in message
news:...
> Roberto Gallo wrote:
> > 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();
--
Dale King




Dale King
  Reply With Quote
Old 01-27-2004, 11:36 PM   #7
Chris Smith
 
Posts: n/a
Default Re: array copy. Simple question.
"Dale King" <kingd[at]tmicha[dot]net> wrote:
> Except that you need to cast the result:
>
> buf2 = (byte[])buf1.clone();


Right. Good catch!

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation


Chris Smith
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Make a Copy of DVD Movies on Mac dave345 Media 13 07-29-2009 03:29 PM
DVD Copy for Mac OS Users reallyone Software 2 03-22-2008 01:38 AM
Re: Dial-up Modem Question w_tom A+ Certification 0 09-18-2005 09:12 PM
Simple question, join and divide Dr Zero DVD Video 0 12-29-2003 09:52 AM
Dvd X copy express 3.0.1 Alexandre Magny DVD Video 2 09-12-2003 03:02 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46