Reputation: 101
I have two arrays:
byte[] array1=new byte[]{0x01,0x05};
byte[] array2=new byte[]{0x03,0x04,0x02};
I want to add array2
in between array1[0]
and array1[1]
.I do not want to delete any values from array1
after adding array2
.Whatever may be the values in array2,it should be added to array1 as it is.I am not expecting the sorted order.
How can I do it?
Upvotes: 0
Views: 145
Reputation: 1365
The following code returns a new array that containing the data from insertIn to index position, then all data from toInsert and then the rest of insertIn (This code is not fully tested, please implement a unit test for that method ;-) )
import static java.lang.System.arraycopy;
import java.util.Arrays;
public class MyCopy {
public static void main(String[] args) {
byte[] array1 = new byte[] {0x01, 0x05 };
byte[] array2 = new byte[] {0x03, 0x04, 0x02 };
// add array 2 in array 1 at index 1
byte[] inserted = insertIn(array1, array2, 1);
System.out.println(Arrays.toString(inserted));
}
public static byte[] insertIn(byte[] insertIn, byte[] toInsert, int position) {
assert position > 0 && position <= insertIn.length;
byte[] result = new byte[insertIn.length + toInsert.length];
// copy start of insertIn
arraycopy(insertIn, 0, result, 0, position);
// copy toInsert
arraycopy(toInsert, 0, result, position, toInsert.length);
// copy rest of insertIn
int restIndexInResult = position + toInsert.length;
int restLength = toInsert.length - position - 1;
arraycopy(insertIn, position, result, restIndexInResult , restLength);
return result;
}
}
Result: [1, 3, 4, 2, 5]
Upvotes: 2
Reputation: 35
here is the method using systemarraycopy which accepts the two arrays and positon where the second array you want insert in the first array
public static byte[] append(byte[] a, byte[] b,int position) {
byte[] c= new byte[a.length+b.length];
System.arraycopy(a, 0, c, 0, position);
System.arraycopy(b, 0, c, position, b.length);
System.arraycopy(a, position , c, position + b.length , a.length - position);
return c; }
Upvotes: 0
Reputation: 11
A simple solution can be the following:
byte[] array1=new byte[]{0x01,0x05};
byte[] array2=new byte[]{0x02,0x03,0x04};
byte[] newArray = new byte[array1.length+array2.length];
newArray[0]=array1[0];
for(int i=1; i<newArray.length; i++){
if(i<=array2.length){
newArray[i]=array2[i-1];
}else{
newArray[i]=array1[i-3];
}
}
Upvotes: 1
Reputation: 1088
You need a third array to accomplish this.
byte[] array1=new byte[]{0x01,0x05};
byte[] array2=new byte[]{0x03,0x04,0x02};
byte[] targetArray = new byte[array1.length + array2.length];
System.arraycopy(array1, 0, targetArray, 0, array1.length);
System.arraycopy(array2, 0, targetArray, array1.length, array2.length);
for (byte b : targetArray) {
System.out.println(b);
}
result:
1
5
3
4
2
Alternative to put array2
in between array1
s values
byte[] array1=new byte[]{0x01,0x05};
byte[] array2=new byte[]{0x03,0x04,0x02};
byte[] targetArray = new byte[array1.length + array2.length];
int cap = array1.length / 2;
System.arraycopy(array1, 0, targetArray, 0, cap);
System.arraycopy(array2, 0, targetArray, cap, array2.length);
System.arraycopy(array1, cap, targetArray, array2.length + cap, array1.length - cap);
for (byte b : targetArray) {
System.out.println(b);
}
Result:
1
3
4
2
5
Upvotes: 1
Reputation: 1712
You can use below code to combile both array, you have to create third array which will hold both arrays data:
byte[] array1=new byte[]{0x01,0x05};
byte[] array2=new byte[]{0x02,0x03,0x04};
int array1Len = array1.length;
int array2Len = array2.length;
byte[] array3 = new byte[array1Len+array2Len];
System.arraycopy(array1, 0, array3, 0, array1Len);
System.arraycopy(array2, 0, array3, array1Len, array2Len);
Upvotes: 0
Reputation: 52185
You will need to create a new array which is of size array1.length + array2.length
and then using something like System.arrayCopy
to throw in your current arrays in the order you need them to be,
Upvotes: 2