-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise07_31.java
More file actions
61 lines (50 loc) · 2.01 KB
/
Copy pathExercise07_31.java
File metadata and controls
61 lines (50 loc) · 2.01 KB
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
import java.util.Arrays;
import java.util.Scanner;
/*****************************************************************************
* This program takes two lists, merges them into one list, sorts them, then
* outputs them. The method merge both merges and sorts.
*****************************************************************************/
/**
*
* @author jorda
*/
public class Exercise07_31 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//input list1
System.out.print("Enter list1 size and contents: ");
int[] list1 = new int[input.nextInt()];
//System.out.println("Enter the contents of the first array");
for (int i = 0; i < list1.length; i++) {
list1[i] = input.nextInt();
}
//input list2
System.out.print("Enter list2 size and contents: ");
int[] list2 = new int[input.nextInt()];
//System.out.println("Enter the contents of the Second array");
for (int i = 0; i < list2.length; i++) {
list2[i] = input.nextInt();
}
//print the lists before loigc
System.out.println("list1 is ");
for(int i : list1)
System.out.print(i + " ");
System.out.println("list2 is ");
for(int i : list2)
System.out.print(i + " ");
//merge the lists and sort
int[] merged = merge(list1, list2);
//output the sorted, merged lists
System.out.println("The merged lists are ");
for(int i : merged)
System.out.print(i + " ");
}
//takes two lists of any size, combines them, then sorts them
public static int[] merge(int[] list1, int[] list2){
int[] merged = new int[list1.length + list2.length];
System.arraycopy(list1, 0, merged, 0, list1.length);
System.arraycopy(list2, 0, merged, list1.length, list2.length);
Arrays.sort(merged);
return merged;
}
}