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

Zen*_*011 1 java iterator data-structures

我有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
Run Code Online (Sandbox Code Playgroud)

现在.我的删除方法迫切需要帮助.我研究过ArrayList类及其方法.但只是编写stuList.remove()根本不起作用.我也尝试了迭代器方法(我迷失了那个).请指导我正确的方向..谢谢!

Ton*_*nny 5

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

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

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

  • 正如我从这些主题中看到的那样,这个问题最简单的解决方案.只需在找到匹配ID的索引处取消所有数组,分配就没有说明维护数组,只是应删除学生.顺便问一下:谁在教这个?这是可怕的代码! (2认同)