从字符串数组获取不同的随机字符串

时间:2012-06-05 02:49:45

标签: java android arrays string random

嗨,我有一个包含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));
}

怎么了?

4 个答案:

答案 0 :(得分: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];

  ..
}

答案 1 :(得分:0)

这称为“随机抽样”。两种常用技术是:

  • 简单地将整个列表/数组洗牌,确保使用像Collections.shuffle()提供的公平的洗牌算法,然后获取前n个元素;
  • 查看列表,每次根据随机数是否超过表示仍需要的项目数/剩余项目数的阈值来决定是否包含下一项。

如果你选择第一种方法,只需确保你的shuffle真的是一个公平的算法。许多程序员如果被要求从头开始编写一个shuffle算法就会出错。 Java的Collections.shuffle()是如何做到的一个例子。

您可能也对我之前写过的关于random sampling in Java的文章感兴趣,其中包括第二个案例的示例骨架代码。

为了便于学习,您可能还希望研究一种称为“水库采样”的东西,尽管从可能的50种物品中选择10种物品是过度的。

答案 2 :(得分:0)

这就是我想出的......

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);
            }

    }
}

答案 3 :(得分: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
相关问题