Rivers31334
Rivers31334

Reputation: 654

Concatenating two int arrays

I am looking to take two int[] arrays, and concatenate them into a single one. I realize that there are quite a few posts already on here, yet all of them are using more advanced scripts (System.arraycopy, etc.) that I am not familiar with. I could easily use them, but I lose understanding for why I am using these methods.

My plan looking forward is to set both my arrays to Strings (using the toString() method), and then trace them with a for loop. Each time an integer is found, it prints it onto the end of a new String that is originally initialized to "".

Example (line breaks for spacing):

array1 = [1, 2, 3, 4];

array2 = [5, 6, 7];

array3 = [1, 2, 3, 4, 5, 6, 7]; //this is what array should look like.

Can anyone offer me any advice on my proposed method to fulfilling this part of my work?

Upvotes: 2

Views: 1625

Answers (5)

Rahul
Rahul

Reputation: 77934

if you don't want to use built in methods arraycopy then do it manually. Create a 3rd array with size arr1+arr2 and then loop through both array and store their contents to arr3. Like

int size = arr1.length + arr2.length;
int indexval=0;
int[] arr3 = new int[size];

for(int i=0;i<arr1.length;i++)
{
 arr3[i] = arr1[i];
 indexval++;
}

for(int i=0,i<arr2.length;i++)
{
  arr3[indexval] = arr2[i];
  indexval++;
}

Upvotes: 1

r.v
r.v

Reputation: 4877

You do not need to use strings here. Just get a new array like:

int[] array3 = new int[array1.length + array2.length];

Now you can use arraycopy to copy the two arrays or, if you do not like that, just use loops. I guess you already know from elsewhere how to use arraycopy. As for loops, you can two loops, one that runs from 0 to array1.length and copies array1 into array3 and another that runs from 0 to array2.length and copies array2 into array3 beginning beginning from the index array1.length in array3.

If you want to subsequently print the arrays, use Arrays.toString()

Upvotes: 3

Chamil
Chamil

Reputation: 803

using Apache Commons Lang library

int[] array1 = { 1, 2, 3, 4, 5 };
int[] array2 = { 6, 7, 8, 9, 10 };

int[] array3 = ArrayUtils.addAll(array1, array2);

http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/ArrayUtils.html#addAll(int[], int[])

Upvotes: 0

Jakub H
Jakub H

Reputation: 2158

You can use ArrayUtils from Apache Commons

int [] array1 = {1,2,3, 4};
int [] array2 = {5,6,7};
int [] array3 = ArrayUtils.addAll(array1, array2);

Upvotes: 0

Mureinik
Mureinik

Reputation: 312289

There's no need to use a String. You can just allocate a new array the size of the two others combined, and copy all the values into it:

int[] array1 = ...;
int[] array2 = ...;
int[] result = new int[array1.length + array2.length];
for (int i = 0; i < array1.length; ++i) {
    result[i]  = array1[i];
}
for (int i = 0; i < array2.length; ++i) {
    result[array1.length + i] = array2[i];
}

Of course, you could use System.arrayCopy to do the copying more efficiently, but the principal still stands.

Upvotes: 3

Related Questions