2017-01-01 6 views
0

こんにちはみんな...文字列配列に10個のランダムな文字列を得るには?文字列が取られている私は、Javaの配列に(96文字程度)文字列の集合からランダムに10の文字列を取る方法を作成する方法を知りたい

は、テキストファイル を形成し、これは、ファイルを読み取り、質問配列

Qを作成するための私のコードです

も私はすでにファイルを読み込み、ライン に変数を割り当てるための方法を作成し、私は質問の配列を作成した質問である

a ch o

b ....

CAが正解

public static Question[] readAllQuestions() throws FileNotFoundException { 
    int numberOfQuestions = CountQuestions(); 
    Question [] allQuestions = new Question[numberOfQuestions]; 
    Scanner file = new Scanner (new File("TestBank.txt")); 
    String q = ""; 
    String aa = ""; 
    String bb = ""; 
    String cc = ""; 
    String dd = ""; 
    String ccA = ""; 
    int x = 0 ; 
    int k = 0 ; 
    while (x< (allQuestions.length-1)) { 
     while (file.hasNext() && k == 0) { 
      String ques[] = file.nextLine().split(" "); 
      for (int ww = 0 ; ww< ques.length ; ww++) { 
       q += ques[ww]; 
       q+=" "; 
      } 
      if(ques[0].equals("")) { 
       k++ ; 
      } 
     } 
     while (k == 1) { 
      String a[] = file.nextLine().split(" "); 
      for (int ww = 0; ww< a.length; ww++) { 
       aa += a[ww]; 
       aa+= " "; 
      } 
      if(a[0].equals("")) { 
       k++ ; 
      } 
     } 
     //file.hasNext() && 
     while (k == 2) { 
      String b[] = file.nextLine().split(" "); 
      for (int ww = 0 ; ww< b.length ; ww++) { 
       bb += b[ww]; 
       bb+= " "; 
      } 
      if(b[0].equals("")) { 
       k++; 
      } 
     } 
     while (k == 3) { 
      String c[] = file.nextLine().split(" "); 
      for (int ww = 0; ww<c.length; ww++) { 
       cc += c[ww]; 
       cc+= " "; 
      } 
      if(c[0].equals("")) { 
       k++; 
      } 
     } 
     while (k == 4) { 
      String d[] = file.nextLine().split(" "); 
      for (int ww = 0; ww< d.length; ww++) { 
       dd += d[ww]; 
       dd+= " "; 
      } 
      if(d[0].equals("")) { 
       k++; 
      } 
     } 
     while (k == 5) { 
      String cA[] = file.nextLine().split(" "); 
      for (int ww = 0; ww<cA.length; ww++) { 
       ccA += cA[ww]; 
       ccA += " "; 
      } 
      if(cA[0].equals("")) { 
       k++; 
      } 
     } 
     while (k == 6) { 
      Question question = new Question(q,aa,bb,cc,dd,ccA); 
      allQuestions[x] = question; 
      q=""; 
      aa=""; 
      bb=""; 
      cc=""; 
      dd=""; 
      ccA=""; 
      x++; 
      k=0; 
     } 
    } 
    return allQuestions; 
} 

任意のアイデアですか? ありがとう

答えて

0

ランダムクラスを使用できます。 array[random.nextInt(array.length)]は、配列番号0からarray.length - 1までの任意の番号の配列要素を、nextIntメソッドで最後のインデックスとして除外します。

public static String[] getRandomStrings(String [] mainStrings, int i) { 
    String [] randomStrings = new String[i]; 
    Random random = new Random(); 
    for(int index = 0; index < i ; index++){ 
     randomStrings[index] = mainStrings[random.nextInt(mainStrings.length)]; 
    } 
    return randomStrings; 
} 
+0

私は配列を印刷すると、データだけではなくアドレスを出力します。 – Ammar

+0

[質問@ 3d4eac69、質問@ 42a57993、.......] – Ammar

+0

要素を印刷するためにループを使用します。 –

2

重複を選択せず​​に文字列の配列から10個のランダムな文字列が必要なようです。これを達成する簡単な方法の1つは、Collections::shuffleを使用して要素をランダムに並べ替えた後、シャッフルされたリストのサブセットを選択することです。ここに例があります:

public static void main(String[] args) throws Exception 
{ 
    String[] exStringArray = IntStream.range(0, 96) 
      .mapToObj(Integer::toString) 
      .toArray(size -> new String[size]); 

    System.out.println(getRandomElements(Arrays.asList(exStringArray), 10)); 
} 

private static <T> Collection<T> getRandomElements(Collection<T> c, int maxElements) 
{ 
    List<T> deepCopiedList = new ArrayList<>(c); 
    Collections.shuffle(deepCopiedList); 
    return Collections.unmodifiableList(deepCopiedList.subList(0, Math.max(Math.min(deepCopiedList.size(), maxElements), 0))); 
} 
+0

toStringをオーバーライドします。 – pscuderi

関連する問題