创建remove()方法以从arraylist中删除项(Java)
我有两个班,学生班和学生测试班。在我的Student类中,我需要编写一个名为remove(intid)的方法来从arraylist中删除具有特定ID的Student。该方法由StudentTest类中的main()方法调用。代码如下所示:创建remove()方法以从arraylist中删除项(Java),java,data-structures,iterator,Java,Data Structures,Iterator,我有两个班,学生班和学生测试班。在我的Student类中,我需要编写一个名为remove(intid)的方法来从arraylist中删除具有特定ID的Student。该方法由StudentTest类中的main()方法调用。代码如下所示: public class Student { private final int size = 12; //max. size private int [] ID = new int [size]; //the student's i
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
公共班级学生{
私有最终整数大小=12;//最大大小
private int[]ID=new int[size];//学生的ID号
私有字符串[]名称=新字符串[大小];//学生姓名
私人双人[]学费=新双人[尺码];
int position=0;//添加数据的位置
//这里有一个add()方法,但我的问题不是这样,所以我要发出它
//下面是remove()命令,用于删除给定ID号的学生
公共无效删除(int ID){
对于(int i=0;i代码中的问题是,remove()方法递归地调用自身,以使用
public void删除(int-ID){
布尔值=false;
int i=0;
对于(i=0;i代码中的问题是,remove()方法递归地调用自身,以使用
public void删除(int-ID){
布尔值=false;
int i=0;
对于(i=0;i我会放弃解决眼前的问题,回到设计上来,从
1) 学生,这应该是一个集合还是代表一个学生
这是程序设计入门课程中的一项作业吗?我会放弃解决眼前的问题,回到设计上来,从
1) 学生,这应该是一个集合还是代表一个学生
这是入门编程课程中的作业吗?您有3个数组和一个int位置
来存储添加数据的位置。如果您删除了一个学生,您必须:
1) 找到它在数组中的位置,比如说r
是要删除的位置(它可以在0和位置-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...
就是这样…您有3个数组和一个int位置来存储要添加数据的位置。如果您删除一个学生,您必须:
1) 找到它在数组中的位置,比如说r
是要删除的位置(它可以在0和位置-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...
就是这样…我不明白为什么必须将StudentID、Name和tutory作为数组,student类应该定义一个“student”,而不是多个student
一班-学生
Student
{
int ID;
string Name;
double Tution;
}
二班-学生经理
StudentManager
{
Student ListOfStudents;
AddStudent();
RemoveStudent();
}
编辑:
Student类代表一个学生,该学生的特征(如姓名和学费)StudentManager用于与学生对象交互,添加和从列表中删除学生对象等,而不是使用包含一条学生信息的3个数组并尝试更新所有学生信息,这是一个糟糕的设计,也是一个错误尽早学会避免这种事情很好
当我在开始编写代码之前学习OOP时,我用它来识别可能被转换成类的对象,发现它们可能具有的属性以及它们如何与其他对象交互
你会发现没有人会在这里发布你问题的解决方案,因为这是家庭作业,但我们可以尝试帮助你理解如何解决你的问题。我不明白为什么你必须将学生ID、姓名和学费作为数组,学生类应该定义一个“学生”,而不是多个学生
一班-学生
Student
{
int ID;
string Name;
double Tution;
}
二班-学生经理
StudentManager
{
Student ListOfStudents;
AddStudent();
RemoveStudent();
}
编辑:
Student类代表一个学生,该学生的特征(如姓名和学费)StudentManager用于与学生对象交互,添加和从列表中删除学生对象等,而不是使用包含一条学生信息的3个数组并尝试更新所有学生信息,这是一个糟糕的设计,也是一个错误尽早学会避免这种事情很好
当我在开始编写代码之前学习OOP时,我用它来识别可能被转换成类的对象,发现它们共同的属性