This is a Java Program to Split an Array from Specified Position.
Enter size of array and then enter all the elements of that array. Now enter the position from where you want to split. We first copy the elements from first position to that given position in secnd array and remaining elements in third array.
Here is the source code of the Java Program to Split an Array from Specified Position. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Split
{
public static void main(String[] args)
{
int n, x, flag = 1, loc = 0, k = 0,j = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
int b[] = new int[n];
int c[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
System.out.print("Enter the position from where you want to split:");
loc = s.nextInt();
for(int i = 0; i < loc; i++)
{
b[k] = a[i];
k++;
}
for(int i = loc; i < n; i++)
{
c[j] = a[i];
j++;
}
System.out.print("First array:");
for(int i = 0;i < k; i++)
{
System.out.print(b[i]+" ");
}
System.out.println("");
System.out.print("Second array:");
for(int i = 0; i < j; i++)
{
System.out.print(c[i]+" ");
}
}
}
Output:
$ javac Split.java $ java Split Enter no. of elements you want in array:8 Enter all the elements: 2 3 4 7 1 9 11 6 Enter the position from where you want to split:4 First array:2 3 4 7 Second array:1 9 11 6
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Next Steps:
- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Practice Information Technology MCQs
- Apply for Information Technology Internship
- Practice BCA MCQs
- Apply for Java Internship
- Practice Programming MCQs