随机抽样一个没有替换的arraylist子集

时间:2014-06-30 10:28:49

标签: java arraylist shuffle

我有一个输入,每行包含一个符号,并使用扫描仪读入一个arralist(也许)。我希望从这个arraylist(size = 3)中随机获得1000个数据子集,并将其逐行保存到文件中(每行都是一个子集)。

所以输入就像:

AAA
BBB
CCC
DDD
EEE
FFF

预期输出为:

AAA EEE FFF
CCC FFF BBB
DDD BBB AAA

这是我到目前为止所拥有的。我有点坚持如何指示1000次洗牌以及如何定义size = 3。我希望如果我可以洗牌1000次并且每次拿到前3个。这是我的策略。

public class sampling {
    public static void main(String[] args) throws FileNotFoundException{
        Scanner scan1 = new Scanner(new File("Symbol.txt"));
        ArrayList<String> Wholelist = new ArrayList<>();
        while (scan1.hasNextLine()){
            String line = scan1.nextLine();
            Wholelist.add(line);
        }
        try{    
            FileWriter stream = new FileWriter(args[2]);
            BufferedWriter out = new BufferedWriter(stream);
            for (int i=0,n=Wholelist.size();i<n; i++){
                Collections.shuffle(Wholelist);
                Wholelist.get(3);
            }
        }
        catch (IOException e) {
            System.err.println("Error: "+ e.getMessage());
        }
    }
}

请尝试根据我的代码修改它,因为我是Java的初学者。非常感谢,如果有人可以提供帮助的话。

3 个答案:

答案 0 :(得分:1)

你走在正确的轨道上。只需循环你的循环1000次而不是n!要获取列表的前三个元素,请使用方法subList(0,3)

答案 1 :(得分:1)

尝试类似的东西以满足您的要求:

public static void main(String[] args) {
    List<String> wholeList = new ArrayList<>();
    wholeList.add("AAA");
    wholeList.add("BBB");
    wholeList.add("CCC");
    wholeList.add("DDD");
    wholeList.add("EEE");
    wholeList.add("FFF");
    wholeList.add("GGG");

    int randomLimit = wholeList.size();
    Random r = new Random();

    for (int i = 0; i < 1000; i++) {
        // get three random indices from 0(inclusive) to list max size
        // (exclusive)
        int u = r.nextInt(randomLimit);
        int v = r.nextInt(randomLimit);
        int w = r.nextInt(randomLimit);
        // chose your format
        System.out.print(wholeList.get(u) + "\t");
        System.out.print(wholeList.get(v) + "\t");
        System.out.println(wholeList.get(w));
    }
}

答案 2 :(得分:1)

试试这个

                import java.io.BufferedWriter;
                import java.io.File;
                import java.io.FileNotFoundException;
                import java.io.FileWriter;
                import java.io.IOException;
                import java.util.ArrayList;
                import java.util.Collections;
                import java.util.Random;
                import java.util.Scanner;

                public class FileTester {
                    public static void main(String[] args) throws FileNotFoundException {
                        Scanner scan1 = new Scanner(new File("YOUR_INPUT_FILE"));
                        ArrayList<String> wholeList = new ArrayList<String>();
                        String line = "";
                        while (scan1.hasNextLine()) {
                            line = scan1.nextLine();
                                wholeList.add( line );
                        }
                        try {
                            FileWriter stream = new FileWriter("YOUR_OUTPUT_FILE");
                            BufferedWriter out = new BufferedWriter(stream);
                            for (int i = 0, n = wholeList.size(); i < n; i++) {
                                Random r = new Random();
                                Collections.shuffle(wholeList);
                                out.append(wholeList.get( r.nextInt(wholeList.size()) ) + "\t");
                                out.append(wholeList.get( r.nextInt(wholeList.size()) ) + "\t");
                                out.append(wholeList.get( r.nextInt(wholeList.size()) ) + "\t");
                                out.append("\n");
                            }
                            out.close();
                        } catch (IOException e) {
                            System.err.println("Error: " + e.getMessage());
                        }
                    }
                }