0

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?

sup
  • 101
  • 2
  • 11

6 Answers6

2

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,

npinti
  • 51,780
  • 5
  • 72
  • 96
  • how to add to a particular index – sup Aug 14 '15 at 06:59
  • i have tried this for adding to starting and end of the array . but how to add in the middle – sup Aug 14 '15 at 07:01
  • @sup: The method linked in my answer specifies 2 index values. These will determine from where you start copying from the source array and to where you'll begin writing to the destination array. – npinti Aug 14 '15 at 07:01
2

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]

Sergej Werfel
  • 1,335
  • 2
  • 14
  • 25
1

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 array1s 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
dly
  • 1,080
  • 1
  • 17
  • 23
  • if the second array is {0x03,0x02,0x04}, how will i add that,i do not want them to be in the sorted order.i want the answer to be{0x01,0x03,0x02,0x04,0x05} – sup Aug 14 '15 at 07:14
  • Remove the `Arrays.sort()` and you're fine. The values will be added to the target array in the order they come from the source arrays. – dly Aug 14 '15 at 07:18
  • i tried that but the output is displayed as 1 5 2 3 4 – sup Aug 14 '15 at 07:20
  • im not expecting the sorted order . whatever may be values in array2 should be between array1[0] and array1[1]. – sup Aug 14 '15 at 07:21
  • Did you change `array2`s values or their order? In this code they're already sorted, so the output is sorted as well. – dly Aug 14 '15 at 07:21
  • i have edit my question and changed array2,please help me in that case – sup Aug 14 '15 at 07:24
  • i am expecting the result to be as 1 3 4 2 5. – sup Aug 14 '15 at 07:28
  • i tried changing this line System.arraycopy(array2, 0, targetArray, array1.length-1, array2.length);. then im getting the output as 1 3 4 2 0 – sup Aug 14 '15 at 07:29
  • The result with this method will be all values of `array1` in the order they come followed by the values of `array2` in the order they come. I don't know how a result of 1 3 4 2 5 can be expected. Do you want to put `array2` between `array1`s values? – dly Aug 14 '15 at 07:32
  • ya exactly.i am expecting to put array2 values in between array1's value – sup Aug 14 '15 at 07:35
  • edited with alternative solution to match your request – dly Aug 14 '15 at 07:42
1

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];
        }           
    }
Sudhir
  • 11
  • 1
0

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);
Nitesh Virani
  • 1,688
  • 4
  • 25
  • 41
0

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; }
Harish Naidu
  • 35
  • 1
  • 12