• Post Reply Bookmark Topic Watch Topic
  • New Topic

Addition of two byte[]  RSS feed

 
munna next
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Thanks in Advance.
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks in advance.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, sorry, my code doesn't work for arrays of primitives. Shouldn't be too hard to adapt, though.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!