随机化Java中的ArrayList <object>

时间:2015-06-27 00:48:37

标签: java arraylist shuffle

鉴于我有一个公司对象的arrayList。我想要将列表公司内的对象洗牌。 listcompanies里面有一个对象公司列表。我想在列表公司arraylist中洗牌公司对象。

这是我的公司对象

public class Company {
private String id;
private String companyName;
private String currentTaskId;

private String task1_id;
private String task1_url;
private String task1_title;
private String task1_description;
private String task1_type;


private String task2_id;
private String task2_url;
private String task2_title;
private String task2_description;
private String task2_type;

private String task3_id;
private String task3_url;
private String task3_title;
private String task3_description;
private String task3_type;

private String task4_id;
private String task4_url;
private String task4_title;
private String task4_description;
private String task4_type;

private String task5_id;
private String task5_url;
private String task5_title;
private String task5_description;
private String task5_type;
}

这是我存储数据的方式:

//create a company ArrayList
ArrayList<Company> listcompanies = new ArrayList<>();
for(int i = 0 ; i < 60 ; i ++){
   //Initialise a new company per iteration 
   Company company = new Company();
   //store data
   company.setcompanyName("Name: " + i);
   company.setcompanycurrentTaskId(i+"");
   listcompanies.add(company);
}

我尝试这样做来洗牌公司,但它没有用。

// I TRIED doing this for sorting the listcompanies
    long seed = System.nanoTime();
    Collections.shuffle(listcompanies, new Random(seed));
// nothing happened it did not sort the list of companies

1 个答案:

答案 0 :(得分:0)

我的问题有点令人困惑。虽然我已经弄明白了,当它被包含在arraylist中时,随机数据随机化。因此它一直是随机的。