Question
can anyone help me fix my code and tell me where is my fault ? java code this is my code import java.util.ArrayList; import java.util.HashMap;
can anyone help me fix my code and tell me where is my fault ?
java code
this is my code
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String a[])
{
Scanner sc = new Scanner(System.in);
ArrayList
ArrayList
System.out.println("Enter the size of list1 ");
int n1= sc.Int1();
System.out.println("Enter the size of list 2: ");
int n2 = sc.Int2();
System.out.println("Enter first array elements: ");
for(int i = 0; i < n1; i++){
int num = sc.Int1();
list1.add(num);
}
System.out.println("Enter second array elements: ");
for(int i = 0; i < n2; i++){
int num1 = sc.Int2();
list2.add(num);
}
HashMap
for(int i = 0; i < n1; i++){
mp.put(list1.get(i), 1);
}
for(int i = 0; i < n2; i++){
mp.put(list2.get(i), 2);
}
System.out.println(" common elements: ");
for(int i = 0; i < n; i++){
if(mp.containsKey(list2.get(i)))
System.out.print(list2.get(i) + " ");
}
System.out.println();
}
}
i want to do is like this
"Enter the size of list1 ");
4
"Enter the size of list2
6
"Enter first array elements:
1 2 3 4
"Enter 2nd array elements:
2 3 4 5 6 7
common elements:
2 3 4
thanks
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started