-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathSplittingArray.java
62 lines (45 loc) · 1.08 KB
/
SplittingArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public import java.util.*;
public class SplittingArray {
// this method accepts a array and prints the value
static void pprint(int arr[])
{
for (int var : arr) {
System.out.print(var + " ");
}
System.out.println();
}
public static void main(String args[])
{
// original array
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
// size of array
int n = a.length;
// accepting the value of position from the user
Scanner scanner = new Scanner(System.in);
System.out.println("Enter position to split.");
int pos = scanner.nextInt();
// validating the position for invalid values.
if (pos > 0 && pos < n) {
// method 1 : using two for loops
// declaring array B and C
int b[] = new int[pos];
int c[] = new int[n - pos];
// initializing array B
for (int i = 0; i < pos; i++) {
b[i] = a[i];
}
// initializing array C
for (int i = 0; i < n - pos; i++) {
c[i] = a[i + pos];
}
// printing the array b and c
pprint(b);
pprint(c);
}
else {
System.out.println("Invalid position.");
}
}
}
class SplittingArray {
}