JAVA PROGRAM
Program for appending two arrays |Java Program|
Introduction
Arrays are fundamental data structures in programming that allow us to store multiple elements of the same type in a contiguous block of memory. Often, you may find yourself needing to combine or append two arrays into a single array. In this blog post, we’ll explore a Java program that appends two arrays into one, demonstrating a simple yet effective way to achieve this.
What Does It Mean to Append Arrays?
Appending two arrays means taking the elements of one array and adding them to the end of another array. For instance, if you have two arrays:
Array A: 1,2,3,4
Array B: 5,6,7
Appending them would result in:
Array C: 1,2,3,4,5,6,7
Java Program to Append Two Arrays
Let’s look at the Java code that accomplishes this task.
import java.io.*;
import java.util.*;
public class append {
public static void main(String args[]) throws IOException {
int i, j, m, n, total;
int a[] = new int[10];
int b[] = new int[10];
int c[] = new int[30];
DataInputStream s = new DataInputStream(System.in);
// Input size and elements for Array A
System.out.println("Enter the size of array A:");
n = Integer.parseInt(s.readLine());
System.out.println("Enter the elements of array A:");
for (i = 0; i < n; i++) {
a[i] = Integer.parseInt(s.readLine());
}
// Input size and elements for Array B
System.out.println("Enter the size of array B:");
m = Integer.parseInt(s.readLine());
System.out.println("Enter the elements of array B:");
for (i = 0; i < m; i++) {
b[i] = Integer.parseInt(s.readLine());
}
// Append arrays
total = m + n;
for (i = 0; i < n; i++) {
c[i] = a[i];
}
for (j = 0; j < m; j++, n++) {
c[n] = b[j];
}
// Output the appended array
System.out.println("Append array C:");
for (i = 0; i < total; i++) {
System.out.println(c[i]);
}
}
}
Output
Enter the size of array A:
4
Enter the elements of array A:
1
2
3
4
Enter the size of array B:
3
Enter the elements of array B:
5
6
7
Append array C:
1
2
3
4
5
6
7
Explanation
1. Declare and Initialize Arrays:
We declare three arrays: a and b for the input arrays, and c for the result array that will contain the appended elements. The sizes of a and b are initialized based on user input, while c is large enough to hold all elements from both a and b.
2. Input Elements for Array A:
The program prompts the user to enter the size of Array A, followed by the individual elements.
3. Input Elements for Array B:
Similarly, the user is prompted to enter the size and elements for Array B.
4. Appending Arrays:
First, the elements of Array A are copied into Array C.
Then, the elements of Array B are appended after the elements of Array A in Array C.
5. Output the Appended Array:
The program prints out the contents of the appended array, showing the combined elements of Arrays A and B.
Conclusion
Appending arrays is a fundamental operation in many programming tasks. This Java program provides a clear and simple method to combine two arrays into one. Understanding how to manipulate arrays is crucial in building more complex data structures and algorithms. This basic operation forms the foundation for more advanced tasks in Java programming.
Feel free to modify the program to handle different sizes of arrays or to append more than two arrays. Happy coding!
Post a Comment
0 Comments