0% found this document useful (0 votes)
14 views2 pages

Q 1

The document defines a JoinArrays class containing a main method that declares and initializes three integer arrays. It calls the joinArrays method, passing the three arrays, which returns a new array containing the concatenated contents of the input arrays. The main method then prints the resulting joined array. The joinArrays method calculates the length of the result, creates a new array, and copies each element of the input arrays into the result in order.

Uploaded by

aliarain1404
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Q 1

The document defines a JoinArrays class containing a main method that declares and initializes three integer arrays. It calls the joinArrays method, passing the three arrays, which returns a new array containing the concatenated contents of the input arrays. The main method then prints the resulting joined array. The joinArrays method calculates the length of the result, creates a new array, and copies each element of the input arrays into the result in order.

Uploaded by

aliarain1404
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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