创建remove()方法以从arraylist中删除项(Java)

时间:2011-03-01 06:24:26

标签: java data-structures iterator

我有2个班级的学生班和StudentTest班。在我的Student课程中,我需要编写一个名为remove(int ID)的方法来从arraylist中删除具有特定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()根本不起作用。我也尝试了迭代器方法(我迷失了那个)。请指导我正确的方向..谢谢!

4 个答案:

答案 0 :(得分:5)

我会放弃解决眼前的问题并返回设计并获得正确的OOP,从

开始

1)学生,应该是一个集合,还是代表一个学生。

是介绍编程课程中的作业吗?

答案 1 :(得分:1)

我不明白为什么你必须将StudentID,Name和Tuition作为数组,学生班应该定义一个“学生”而不是多个学生。

第1课 - 学生

Student
{
   int ID;
   string Name;
   double Tution;
}

第2课 - StudentManager

StudentManager
{
   Student ListOfStudents;

   AddStudent();
   RemoveStudent();
}

编辑:

学生班代表一名学生,该学生的名字和学费,学生管理器的功能用于与学生对象进行交互,从列表等添加和删除它们,而不是让3个数组包含一个学生的信息,并试图更新他们所有,这是糟糕的设计,并很好学习尽早避免这种事情。

当我在开始编码之前学习OOP时,我常常会识别可以转换为类的可能对象,发现它们可能具有哪些属性以及它们如何与其他对象进行交互。

你会看到没有人会在这里发布你的问题的解决方案,因为这是家庭作业,但我们可以尝试帮助你了解如何解决你的问题。

答案 2 :(得分:0)

您有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(新的位置值)

如果您在执行此操作时遇到问题,请向我们展示一些代码并再次寻求帮助......

编辑:回复您的评论:

您有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 :(得分:-1)

你的代码中的问题是你的remote()方法以递归方式无限地调用自己而死于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;
}