1

名前を配列に格納するメニューをコーディングしてから、生徒を追加/削除したり、特定の生徒を検索したりするオプションを提供していますが、他の場所で使用できるように配列を設定する方法がわからないようです。たとえば、私のコードでは、オプション1を使用した場合にのみ名前を入力できますが、オプション3を選択して配列内の名前を検索すると、これらの名前が配列に保持されず、nullとして表示されます。アレイ内のすべてのスロットに対して。

私が抱えているもう1つの問題は、生徒を削除する方法です。名前が配列の最後にある場合は明らかに簡単ですが、名前が中央にある場合は、名前を削除してシフトするにはどうすればよいでしょうか。アレイにギャップがないように、他のすべての名前を1つのスロットに配置しますか?

import java.util.Scanner;

public class Lab10Ex2 {

    public static void main(String[] args) {
        int choice = 0;
        int[] stringArray = {};

        do{         

        String[] stringarray = new String[20];

            System.out.println("----------------MENU---------------");
            System.out.println();
            System.out.println("1. Add Students");
            System.out.println("2. Delete Students");
            System.out.println("3. Search for student");
            System.out.println("4. Print all Students");
            System.out.println("5. exit");

            Scanner scanchoice = new Scanner(System.in);
            choice = scanchoice.nextInt();

            if (choice ==1){

                    Scanner scannames = new Scanner(System.in);

                System.out.println("Please enter the student names into the array");

                int i = 0;

                for(i = 0; i<stringarray.length; i++){

                    String temp =scannames.nextLine();

                    stringarray[i]=temp.toLowerCase();

                    if(i==(stringarray.length-1)){
                        System.out.println("successfully filled up array fully");
                        System.out.println();
                    }
                }
            }

            if(choice==2){

            }

            if(choice==3){

                for(int p = 0; p<stringarray.length; p++){

                                    System.out.println(stringarray[p]);

                }
                                int x=0;
                                Scanner scannames1 = new Scanner(System.in);

                                System.out.println("Enter name of student you want to search for: ");
                                System.out.println();
                                String search=scannames1.nextLine();
                                String searchName=search.toLowerCase();

                                for(int p=0;p<20;p++){
                                    if(searchName.equals(stringarray[p])){
                                        x=1;

                                        }
                                    else{
                                        x=0;
                                    }
                                }

                                if(x==1){
                                    System.out.println("We have a match in our database for "+ searchName);
                                }
                                else if (x==0){
                                    System.out.println("No match for "+ searchName);
                                }
                            }

                            if (choice ==4){
                                System.out.println("List of names:");

                                for(int p = 0; p<stringarray.length; p++){
                                    System.out.println(stringarray[p]);
                                }
                            }
        }while(choice!=5);
    }
}
4

3 に答える 3

2
    int choice = 0;
    int[] stringArray = {};

    do{         

        String[] stringarray = new String[20];

行を削除しint[] stringArrayます(どこにも参照しません)。

String[] stringarrayループの外側で上に移動します。

削除に関しては、自分でコーディングするか(削除されたアイテムを超えて配列内の1つ上に移動する)、またはJavaで提供されるコレクションクラスの1つ(ネイティブ配列ではなく)を使用して削除を処理する必要があります。

于 2012-05-10T16:16:24.380 に答える
1
do{         

    String[] stringarray = new String[20];

Do {...}ループを繰り返すたびに、stringarray変数を再作成し、それをクリアします。これをループの外に移動すると、学生のエントリが維持されます。

削除に関しては、文字列の配列を使用する必要がない場合は、ArrayListを使用することをお勧めします。他のエントリを気にすることなく、特定のエントリを簡単に削除できます。それ以外の場合は、はい、最も簡単な方法は、ギャップを回避するために他のすべてのエントリを1つのスロットに移動することです。

于 2012-05-10T16:19:16.127 に答える
0

修正されたコードは次のとおりです。

import java.util.Scanner;


public class Lab10Ex2 {

/**
 * @param args
 */
public static void main(String[] args) {
    int choice = 0;
    int[] stringArray = {};
    String[] stringarray = new String[20];

    do{         




        System.out.println("----------------MENU---------------");

        System.out.println();
        System.out.println("1. Add Students");
        System.out.println("2. Delete Students");
        System.out.println("3. Search for student");
        System.out.println("4. Print all Students");
        System.out.println("5. exit");



        Scanner scanchoice = new Scanner(System.in);
        choice = scanchoice.nextInt();

        if (choice ==1){

            Scanner scannames = new Scanner(System.in);

            System.out.println("Please enter the student names into the array");
            System.out.println();

            int i = 0;


            for(i = 0; i<stringarray.length; i++){



                String temp =scannames.nextLine();

                stringarray[i]=temp.toLowerCase();

                if(i==(stringarray.length-1)){
                    System.out.println("successfully filled up array fully");
                    System.out.println();
                }


            }







        }

        if(choice==2){


        }


        if(choice==3){


            for(int p = 0; p<stringarray.length; p++){

                System.out.println(stringarray[p]);

            }
            int x=0;
            Scanner scannames1 = new Scanner(System.in);

            System.out.println("Enter name of student you want to search for: ");
            System.out.println();
            String search=scannames1.nextLine();
            String searchName=search.toLowerCase();

            for(int p = 0; p < stringarray.length ;p++){
                if(searchName.equals(stringarray[p])){
                    x=1;
                    break;
                }
                else{
                    x=0;
                }
            }



            if(x==1){
                System.out.println("We have a match in our database for "+ searchName);
            }
            else if (x==0){
                System.out.println("No match for "+ searchName);
            }
        }





        if (choice ==4){
            System.out.println("List of names:");

            for(int p = 0; p<stringarray.length; p++){

                System.out.println(stringarray[p]);
                System.out.println();


            }

        }










    }while(choice!=5);

}

}

あなたが間違っていたこと:

  1. dowhileループで配列をインスタンス化します。
  2. 配列内に検索エンティティが見つかった場合でも、ループから抜け出さない。

削除後に配列のスペースを無駄にしないようにする場合は、ArrayListを使用します。単純な配列でこれにバインドされている場合は、次のように実行できます。

削除したインデックスにnullを配置します。すべてのnull値を使用して、元の配列と同じサイズの一時配列を作成します。元の配列から一時配列にすべての要素をコピーしますが、nullの要素はスキップします。元の配列を新しい配列にポイントします。

ハードコーディング配列の長さを避けてください!

于 2012-05-10T16:34:01.733 に答える