简体   繁体   English

返回布尔值/从数组中删除该元素的类 [JAVA]

[英]Class that returns boolean/removing that element from array [JAVA]

For a class project, I'm supposed to make a Gradebook with the four classes: Main (which just runs the gradebook), Gradebook (which has all the methods I will be using such as add student, remove student, add assignment, etc.), Student (which will maintain the student's name, number, email), and Assignment (which holds all the assignment details).对于班级项目,我应该制作一个包含四个班级的成绩簿:Main(仅运行成绩簿)、成绩簿(其中包含我将使用的所有方法,例如添加学生、删除学生、添加作业等) .)、学生(将保留学生的姓名、号码、电子邮件)和作业(保存所有作业详细信息)。 My instructor wrote most of the code design and he left us to write helper methods that find particular students and assignments and return a boolean.我的导师编写了大部分代码设计,他让我们编写帮助方法来查找特定的学生和作业并返回一个布尔值。 I need these helper methods but I don't know how to write it so that I could use the student or assignment they had found (ie how do I remove a student after the helper method had found said student?).我需要这些辅助方法,但我不知道如何编写它以便我可以使用他们找到的学生或作业(即,在辅助方法找到所述学生后如何删除学生?)。 All my students and assignments are in separate arrays (not array lists or anything else, per his instructions) and if there's another way without having the return types as booleans, I think I'm able to change the return types as well.我所有的学生和作业都在单独的数组中(不是数组列表或其他任何东西,按照他的指示),如果有另一种方法没有将返回类型作为布尔值,我想我也可以更改返回类型。

Here's my helper method:这是我的辅助方法:

   private boolean findStd (String stdName)
   {
       boolean thing = false;

       for(int i = 0; i < students.length; i++)
       {
           if(stdName.equalsIgnoreCase(students[i].studentName))
           {
               thing = true;
           }
       }

       return thing;
   }        

Here's my removeStudent method for now:这是我现在的 removeStudent 方法:

 private boolean removeStudent()
   {
       String name = JOptionPane.showInputDialog("What's the student's name (last, first)?");
       findStd(name);

       return false;
   }             // delete student from list 

Assuming the Student name is stored as "last, first", then you just need to iterate through the array, and delete it once you hit the correct student:假设学生姓名存储为“last, first”,那么您只需要遍历数组,并在找到正确的学生后将其删除:

for (int i = 0; i < students.length; i++) {
    if (students[i].studentName.equalsIgnoreCase(name)) {
        // delete student, e.g. students[i] = null;
    }
}

where name is the result from the JOptionPane.其中 name 是 JOptionPane 的结果。

Your logic for finding and removing seems to be off.您查找和删除的逻辑似乎已关闭。 In order to remove a student from an array you'd like for a couple things to happen为了从数组中删除学生,您希望发生一些事情

  1. I determine if the name of that person I am trying to remove exists.我确定我要删除的那个人的名字是否存在。
  2. If it does exist in my array then I will remove the student by telling it what name or index to use.如果它确实存在于我的数组中,那么我将通过告诉它使用什么名称或索引来删除学生。 (this is where you use the show input dialog) (这是您使用显示输入对话框的地方)

Try this:尝试这个:

//Change the method for find to return the index, but returns -1
// if the name isn't found which can be used to let a caller of 
// the function know that it does not exist as I will show in the removeStudent()
private int findStd (String stdName)
{
    int indexOfStudent = -1;

    for(int i = 0; i < students.length; i++)
    {
        if(stdName.equalsIgnoreCase(students[i].studentName))
        {
           indexOfStudent = i;
           break;
        }
    }
   //now we will either return a valid index or -1
   return indexOfStudent;
}

private boolean removeStudent()
{
    String name = JOptionPane.showInputDialog("What's the student's name (last, first)?");
    //if the name exists then our findStd() will give us the index
    int indexToRemove = findStd(name);
    if(indexToRemove != -1)
    {
        students[indexToRemove] = null;
        return true;
    }
    return false;
}

There are different ways to "remove" an element from an array.从数组中“删除”一个元素有不同的方法。 It depends on what you mean by "remove".这取决于您所说的“删除”是什么意思。

  1. You can assign null to the array at the position of the element you want to remove.您可以在要删除的元素的位置为数组分配null

    Problem: you now have null values in the array, and the rest of your code has to deal with that.问题:现在数组中null值,其余代码必须处理这个问题。

  2. You can use a loop (or a utility method) to copy elements left to fill in the "hole" left by the removed element.您可以使用循环(或实用程序方法)复制左侧的元素以填充已删除元素留下的“洞”。

    Problem: now you have a "hole" (ie nulls) at the right-hand end.问题:现在您在右手端有一个“洞”(即空值)。 Your code needs to deal with the fact that your conceptual array is smaller than the actual array length;您的代码需要处理概念数组小于实际数组长度的事实; eg例如

    for(int i = 0; i < students.length /** <<== wrong */ ; i++)
  3. You can create a new array with the correct size and copy the elements to it ... using one or two loops, or calls to utility methods.您可以创建一个具有正确大小的新数组并将元素复制到其中……使用一两个循环,或调用实用程序方法。

    Problem: you now need to update all places where you have a reference to the original array.问题:您现在需要更新所有引用原始数组的地方。

  4. You can use a List rather than an array.您可以使用List而不是数组。 The List API provides operations for removing elements. List API 提供删除元素的操作。

    Problem: You may not be able to change the declaration of students .问题:您可能无法更改students的声明。

There should be enough information in the above descriptions to tell you what your code needs to do ... in each case.在上面的描述中应该有足够的信息来告诉你你的代码需要做什么......在每种情况下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM