2017-01-01 194 views
0

嗨,大家好,我想知道如何创建在Java中的数组中随机取10串从一组字符串(约96串)的方法...如何获得10个随机字符串的字符串数组?

的字符串被形式的文本文件 和我已经创建了一个方法来读取文件并分配变量线 和我创建问题的阵列也

这是我的代码读取该文件,并创建一个问题阵列

q是问题

a ch香料a

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)]给出了从0array.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个随机字符串而不选择重复。一个简单的方法是使用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