Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;

import java.util.Collections;

public class Main {
    public static void split(String[] items, ArrayList<String> res, int number) {
        // split items to res1 and res2, where res2 has number items
        res.clear();
        String res1 = "";
        String res2 = "";
        ArrayList<String> newList = new ArrayList<String>();
        for (int i = 0; i < items.length; i++) {
            newList.add(items[i]);
        }
        Collections.shuffle(newList);
        for (int i = 0; i < newList.size(); i++) {
            if (i < number) {
                res1 += newList.get(i) + "\t";
            } else {
                res2 += newList.get(i) + "\t";
            }
        }
        res.add(res1);
        res.add(res2);
    }

    public static <T> ArrayList<T> shuffle(ArrayList<T> population, int sample) {
        ArrayList<T> newList = new ArrayList<T>();
        ArrayList<T> ret = new ArrayList<T>();
        newList.addAll(population);

        Collections.shuffle(newList);
        ret.addAll(newList);
        for (int i = sample; i < ret.size(); i++) {
            ret.remove(i);
            i--;
        }
        newList.clear();
        return ret;
    }
}