You are on page 1of 2

public class JoinArrays {

public static void main(String[] args) {

int[] A = {3, 1, 9};


int[] B = {12, 13};
int[] C = {5, 17, 8, 2};

// Display arrays A, B, and C


System.out.print("Array A: ");
printArray(A);

System.out.print("Array B: ");
printArray(B);

System.out.print("Array C: ");
printArray(C);

// Call the method to join arrays


int[] result = joinArrays(A, B, C);

// Display the resulting array


System.out.print("Resulting array: ");
printArray(result);
}

// Method to join three arrays of integers


public static int[] joinArrays(int[] array1, int[] array2, int[] array3) {
// Calculate the length of the resulting array
int resultLength = array1.length+array2.length+array3.length;

// Create a new array to store the result


int[] result = new int[resultLength];

// Copy elements from array1 to result


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

// Copy elements from array2 to result


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

// Copy elements from array3 to result


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

// Return the resulting array


return result;
}

// Method to print an array


public static void printArray(int[] array) {
for (int value : array) {
System.out.print(value + " ");
}
System.out.println();
}
}

You might also like