programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Addition of two byte[]

Ranch Hand
Posts: 46
• Number of slices to send:
Optional 'thank-you' note:
Hi Everyone,

Is it possible that i can add two arrays.

For example, I had two array of type byte[] bArray1;
and byte[] bArray2;
Then what is it ok if i do
bArray2 = bArray1 + bArray2;
When I am doing this. I am getting an error saying that"The operator + is undefined for byte[],byte[]

Can anyone please explain me how to add two arrays
Correct me if i am wrong

Bartender
Posts: 1205
22
• Number of slices to send:
Optional 'thank-you' note:
That all depends on what result you want. Are the two byte arrays binary representations of long integers? Are they arrays of 1s and 0s (Ascii 48 and 49) represeting binary numbers? Are they vectors (in the mathematical, not the Java sense), so that the corresponding elements in the two arrays should be added together without "carrying the 1" to the next elements?

Since I can't tell how what you mean by adding two arrays, how can the compiler/JRE?

Ryan

author and iconoclast
Posts: 24207
46
• Number of slices to send:
Optional 'thank-you' note:
Allocate a new array whose length is the sum of the other two; then use the System.arraycopy() method to copy each small array into the appropriate location in the big array.

lowercase baba
Posts: 13089
67
• Number of slices to send:
Optional 'thank-you' note:
do you want to add the value of bArray1[0] to bArray2[0] and put it into bArray2[0]?

or, if bArray1 has 8 elements, and bArray2 has 4 elements, you want a brand new array with 12 elements?

munna next
Ranch Hand
Posts: 46
• Number of slices to send:
Optional 'thank-you' note:
Thanks Everyone for your replies.
yes, fred i have two byte arrays, And i wanted to add the contents of bArray1 to bArray2

Can any one pl explain how to do this?

Ranch Hand
Posts: 580
• Number of slices to send:
Optional 'thank-you' note:
All you need to do is figure out how big your new array will have to be (array1.length + array2.length) and instantiate it. Then, you can either hand code the copy or use System.arraycopy.

author
Posts: 14112
• Number of slices to send:
Optional 'thank-you' note:
Here is a small utility method I wrote:

Notice that the second parameter is allowed to be null, and the type of the concatenated array is determined by the type of the first parameter.

Ilja Preuss
author
Posts: 14112
• Number of slices to send:
Optional 'thank-you' note:
Ah, sorry, my code doesn't work for arrays of primitives. Shouldn't be too hard to adapt, though.

 Don't get me started about those stupid light bulbs.