-1

I'm a beginner with JAVA so this question is not making much sense:

Write a method called append that accepts two integer arrays as parameters and returns a new array that contains the result of appending the second array's values at the end of the first array. For example, if arrays list1 and list2 store {2, 4, 6} and {1, 2, 3, 4, 5} respectively, the call of append(list1, list2) should return a new array containing {2, 4, 6, 1, 2, 3, 4, 5}. If the call instead had been append(list2, list1), the method would return an array containing {1, 2, 3, 4, 5, 2, 4, 6}

Help is greatly appreciated. Thanks.

CyberMaestro
  • 15
  • 1
  • 5
  • Well which part of that you don't understand? – user140547 Oct 15 '16 at 22:13
  • How to append two arrays being passed to a method. – CyberMaestro Oct 15 '16 at 22:16
  • At least you could search the same question on stackoverflow. Also check link provided by @thoeni. And if you are beginner in java you could try write some code by yourself and just then post the question. Why other guys should do your home task? – eg04lt3r Oct 15 '16 at 22:48

4 Answers4

0

This works, and is fairly self-explanatory:

int[] append(int[] ary1, int[] ary2) {
    int[] arrayOut = new int[ary1.length + ary2.length];
    for(int i = 0; i<ary1.length; i++) {
        arrayOut[i] = ary1[i];
    }
    for(int i = ary1.length; i< (ary1.length + ary2.length); i++) {
        arrayOut[i] = ary2[i - ary1.length];
    }
    return arrayOut;
}
PEF
  • 207
  • 1
  • 4
  • 11
0

Try this.

static int[] append(int[]... a) {
    return Stream.of(a)
        .flatMapToInt(IntStream::of)
        .toArray();
}

and

int[] a = {2, 4, 6};
int[] b = {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(append(a, b)));

result:

[2, 4, 6, 1, 2, 3, 4, 5]

You can also append three or more arrays.

0

package iran;

public class ExampleTest {

//https://telegram.me/javalike

public int[] append(int[] array1, int[] array2) {

    int array[] = new int[array1.length + array2.length];
    int counter = 0;
    int flage = 0;
    for (int i = 0; i < array.length; i++) {
        if (i < array1.length) {
            array[i] = array1[counter];
            counter++;
        } else {
            if (flage == 0) {
                counter = 0;
                flage = 1;
            }
            array[i] = array2[counter];
            counter++;
        }
    }

    return array;
}

public static void main(String[] args) {
    ExampleTest t = new ExampleTest();

    int a[] = { 2, 4, 6 };
    int b[] = { 1, 2, 3, 4, 5 };
    int c[] = t.append(b, a);
    for (int i = 0; i < c.length; i++) {

        System.out.print(c[i] + " ");
    }

}

}

result: 1 2 3 4 5 2 4 6

0

If you are looking for the best performance, you can use native methods like System.arraycopy() instead of a loop.

public static int[] append(int[] a, int[] b)
{
    int[] merged = new int[a.length + b.length];
    System.arraycopy(a, 0, merged, 0, a.length);
    System.arraycopy(b, 0, merged, a.length, b.length);
    return merged;
}

But not coming up with your own "manual" solution could defeat the purpose of this assignment.

Crusha K. Rool
  • 1,502
  • 15
  • 24