0

私はプログラミングの課題を抱えており、次のタスクを課されています。

2 つの int 値 (x と y) を取り、2 つの異なる配列を作成しています。 (サイズ x) で、独自の配列に格納します。次に、2 番目の配列を出力します。ただし、2 番目の配列に繰り返し値を含めることはできません。たとえば、配列のサイズが 10 の場合、10 個の個々のインデックス内に同じ数字を 2 つ持つことはできません。一意の要素をチェックするためのブール値と、それらの一意の要素を格納するためのブール値の 2 つの配列を作成して、2 番目の配列に一意の要素を格納しようとしています。これが私のコードです:

/*
* user will enter desired size x for first array labeled arr_1
* arr_1 will contain values descending from x down to 1
* user will enter desired size y for second array labeled arr_2
* arr_2 will contain random values taken from arr_1 w/o repeating numbers
*/

import java.util.Arrays;
// import java.util.Arrays;
import java.util.Random;
// import java.util.Scanner;
public class Prog1B  
{
    public static void main(String[] args)
    {
        System.out.println("Program 1B, Christopher Moussa, masc1574");
        // Scanner scnr = new Scanner(System.in); 
        int x = 20;
        int v = x;
        int[] arr_1 = new int[x];

        for (int i = x-1; i >= 0; i--)
        {
            arr_1[i] = v;   // System.out.print(i+1 + " "); prints 20, 19, ... , 1
            v--;            // System.out.print(arr_1[i] + " "); prints 20, 19, ... , 1
        }
        // int[] b = unique(arr_1);
        System.out.println(Arrays.toString(unique(arr_1)));
    }

    public static int[] unique (int[] n)
    {
        boolean[] seen = new boolean[n.length];
        int[] unique = new int[n.length];
        Random rand = new Random(123L);
        for (int i = 0; i < n.length; i++)
        {
            int index = rand.nextInt(n.length);
            while (seen[index])
            {
                index = rand.nextInt(n.length);
            }
            unique[i] = n[index];
        }
        return unique;
    }



}

コードはコンパイルおよび実行されますが、値が繰り返される配列が出力されます。繰り返し値を持つ配列を出力せず、一意の値のみを出力するようにプログラムを作成しようとしています。問題がどこにあるのかについて何か提案はありますか? より具体的には、ブール配列が一意の値をチェックしているときに、それが「一意の」メソッド内にあると確信しています(デバッグしようとしているときに、生成されたランダムインデックスが一意ではなくても、while 条件をスキップし、印刷しました)。私は初心者のプログラマー (コンピューター サイエンスを勉強しているサンディエゴ州立大学の新入生) であり、どんなフィードバックやアドバイスも大歓迎です。どうもありがとうございます。

4

4 に答える 4

2

配列 seen[index] = true; を設定する必要さえあります。

public static int[] unique (int[] n)
    {
        boolean[] seen = new boolean[n.length];
        int[] unique = new int[n.length];
        Random rand = new Random(123L);
        for (int i = 0; i < n.length; i++)
        {
            int index = rand.nextInt(n.length);
            while (seen[index])
            {
                index = rand.nextInt(n.length);
            }
            unique[i] = n[index];
            seen[index] = true;
        }
        return unique;
    }
于 2016-02-10T04:08:16.140 に答える
2

あなたのコードに問題が見つかりました。「見た」ブール配列を更新することはありません。修正については、以下のコードを参照してください。

public static int[] unique (int[] n){
 boolean[] seen = new boolean[n.length];
 int[] unique = new int[n.length];
 Random rand = new Random(123L);
 for (int i = 0; i < n.length; i++)
 {
     int index = rand.nextInt(n.length);
     while (seen[index])
     {
         index = rand.nextInt(n.length);
     }
     seen[index] = true; //boolean array updated
     unique[i] = n[index];
 }
 return unique;

}

この修正を使用して、以下の出力を取得できました (繰り返しはありません)。

[3、11、17、10、16、18、15、6、14、20、7、13、1、19、9、2、5、4、12、8]

于 2016-02-10T04:09:16.870 に答える
1

Unless you specifically have to do it this way, I suggest you take a step back and try a totally different approach, something like this:

Set<int> mySet = new HashSet<int>(Arrays.asList(someArray));

NOTE: You will want to adjust the return type of unique() to be Set

The rest of the implementation is left as an excercise for the reader. Basically you take the array and convert it to a set as the example above.

(Credit where credit is due)

I just wanted to steer you in the right direction per https://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions

Good luck, I would say the biggest lesson here is how to walk away from code that has become inefficient when a better solution exists. Good luck!

于 2016-02-10T04:06:39.140 に答える