2011-06-26 143 views
-4

我很難刪除數組中的元素。我已經嘗試了一會兒,並且正在試圖弄清楚這一點,我正在毆打我的頭。我將不勝感激任何幫助。如何刪除數組中的元素?

package javaapplication9; 
import java.util.Scanner; 
import java.util.ArrayList; 

public class JavaApplication9 { 
    int x; 
    final int maxContacts = 3; 
    final String[] FIRSTNAME = { "Josh", "Joe", "Jim" }; 
    final String[] LASTNAME = { "Jones", "Smith", "Thomas" }; 
    final String[] ADDRESS = 
     { "142 Washington Ave", "500 Main St", "200 Oak Way" }; 
    final String[] CITY = { "Pittsburgh", "Pittsburgh", "Pittsburgh" }; 
    final String[] STATE = { "PA", "PA", "PA" }; 
    final String[] ZIP = { "15222", "15222", "15222" }; 
    final String[] TELEPHONE = 
     { "412-722-1500", "412-498-2500", "412-787-3500" }; 
    String[]firstName = new String[3]; 
    String[]lastName = new String[3]; 
    String[]address = new String[3]; 
    String[]city = new String[3]; 
    String[]state = new String[3]; 
    String[]zip = new String[3]; 
    String[]telephone = new String[3]; 
    String nameSearch; 
    boolean firstNameFound = true, lastNameFound = true, addressFound = 
     true, cityFound = true, stateFound = true, zipFound = 
     true, telephoneFound = true; 

    Scanner keyboard = new Scanner(System.in); 

    public void getInfo() { 

     while (x < maxContacts) { 
      Scanner input = new Scanner(System.in); 
      System.out. 
       println 
       ("Enter '1' To Add A Contact \nEnter '2' To Delete A Contact \nEnter '3' To Search For A Name \nEnter '4' To Display All Contacts \nEnter 'Q' To Quit"); 
      int usersChoice = input.nextInt(); 

      if (usersChoice == 1) { 

       System.out.print("Please enter a first name: "); 
       firstName[x] = keyboard.nextLine(); 
       if (firstNameFound) { 
        System.out. 
         print("Please enter a last name: "); 
        lastName[x] = keyboard.nextLine(); 
       } 
       if (lastNameFound) { 
        System.out. 
         print("Please enter an address: "); 
        address[x] = keyboard.nextLine(); 
       } 
       if (addressFound) { 
        System.out. 
         print("Please enter a city: "); 
        city[x] = keyboard.nextLine(); 
       } 
       if (cityFound) { 
        System.out. 
         print("Please enter a state: "); 
        state[x] = keyboard.nextLine(); 
       } 
       if (stateFound) { 
        System.out. 
         print("Please enter a zip: "); 
        zip[x] = keyboard.nextLine(); 
       } 
       if (zipFound) { 
        System.out. 
         print 
         ("Please enter a telephone number: "); 
        telephone[x] = keyboard.nextLine(); 
       } 

      } 

      if (usersChoice == 4) { 
       System.out.println("\nList Of Contacts"); 
       System.out. 
        println("------------------------------"); 
       for (int i = 0; i < FIRSTNAME.length; i++) { 
        System.out.println("First Name: " + 
           FIRSTNAME[i] + 
           "\nLast Name: " + 
           LASTNAME[i] + 
           "\nAddress: " + 
           ADDRESS[i] 
           + "\nCity: " + 
           CITY[i] + 
           "\nState: " + 
           STATE[i] + 
           "\nZip: " + ZIP[i] + 
           "\nTelephone: " + 
           TELEPHONE[i] + 
           "\n-------------------------\n"); 
       } 
       for (int i = 0; i < firstName.length; i++) { 
        System.out.println("First Name: " + 
           firstName[i] + 
           "\nLast Name: " + 
           lastName[i] + 
           "\nAddress: " + 
           address[i] 
           + "\nCity: " + 
           city[i] + 
           "\nState: " + 
           state[i] + 
           "\nZip: " + zip[i] + 
           "\nTelephone: " + 
           telephone[i]); 
       } 
      } 

      if (usersChoice == 3) { 
       System.out.print("\n\nPlease enter a name to find "); 
       // no idea how to 
       // search a name and 
       // display the 
       // corresponding 
       // number! 
       nameSearch = keyboard.next(); 

       for (int i = 0; i < firstName.length; i++) { 
        if (nameSearch.equals(firstName[i])) { 
         System.out.println("The name " + 
            firstName[i] 
            + 
            " was found " 
            + 
            "with the phone number " 
            + 
            firstName 
            [i]); 
        } 
       } 
      } 
     } 
    } 

    public static void main(String args[]) { 
     JavaApplication9 show = new JavaApplication9(); 
     show.getInfo(); 
    } 
} 
+0

這陣你想從刪除?你是如何嘗試從中刪除的?你能在十行程序中重現你的問題而不是這段代碼? – sarnold

+0

在此代碼中沒有真正的嘗試刪除操作,它將成爲userChoice爲2時的最後一個if else語句(讀取getInfo開始處的菜單選項) – Snicolas

回答

3

您應該使用一個CollectionArrayList,而不是一個數組。在java中數組的大小是固定的,它們並不是動態的。

如果你真的想要「刪除」並保留一個數組(這不是一個好主意),那麼你應該使用null作爲標記值,​​這意味着null元素被「刪除」。然後你必須改變你的其他方法來尊重你的程序的其他方面的這個含義null

使用ArrayList ...

0

如果你確實還是要使用數組,你可以從一個數組刪除項目,建設性的。所有你需要做的就是創建一個新的數組,它將比原始數組小一個,並將所有相關項目複製到新數組中。我寫了一個示例,顯示如何從數組中刪除項目。

的代碼是在這裏:http://pastebin.com/irnznxCA

希望幫助..