![]() |
|
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
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 |
|
|
|
#3 |
|
Posts: n/a
|
"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 |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
"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 |
|
|
|
#6 |
|
Posts: n/a
|
"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 |
|
|
|
#7 |
|
Posts: n/a
|
"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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |