2011-06-26 37 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

?どうやってそれから削除しようとしましたか?このコードの壁ではなく、10行のプログラムで問題を再現できますか? – sarnold

+0

このコードで実際に削除しようとする試みはありません.userChoiceが2の場合は、最後のif else文になります(getInfoの先頭にあるメニューオプションを参照してください) – Snicolas

答えて

3

あなたはArrayList、ない配列としてCollectionなどを使用する必要があります。配列はJavaで固定サイズですが、動的ではありません。

実際に「削除」して配列を保持したい場合は、nullをセンチネル値として使用する必要があります。これは、null要素が「削除済み」であることを意味します。次に、あなたのプログラムの他のすべての面において、この意味を尊重するために他の方法を変更する必要があります。nullあなたが実際にはまだ配列を使用する必要がある場合

ArrayList ...

0

を使用して、あなたが建設的に、配列から項目を削除することができます。必要なのは、オリジナルのサイズよりも小さい新しい配列を作成し、関連するすべてのアイテムを新しい配列にコピーするだけです。私は配列から項目を削除する方法を示す例を書いています。

コードはここにある:あなたがから削除しようとしている配列に役立ちますhttp://pastebin.com/irnznxCA

・ホープ..

関連する問題