【问题标题】:For cicle adding strings to ArrayList [duplicate]循环将字符串添加到 ArrayList [重复]
【发布时间】:2022-11-30 18:56:46
【问题描述】:

这是我的代码:

public class Lecture
{
    private ArrayList<String> student;
    
    public Lecture() 
    {
        student = new ArrayList<>();
    }
    
    public boolean addStudent(String name) 
    {
        student.add(name);
        return true;
    }
    
    public int getHomonyNumber(String n) 
    {
        int count=0;
        for(String name : student) {
            if (n==name) {
                count++;
            }
        }
        return count;
    }
    
    public void printCSList()
    {
        String sep = "";
        for(String name : student) {
            System.out.print(sep + name);
            sep = ", ";
        }
    }
    
    public boolean swap(int index1, int index2)
    {
        Collections.swap(student, index1, index2);
        return true;
    }

我需要解决这个最后的任务:一个方法 void testIt() 添加至少 4 个学生(至少两个同名),在一行上打印列表,交换 2 个学生,再次打印列表,最后打印具有相同姓名的学生人数(对于您在列表中插入两次的姓名)。 有人可以帮助我吗?

该任务要求至少添加 4 名学生,其中 2 名同名。我尝试添加三个新名称,因为如果您添加至少 4 个,您肯定会有 2 个同音异义词。我已经尝试过使用 for cicle 但我无法完成最终的写作,有人可以帮助我吗?

【问题讨论】:

标签: java


【解决方案1】:

在 Java 中,您不能将两个字符串与 == 进行比较。您应该改用 equals() 方法。

public class Foo {

    public static void main(String... args) {
        Lecture lecture = new Lecture();
        lecture.addStudent("Anna");
        lecture.addStudent("Oleg");
        lecture.addStudent("Peter");
        lecture.addStudent("Anna");
        lecture.print(); // Anna, Oleg, Peter, Anna

        lecture.swap(0, 1);
        lecture.print();    // Oleg, Anna, Peter, Anna
        System.out.println(lecture.getTotalStudentsWithSameName()); // 2
    }

}

class Lecture {

    private final List<String> students = new ArrayList<>();

    public void addStudent(String name) {
        Objects.requireNonNull(name);
        students.add(name);
    }

    public int getTotalStudentsWithSameName() {
        Map<String, Integer> map = new HashMap<>();

        for (String student : students)
            map.put(student, map.getOrDefault(student, 0) + 1);

        int count = 0;

        for (int total : map.values()) {
            if (total > 1) {
                count += total;
            }
        }

        return count;
    }

    public int getHomonyNumber(String name) {
        int count = 0;

        for (String student : students) {
            if (student.equalsIgnoreCase(name)) {
                count++;
            }
        }

        return count;
    }

    public void print() {
        boolean comma = false;

        for (String student : students) {
            if (comma) {
                System.out.print(", ");
            }

            comma = true;
            System.out.print(student);
        }

        System.out.println();
    }

    public void swap(int one, int two) {
        Collections.swap(students, one, two);
    }

}

【讨论】:

    猜你喜欢
    • 2016-08-02
    • 2013-08-13
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 2015-06-20
    相关资源
    最近更新 更多