【发布时间】: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 但我无法完成最终的写作,有人可以帮助我吗?
【问题讨论】:
-
您甚至没有在代码中声明
testIt()。请分享你的试图并描述您遇到的具体问题(请注意,简单的赋值语句不是问题)。 StackOverflow 上的每个问题都应该展示您的努力。参见How do I ask a good question?和Why is "Can someone help me?" not an actual question? -
你的
if (n==name)没有做你想做的事。
标签: java