[英]Creating a remove() method to remove an item from an arraylist (Java)
我有2个班的Student班和StudentTest班。 在学生类中,我需要编写一个称为remove(int ID)的方法,以从数组列表中删除具有特定ID的学生。 该方法由StudentTest类中的main()方法调用。 代码如下:
public class Student {
private final int size = 12; //max. size
private int [] ID = new int [size]; //the student's id number
private String [] name = new String [size]; //the student's name
private double [] tuition = new double [size];
int position= 0; //position to add data
//an add() method goes here, but is not the case of my question so I'm emitting it
//Here is the remove() to remove a student given their ID number
public void remove(int ID){
for(int i=0; i<size ; i++)
if (ID[i].equals(ID)
{
remove(i);
return true;
}
return false;
}//remove() :this method is so wrong I know, but I've been trying so many different things and its just driving me nutts!
//a method goes here to display student info.
} //end Student class
//below is my StudentTest class which will be calling the remove() method
public class StudentTest extends Student {
//main
public static void main(String args[]){
Student stuList = new Student();
stuList.add(1234, "Jane Jane", 23000);
stuList.add(4321, "Billy Bill", 15500);
//2 students are added to the list: in this order; (ID, "Name", Tuition)
//now this main program calls remove(), to remove a student by ID
stuList.remove(1234);
//rest of code entails displaying the new list and so on
}//main()
}// StudentTest class
现在。 我的删除方法非常需要帮助。 我研究了ArrayList类及其方法。 但是简单地编写stuList.remove()根本不起作用。 我还尝试了迭代器方法(我迷上了那个方法)。 请指引我正确的方向..谢谢!
我会放弃解决眼前的问题,然后返回到设计并正确完成OOP,从
1)学生,应该是集合还是代表一个学生。
这是入门编程课程中的作业吗?
我不明白为什么您必须将StudentID,Name和Tuition作为数组,学生类应该定义一个“学生”而不是多个学生。
第1类-学生
Student
{
int ID;
string Name;
double Tution;
}
第2类-StudentManager
StudentManager
{
Student ListOfStudents;
AddStudent();
RemoveStudent();
}
编辑:
学生类代表一个学生,并且该学生的特征(例如姓名和学费)与用于添加对象并将其从列表中删除的对象的学生管理器进行交互,而不是具有包含一个学生信息的3个数组并尝试更新所有这些,这是一个糟糕的设计,并且很容易学会避免这种情况。
当我甚至在开始编码之前就学习OOP时,我曾经识别出可能转换为类的对象,发现了它们可能具有的属性以及它们如何与其他对象进行交互。
您会看到没有人会在这里发布解决问题的方法,因为这是家庭作业,但是我们可以尝试帮助您了解如何解决问题。
您有3个数组和一个int position
来存储添加数据的位置。 如果您删除一名学生,则必须:
1)找到它在数组中的位置,说r
是要删除的位置(可以在0和position-1
)
2)减少位置( position = position - 1
),因为现在您的列表会更短。
3)对于所有3个数组,将位置r
元素替换为位于位置r+1
的元素,现在您丢失了位置r
的元素,并且拥有位于r+1
的元素的两倍。
4)将r+1
替换为r+2
,依此类推,直到将position-1
替换为position
( position
的新值)
如果您在执行此操作时遇到问题,请向我们显示一些代码,然后再次寻求帮助...
编辑:回应您的评论:
您有7个元素,编号为0到6,位置为7,因为要插入下一个值,您想删除编号为4的元素(r = 4)。 这是一个更简单的解决方案,但它将更改列表的顺序:
position = position - 1; // now position is 6
array[r] = array[position]; // now element at position 4 was replaced with the one at the end of the array, which is still there by the way. Do this for all the 3 arrays...
而已...
您的代码中的问题在于,您的remove()方法会无限递归地调用自身,以死于StackOverflow
public void remove(int ID){
boolean found = false;
int i = 0;
for(i=0; i<size ; i++)
if (ID[i].equals(ID)
{
found = true;
break;
}
if (found) {
// remove the item and push all subsequent items to save space.
while (i < size - 1; i++) {
ID[i] = ID[i + 1];
}
}
return found;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.