动态创建ArrayList并将对象添加到每个ArrayList

时间:2019-03-31 18:09:31

标签: java android arraylist dynamic

我正在尝试动态创建多个ArrayList,并且我想向每个对象添加对象。

每个ArrayList不得大于其包含的所有对象的平均值Player.getNum(),该平均值是在此调用之前计算的。另外,必须将其改组。

    Bundle bundle = this.getArguments();
    getArrayList = (ArrayList<Player>) getArguments().getSerializable(KEY_PLAYERS);
    getArrayList = (ArrayList<Player>) getArrayList.clone();
    int teams = bundle.getInt("number_of_teams" , 0);
    double avg = bundle.getDouble("avg" , 0);

    getListToCreateLists(getArrayList , teams , avg);
    onClick(refreshButton);
    onClick(backFromShowinBtn);
    return v;
}

private boolean checkPlayerIfExists(String name, ArrayList<Player> list) {
    for (Player o : list) {
        if (o != null && o.getName().equals(name)) {
            return true;
        }
    }
    return false;

}

public void onClick(View v) {
    v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.backFromShowinBtn:
                    FragmentManager fm = getActivity().getSupportFragmentManager();
                    fm.popBackStack();
                    break;
                case R.id.refreshButton:
                    ((MainActivity2) getActivity()).refresh();
                    break;
                default:
                    break;
            }
        }
    });
}

public void getListToCreateLists(ArrayList<Player> bigList , int teams , double avg) {

    //bigList = number of objects the user insert;
    // teams = number of teams the user insert;
    // avg = the avarge of objects.getnum() / teams;
    //object.getnum() = Player (object) int num (value);
    Log.d(TAG, "GetArrayList: " + getArrayList.size() + ",--------- avg " + avg + ", ------ teams " + teams);

    ArrayList<Player>[] d = (ArrayList<Player>[]) new ArrayList[teams];
    for (int i = 0; i < teams; i++) {
        d[i] = new ArrayList<Player>();
    }

    Collections.shuffle(bigList);
    for (int i = 0; i < bigList.size(); i++) {

        Player current = bigList.get(i);
        //here should be the method to insert objects randomly to all the arraylists that has been created above.. 

    }
}

0 个答案:

没有答案