从列表中选择参数

时间:2017-09-15 06:57:41

标签: java arraylist collections

此程序用于打印学生详细信息和所有内容。学院班级有一个activity。它们是可用的活动,学生需要从中选择任何人。然后我想打印选择每项活动的学生人数。

例如:

游泳:2名学生。

比赛:1名学生。

绘画:1名学生。

歌唱:0等等..

帮我打印如上

学生班级

public class Student {
    int rollNo, yearOfStudy;
    String fName, lName, activity;
    Student(int rollNo, String fName, String lName, int yearOfStudy, String activity) {
        this.rollNo = rollNo;
        this.fName = fName;
        this.lName = lName;
        this.yearOfStudy = yearOfStudy;
        this.activity = activity;
    }

    @Override
    public String toString() {
        return new StringBuilder().append("RollNo.").append(rollNo).append(", ").append(fName).append(" ").append(lName)
                .toString();
    }
    void display() {
        System.out.println("Roll Number: " + rollNo + "\nName: " + fName + " " + lName + "\nYear Of Study: "
                + yearOfStudy + "\nActivity: " + activity);
        System.out.println("-------------------");
    }
}

主要类

public class Main {

public static void main(String[] args) {

Student s1 = new Student(1, "Alex", "Iwobi", 2013, "Swimming");
Student s2 = new Student(2, "Denis", "Suarez", 2013, "Painting");
Student s3 = new Student(3, "Gerard", "Deulofeu", 2013, "Swimming");
Student s4 = new Student(4, "Emre", "Can", 2013, "Playing");

Institute in = new Institute();

in.studentDetails(s1);
in.studentDetails(s2);
in.studentDetails(s3);
in.studentDetails(s4);

in.print();
in.messFood();

}

}

学院班级

public class Institute {

ArrayList studentList = new ArrayList();

void studentDetails(Object S) {

studentList.add(S);

}

void print() {
Iterator<Student> it = studentList.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
s.display();
}
}

void messFood() {
for (int j = 1; j < 5; j++) {

Student randomStudent = (Student) getRandomItem(studentList);

System.out.println("Mess food Incharge for week " + j + " : " + randomStudent.toString());

}
}

private static <T> T getRandomItem(List<T> studentList) {
return studentList.get(new Random().nextInt(studentList.size()));
}

void act() {
Set<String> activity = new LinkedHashSet<String>();
activity.add("Swimming");
activity.add("Reading");
activity.add("Playing");
activity.add("Singing");
activity.add("Painting");
}
}

3 个答案:

答案 0 :(得分:1)

Map<String, int> activity = new HashMap<>();

void act() {
activity.add("Swimming",0);
activity.add("Reading",0);
activity.add("Playing",0);
activity.add("Singing",0);
activity.add("Painting",0);
}

添加时

void studentDetails(Object S) {
Student s = (Student) S;
//update activity
map.put(s.activity, map.get(s.activity) + 1);
studentList.add(S);
}

您可以稍后打印

答案 1 :(得分:1)

这里你去::只需从主类调用此方法。

输出是:

{打= 1,游泳= 2,绘画= 1}

public void numberOfStudentsWhoSelectedEachActivity(){
        HashMap<String, Integer> map = new HashMap<>();
        for(Object s : studentList){
            Student student = (Student) s;
            String act = student.activity;
            if(map.get(act) == null){
                map.put(act, 1);
            }
            else{
                map.put(act, map.get(act)+1);
            }
        }
        System.out.println(map);
    }

答案 2 :(得分:0)

你可以利用收藏品, 您需要将studentList传递给它。

Set<String> studentSet = new HashSet<String>(students);
for (String key : studentSet) {
    System.out.println(key + ": " + Collections.frequency(students, key));
}