جافا : collections .. طباعة تكرار العناصر المدخلة من قبل المستخدم
برنامج لادخال قيم صحيحة من المستخدم
بين 1 و 100 وفي حال ادخل 0 يتوقف الادخال
ويطبع تكرار كل عنصر من العناصر المدخلة
باستخدام Collections
import java.util.*;
public class Q3 {
public static void main (String [] args ){
Scanner E = new Scanner (System.in);
List<String> in= new ArrayList<String>();
try{ while (true){
int m =E.nextInt();
if (m>0&&m<=100)in.add(""+m);
else if (m==0)break;
else {
System.err.println("Enter another value");
continue;
}
}
}catch(Exception e){
System.err.println("Failed");
}
List <String> occ=new ArrayList<String>();
String []a =in.toArray(new String [in.size()]);
for (int i=0;i<in.size();i++){
occ.add(""+Collections.frequency(in, a[i]));
}
for (int i=0;i<a.length;i++){
for (int j=i+1;j<a.length;j++){
if (a[j].equals(a[i])){
a[j]="0";
occ.remove(j);
in.remove(j);
}
}}
for (int i=0;i<occ.size();i++){
System.out.println("Occurances of "+in.get(i)+" Equals "+occ.get(i));
}
}
}
0 التعليقات: