簡體   English   中英

我的刪除方法不起作用

[英]My Remove Method isn't Working

創建一個程序,以跟蹤用戶輸入的以下信息:

First NameLast NamePhone NumberAge

現在-讓我們將其存儲在一個多維數組中,該數組可以容納10個聯系人。 因此,我們的多維數組將需要為10行4列。 您應該能夠添加,顯示和刪除數組中的聯系人。 * /

import java.util.Scanner;

public class compLab2 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in); 
        String[][] contacts = new String [10][4]; 
        System.out.println("Select your options: \n");

        while(true){
            // Give the user a list of their options
            System.out.println("1: Add a new contact to list.");
            System.out.println("2: Remove a contact from the list.");
            System.out.println("3: Display contacts.");
            System.out.println("0: Exit the list.");

            // Get the user input
            int userChoice = input.nextInt();

            switch(userChoice){
                case 1:             
                    addContacts(contacts);
                    break;

                case 2: 
                    removeContacts(contacts);
                    break;

                case 3: 
                    displayContacts(contacts);
                    break;

                case 0: 
                    System.out.println("Goodbye!");
                    System.exit(0);
            }
        }
    }

    private static void addContacts (String [][] contacts) {
        Scanner input = new Scanner(System.in); 
        for (int i = 0; i < 10; i++) {
            if (contacts[i][0] == null || contacts[i][0].equals(null)) {
                contacts[i][0] = input.nextLine();
                contacts[i][1] = input.nextLine();
                contacts[i][2] = input.nextLine();
                contacts[i][3] = input.nextLine();
                boolean Inserted = true;
                break;
            }
        }            
    }

    private static void removeContacts(String [][] contacts) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the name of the contact you want to remove: ");
        String removeContact = input.nextLine();
        for(int i=0; i<contacts.length; i++){
            for(int j = 0; j <contacts[i].length; j++){
                if(contacts[i][j].equals(removeContact)) {
                    contacts[i][0]=" "; 
                    contacts[i][1]=" "; 
                    contacts[i][2]=" ";
                    contacts[i][3]=" "; 
                }
            }
            break;                  
       }
   }

   private static void displayContacts(String [][] contacts) {
       for (int i = 0; i< contacts.length; i++) {
           for (int j= 0; j < contacts[i].length; j++) {
               System.out.print(contacts[i][j] + " ");
           }
           System.out.println();
       }
    }
}
  • 您必須將中斷移動到if條件內(一旦元素被移除,則中斷循環),否則您的第一個for循環將在i = 0條件下中斷。
  • 在if條件下,您必須選中帶有contacts[i][j] removeContact ,因為contacts[i][j]可以為空。

在下面可以找到代碼

    if (removeContact != null) { //removeContact should not be null
        for (int i = 0; i < contacts.length; i++) {
            for (int j = 0; j < contacts[i].length; j++) {
                if (removeContact.equals(contacts[i][j])) { //removeContact is not null so check removeContact with contacts[i][j]
                    contacts[i][0] = " ";
                    contacts[i][1] = " ";
                    contacts[i][2] = " ";
                    contacts[i][3] = " ";
                    break; //break the loop once you remove
                }
            }
        }
    }

您應該將null設置為white space(" ")

if (removeContact != null) {
    for (int i = 0; i < contacts.length; i++) {
        for (int j = 0; j < contacts[i].length; j++) {
            if (removeContact.equals(contacts[i][j])) {
                contacts[i][0] = null;
                contacts[i][1] = null;
                contacts[i][2] = null;
                contacts[i][3] = null;
                break;
            }
        }
    }
}

因為您在添加聯系人時正在檢查null。 因此,將其設置為null將釋放數組中的空間,並允許您向該空間添加新的聯系人。

另外,在顯示聯系人時,您應該檢查空條目

private static void displayContacts(String [][] contacts) {
    for (int i = 0; i < contacts.length; i++) {
        if (contacts[i][0] != null) {
            for (int j= 0; j < contacts[i].length; j++) {
                System.out.print(contacts[i][j] + " ");
            }
            System.out.println();
        }
    }
}

具有自定義類的ArrayList可以輕松解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM