java - 检测并删除 ArrayList 中的重复项?

标签 java android arrays multidimensional-array arraylist

我有一个有2个位置的数组,每个位置都包含一个“ids_alunos”列表,第一个位置有ids_alunos:“1,2,3”,第二个:“4,5”,但是当我添加时发生了什么我的数组中的 ids 是这样的,第一个位置获取“1,2,3”值,第二个位置获取“1,2,3,4,5”。为什么会发生这种情况?

public ArrayList<CadastraEscolas> getFilhos(String mToken) throws Exception {

        String[] resposta = new WebService().get("filhos", mToken);


        if (resposta[0].equals("200")) {

            JSONObject mJSONObject = new JSONObject(resposta[1]);
            JSONArray dados = mJSONObject.getJSONArray("data");



            mArrayList = new ArrayList<CadastraEscolas>();
            mGPSList = new ArrayList<GPSEscolas>();


            for (int i = 0; i < dados.length(); i++) {


                JSONObject item = dados.getJSONObject(i).getJSONObject("escolas");
                JSONArray escolas = item.getJSONArray("data");

                for (int j = 0; j < escolas.length(); j++) {

                    JSONObject jItens = escolas.getJSONObject(j);
                    mCadastraEscolas = new CadastraEscolas();
                    mGPSEscolas = new GPSEscolas();

                    mCadastraEscolas.setId_escola(jItens.optInt("id_escola"));


                    mGPSEscolas.setId_escola(jItens.optInt("id_escola"));



                    JSONObject alunos = jItens.optJSONObject("alunos");

                    JSONArray data = alunos.getJSONArray("data");

                    if (data != null) {

                        ArrayList<Filhos> arrayalunos = new ArrayList<Filhos>();

                        for (int a = 0; a < data.length(); a++) {

                            mFilhos = new Filhos();

                            JSONObject clientes = data.getJSONObject(a);

                            mFilhos.setId_aluno(clientes.optInt("id_aluno"));



                            arrayalunos.add(mFilhos);

                            idsAlunos  += ";" + arrayalunos.get(a).getId_aluno().toString();

                            mGPSEscolas.setIds_alunos(idsAlunos);

                        }


                        mCadastraEscolas.setalunos(arrayalunos);

                    }



                    mArrayList.add(mCadastraEscolas);
                    mGPSList.add(mGPSEscolas);



                }
            }

            return mArrayList;

        } else {
            throw new Exception("[" + resposta[0] + "] ERRO: " + resposta[1]);
        }

    }

最佳答案

您可能希望与 mGPSEscolas 同时初始化 idsAlunos,所以您可以这样做:

mGPSEscolas = new GPSEscolas();

你也可以这样做:

idsAlunos = "";

否则 idsAlunos 会被附加到每个新的 mGPSEscolas

关于java - 检测并删除 ArrayList 中的重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30623809/

相关文章:

android - 属性 app :cornerRadius doen't work for the MaterialButton

java - 如何将 Android Activity 中的变量提交到网站 url?

android - 如何读取assets中的excel文件?

python - numpy 按两个字段(升序和降序)对结构化数组进行排序

java - Bill Pugh 实现 Singleton 的方法导致 "Out-of-order Write"?

java - Karaf 中未列出的服务,可能是什么原因?

java - 从java valueobjects创建json对象时出错

java - Shiro 当前的主题是什么?

javascript - 访问数组中的所有变量

java - 如何检查多个二维数组的所有值是否都相同?