Java 如何为菜单程序编写删除方法

Java 如何为菜单程序编写删除方法,java,arrays,methods,Java,Arrays,Methods,我一直在研究如何为正在编写的菜单(面向对象)程序编写删除方法。菜单为用户提供了删除在程序中输入的姓氏的选项。我完全被这部分卡住了!如果你们能给我一个简单的删除方法,我将不胜感激! 非常感谢 这是我迄今为止添加名称的代码: public static int addOne(String theArray[], int location, String theName, int noofvalues) { int step; if(noofvalues == 0)

我一直在研究如何为正在编写的菜单(面向对象)程序编写删除方法。菜单为用户提供了删除在程序中输入的姓氏的选项。我完全被这部分卡住了!如果你们能给我一个简单的删除方法,我将不胜感激! 非常感谢

这是我迄今为止添加名称的代码:

public static int addOne(String theArray[], int location, String theName, int noofvalues)
{
    int step;

    if(noofvalues == 0)
        {
            theArray[0] = theName;
            noofvalues++;
        }
    else
        {
            for(step = noofvalues - 1; step >= location; step--)
                {
                    theArray[step + 1] = theArray[step];
                }
            theArray[location] = theName;
            noofvalues++;
        }
    return noofvalues;
}
使用列表:

public static int addOne(List names, int location, String theName, int noofvalues)
{
    names.add(location, theName);
    return names.size();
}

public static void deleteOne(List names, int location){
    names.remove(location);
}

因为它看起来像是家庭作业,所以我根本不会提供任何代码(否则,我会解决家庭作业,而你不会学到任何东西),只提供一个想法,一个可能的算法来完成它

  • 将用户插入的姓氏存储在变量中。比方说,这个变量是
    lastInsertedName
  • 遍历数组中的所有元素,并在其中搜索此
    字符串。在数组中找到
    lastInsertedName
    时,将索引存储在变量中,比如
    indexFound
    并停止循环
  • 如果
    indexFound
    大于或等于0:
    • 将数组中从
      indexFound
      开始的所有元素向后移动一个位置
    • 减小用作数组大小的变量

请显示您现在拥有的一些代码。
public void delete(){lastName=”“;}
所以您真正的问题是如何从数组中删除元素,对吗?@luigimendoza Yes!我只是不知道怎么写,我建议用列表代替数组。甚至可能是今天,我已经使用了我所有的选票,但我明天会回来+1你的帖子!:)