2012-06-05 108 views
0

嗨,我有一個字符串數組,像50個字符串,我希望它只選擇10個randon結果並將它們顯示在表格佈局上,我得到的所有東西除了它只顯示一個結果。繼承人我的代碼:從字符串數組中獲取不同的隨機字符串

private String[] list; 
private static final Random rgenerator = new Random(); 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.layout1); 

Resources res = getResources(); 
    list = res.getStringArray(R.array.names); 
    String q = list[rgenerator.nextInt(list.length)]; 
int total = 10; 

    for (int current = 0; current < total; current++) { 
     // Create a TableRow and give it an ID 
     TableRow tr = new TableRow(this); 
     tr.setId(100 + current); 
     tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 

     // Create a TextView to house the name of the province 
     TextView labelTV = new TextView(this); 
     labelTV.setId(200 + current); 
     labelTV.setText(q); 
     labelTV.setTextSize(14); 
     labelTV.setGravity(Gravity.CENTER); 
     labelTV.setTextColor(Color.WHITE); 
     labelTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
     tr.addView(labelTV); 

     tablelayout.addView(tr, new TableLayout.LayoutParams(
       LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
} 

最新怎麼了?

回答

0

通過使用一組相當簡單,那會照顧如何避免重複:

Set<Integer> uniques = new HashSet<Integer>(); 

while (uniques.size() < 10) 
    uniques.add(rgenerator.nextInt(list.length); 

for (Integer i : uniques) 
{ 
    String q = list[i]; 

    .. 
} 
+0

so in labelTV.setText(); 我把q? – SUPLMMM

+0

你需要將'list'中的所有'Strings'連接到一個變量,比如'text',然後調用'labelTV.setText(text)'; –

0

這就是所謂的「隨機抽樣」。兩種常見的技術是:

  • 只需洗刷整個列表/數組,確保使用像Collections.shuffle()提供的公平洗牌算法,然後取前n個元素;
  • 通過列表,每次根據隨機數是否超過表示仍然需要的物品數量/剩餘物品數量的閾值來決定是否包括下一個物品。

如果你選擇第一種方法,只要確保你的shuffle真的是一個公平的算法。許多程序員如果被要求從頭開始編寫一個shuffle算法,那就錯了。 Java的Collections.shuffle()是一個如何實現它的例子。

您可能也有興趣關於random sampling in Java,我寫了一篇文章,其中包括第二種情況的示例骨架代碼。

知識的緣故,您也不妨研究一些所謂的「蓄水池抽樣」,雖然它是矯枉過正,從一個可能的50

0

選擇10個項目這就是我想出了..

import java.util.*; 
public class RandomStringFromArray 
{ 
    public static void main(String[] args) 
    { 
     Random rnd = new Random(); 
     Scanner scan = new Scanner (System.in); 
     System.out.println("Enter how many string in the array"); 
     int x = scan.nextInt(); 
     ArrayList<String> arraystr = new ArrayList<String>(); 

     for (int i=0; i < x ; i++) 
      { 
       System.out.println("Enter elements of the array"); 
       arraystr.add(scan.next()); 
      } 
     System.out.println("\nI picked: "); 

     for (int t = 0; t < 3; t++) // 3 is how many elements you want to pick 
      { 

        int random = rnd.nextInt(x); 
        System.out.println(arraystr.get(random)); 
        arraystr.remove(random); 
      } 

    } 
} 
0

你只得到一個答案的原因是你得到了你的循環以外的字符串,所以你只能得到它一次。它應該看起來更像下面。

此外,如果你想避免重複,你將不得不做一些事情來照顧。 java.util.Collections.shuffle()可能適合你的目的,那麼你只需要前10個項目。

int total = 10; 

    for (int current = 0; current < total; current++) { 
     String q = list[rgenerator.nextInt(list.length)]; 
     // Create a TableRow and give it an ID