-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavalab23.java
More file actions
47 lines (45 loc) · 1.53 KB
/
Copy pathjavalab23.java
File metadata and controls
47 lines (45 loc) · 1.53 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class javalab23 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList <Integer> List1 = new ArrayList<>();
System.out.print("Enter number of elements in the Int type List1: ");
int n = sc.nextInt();
int st;
for(int i = 0;i < n; i++){
System.out.println(" Enter element " + (i+1) + ":");
st = sc.nextInt();
List1.add(st);
}
ArrayList <Integer> List2 = new ArrayList<>();
System.out.print("Enter number of elements in the Int type List2: ");
int n1 = sc.nextInt();
for(int i = 0;i < n1; i++){
System.out.println(" Enter element " + (i+1) + ":");
st = sc.nextInt();
List2.add(st);
}
ArrayList <Integer> res = alternate(List1,List2);
System.out.println("List 1: " + List1);
System.out.println("List 2: " + List2);
System.out.println("Resulted array: " + res);
}
static ArrayList<Integer> alternate(ArrayList<Integer> list1, ArrayList<Integer> list2){
ArrayList<Integer> list3 = new ArrayList<>();
int i = 0;
int j = 0;
while(i<list1.size() || j<list2.size()){
if(i<list1.size())
{
list3.add(list1.get(i++));
}
if(j<list2.size())
{
list3.add(list2.get(j++));
}
}
return list3;
}
}