write a program to input some numbers into two array m and p. The sizes of the arrays may or may not be the same. find and display the elements common to both the arrays.
(Java program)
Answers
Answer:
Sru I knew only python and c++ ya
import java.util.*;
public class ArrayListSorting {
public static void main(String args[]){
ArrayList<Integer> arraylist = new ArrayList<Integer>();
ArrayList<Integer> arraylist2 = new ArrayList<Integer>();
System.out.println("Please provide the integers of first Array provie end once done");
Scanner scanner = new Scanner( system.in - This website is for sale! - System Resources and Information. );
System.out.println("Enter the values ");
String input = scanner.nextLine();
while(input != null && !("end".equalsIgnoreCase(input))){
try {
int val = Integer.parseInt(input);
arraylist.add(new Integer(val));
System.out.println("Enter the values ");
input = scanner.nextLine();
} catch(Exception ex){
ex.printStackTrace();
System.out.println("Please provide the right entry only integer values");
System.out.println("Enter the values ");
}
}
input = null;
System.out.println("Please provide the integers of secong Array provie end once done");
System.out.println("Enter the values ");
input = scanner.nextLine();
while(input != null && !("end".equalsIgnoreCase(input))){
try {
int val = Integer.parseInt(input);
arraylist2.add(new Integer(val));
System.out.println("Enter the values ");
input = scanner.nextLine();
} catch(Exception ex){
ex.printStackTrace();
System.out.println("Please provide the right entry only integer values");
System.out.println("Enter the values ");
}
}
Collections.sort(arraylist);
Collections.sort(arraylist2);
/* for(int x=0;x<arraylist.size();x++)
System.out.println("value at "+x+" is "+arraylist.get(x).intValue());
for(int x=0;x<arraylist2.size();x++)
System.out.println("value at "+x+" is "+arraylist2.get(x).intValue());
*/
int i =0;
int j =0;
Integer fr;
Integer sn;
System.out.println("common value found are");
while(true){
if( i < arraylist.size())
fr = (Integer)arraylist.get(i);
else
break;
if( j < arraylist2.size())
sn = (Integer)arraylist2.get(j);
else
break;
//System.out.println("fr ("+i+") "+fr.intValue()+" sn ("+j+") "+sn.intValue());
if(fr.equals(sn)){
System.out.println(fr.intValue());
i++;
j++;
}
if(fr.intValue() < sn.intValue()) i++;
if(fr.intValue() > sn.intValue()) j++;
}
}
}
Output
C:\Users\koteswara\Downloads>javac ArrayListSorting.java
C:\Users\koteswara\Downloads>java ArrayListSorting
Please provide the integers of first Array provie end once done
Enter the values
5
Enter the values
4
Enter the values
3
Enter the values
2
Enter the values
1
Enter the values
end
Please provide the integers of secong Array provie end once done
Enter the values
7
Enter the values
3
Enter the values
5
Enter the values
6
Enter the values
2
Enter the values
1
Enter the values
end
common value found are
1
2
3
5