当前位置: 首页 > 知识库问答 >
问题:

用方法Java添加用户元素多维数组

鄂慈
2023-03-14
    public static void main(String[]args)
    {
        new Lab2();
    }

    public Lab2()
    {
        Scanner input = new Scanner(System.in);

        while(true)
        {
            System.out.println("Welcome to the Contact Directory, please select from following: ");
            System.out.println("Type 1 to add contact, 2 to remove contact, 3 to display contacts and 0 to exit.");
            int intChoice = input.nextInt();
            input.nextLine();

            String strFirstName; 
            String strLastName;
            switch(intChoice)
            {
                case 1:
                System.out.println("Enter First Name");
                strFirstName = input.nextLine();

                System.out.println("Enter Last Name");
                strLastName = input.nextLine();

                System.out.println("Enter Phone Number");
                String strPhoneNumber = input.nextLine();

                System.out.println("Enter Age");
                String strAge = input.nextLine();

                addItem(strFirstName, strLastName, strPhoneNumber, strAge);
                break;
                case 2:
                System.out.println("Enter First Name to Remove");
                strFirstName = input.nextLine();

                System.out.println("Enter Last Name to Remove");
                strLastName = input.nextLine();

                removeItem(strFirstName, strLastName);
                break;
                case3:// GETTING AN ERROR UNREACHABLE STATEMENT
                displayItems();
                break;
                case 0:
                System.exit(0);
            }
        }
    }

    public void addItem(String strFirstName, String strLastName,
    String strPhoneNumber, String strAge)
    {

       for(int i = 0; i < myArray.length; i++)
    {
        for(int j = 0; j < myArray[i].length; j++)
        {
           if (myArray[i][0] == null)
           {
               myArray[i][0] = strFirstName;
               myArray[i][1] = strLastName;
               myArray[i][2] = strPhoneNumber;
               myArray[i][3] = strAge;
               ; break;
            }
       }
    }
}

    public void removeItem(String strFirstName, String strLastName)
    {
         int i, j;

         for(i = 0; i < myArray.length; i++)
         {
             for(j = 0; j < myArray.length - 1; j++){
                     myArray[i][j] = myArray[i + 1][j];
                     break;
                }
            }
        }

    public void displayItems()
    { 
        for(int i = 0; i < myArray.length; i++)
        {
            for (int j = 0; j < myArray[i].length; j++)
            {
                System.out.print(myArray[i][j] + " ");
            }
            System.out.println();
        }
    }

共有1个答案

尹俊贤
2023-03-14

你的提示应该是用来修复你的问题的代码。找到下一个可用的并添加它。

    if (myArray[i][0] == null)
    {
        myArray[i][0] = strFirstName;
        myArray[i][1] = strLastName;
        myArray[i][2] = strPhoneNumber;
        myArray[i][3] = strAge;
        blnInserted = true; 
        break;
    }

您不需要第二个循环,因为您只是根据名字检查整个条目是否null。在移除封闭的第二个循环后,代码应该添加到您感到困惑的部分。

此外,不需要在之后进行null检查。如果是null,您将无法检查相等性,因为这样做需要一个不能是null的实例。

 类似资料:
  • 问题内容: 我有一个多维数组$ md_array,我想将更多元素添加到子数组recipe_type和美食中,这些美食来自从表中读取数据的循环。 在循环中,我为每行创建一个新表$ newdata: 然后,我需要将$ newdata数组追加到以下多维数组: 使用array_push将新元素(数组)添加到recipe_type数组的语法是什么?我永远无法绕过多维数组,我有点困惑。 问题答案: 如果要在关

  • 问题内容: 好的,所以我花了几个小时试图用Java中的HashMap这个概念来解决问题,但只是无法弄清楚。我看过许多教程,但似乎都没有一个能满足我的确切要求,我无法使其正常运行。 我试图用Java(或类似的东西)创建一个关联的多维数组,以便我既可以使用字符串键又可以保存到数组或从数组中检索。 这就是我将在PHP中执行的操作,并最好地解释了我要执行的操作: 这是我在Java中尝试过的方法,但无法正常

  • 本文向大家介绍numpy向空的二维数组中添加元素的方法,包括了numpy向空的二维数组中添加元素的方法的使用技巧和注意事项,需要的朋友参考一下 直接上代码了 这样就添加了两行4列的数据了。注意append里面是两层括号,这个非常重要,如果漏掉了就不是二维数组了,用axis的时候就会报维度不匹配。 以上这篇numpy向空的二维数组中添加元素的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,

  • 本文向大家介绍Java数组添加元素实例,包括了Java数组添加元素实例的使用技巧和注意事项,需要的朋友参考一下 以下实例演示了如何使用sort()方法对Java数组进行排序,及如何使用 insertElement () 方法向数组插入元素, 这边我们定义了 printArray() 方法来打印数组: MainClass.java 文件: 以上代码运行输出结果为: 数组排序: [length: 10

  • 本文向大家介绍jQuery使用addClass()方法给元素添加多个class样式,包括了jQuery使用addClass()方法给元素添加多个class样式的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了jQuery使用addClass()方法给元素添加多个class样式的方法。分享给大家供大家参考。具体如下: jQuery通过addClass()方法给元素添加多个class,只需要在添

  • 本文向大家介绍jQuery 添加元素和删除元素的方法,包括了jQuery 添加元素和删除元素的方法的使用技巧和注意事项,需要的朋友参考一下 添加新的 HTML 内容 我们将学习用于添加新内容的四个 jQuery 方法: append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在被选元素之后插入内容 before() - 在被选元素之前插