好的,我是Java新手,我只是在通过一个类来工作,而且我在类的程序中遇到了一些障碍。除了最后一件事情之外,我已经设法通过我最终节目中的每一部分工作。ArrayList.remove不能与Integer一起使用,与常量一起工作
public static void remove(String studentID)
{
Integer foundLocation = 0;
for (int i = 0; i < studentList.size(); i++)
{
if (studentList.get(i).getStudentID().compareTo(studentID) == 0)
{
//This means we have found our entry and can delete it
foundLocation = i;
}
}
System.out.println(foundLocation);
if (foundLocation != 0)
{
System.out.println(studentList);
studentList.remove(foundLocation);
System.out.println(foundLocation.getClass().getName());
System.out.println("Student ID removed: " + studentID);
System.out.println(studentList);
}
else
{
System.out.println("Sorry, " + studentID + " not found.");
}
该代码似乎应该工作。但是,我得到的是remove
实际上没有做任何事情。我的额外印刷品在那里进行验证。 ArrayList
只是简单的不会改变。
但是,如果我只是取代:
studentList.remove(foundLocation);
的东西,如:
studentList.remove(3);
它只是删除完美。
foundLocation
是Integer
。
有人可以向我解释我在这里做了什么吗?
我认为对于熟悉Java的人来说这是非常明显的,但我很想念它。
是的,这是一个类。我对这个方法的命名没有任何意见。如果我有选择,我会用Python编写它。明天一旦我更清醒一点(并清醒过来),我会看看这个,看看你的Integer.valueOf是否会解决这个问题。假设我可以做Integer.valueOf(foundLocation)。 –
不! Integer.valueOf是问题的演示,而不是解决方案。您需要将'Integer foundLocation = 0;'更改为'int foundLocation = 0;' – Thilo
...我认为那样做了。我不知道为什么人们说Java是一种不友好的语言(他讽刺地说)。非常感谢你的帮助,每个人。 –