簡體   English   中英

檢測並刪除ArrayList中的重復項?

[英]Detect and Remove duplicates in ArrayList?

我有一個包含2個位置的數組,每個位置包含一個“ ids_alunos”列表,第一個位置具有ids_alunos:“ 1,2,3”,第二個位置具有“ ids_alunos”,但是當我在其中添加ID時會發生什么我的數組是這個,第一個位置獲取“ 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]);
        }

    }

您可能想與idsAlunos同時初始化mGPSEscolas ,因此您可以在其中進行以下操作:

mGPSEscolas = new GPSEscolas();

您也可以這樣做:

idsAlunos = "";

否則idsAlunos將附加到每個新的mGPSEscolas

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM