ArrayList删除函数有bug吗?

时间:2015-07-20 02:52:41

标签: android arraylist

这是我的代码

  public void onClick(View view){
            Logger.debug(TAG, view.getId()+"-->ID");
            ImageView img = (ImageView)hasID.get(view.getId());
            ImageView sameple = (ImageView)hasID.get(isample);

        Logger.debug(TAG, "----------------------");
        Logger.debug(TAG, listFruit.size()+"--->list size");
        Logger.debug(TAG, img.getId() + "--->id");
        Logger.debug(TAG, sameple.getId() +"--->sample id");
        Logger.debug(TAG, hasName.get(img.getId()) +"--->name");
        Logger.debug(TAG, hasName.get(sameple.getId())+ "---> sample name");
        Logger.debug(TAG, "----------------------");
        if(view.getId() == isample){
            String oldScore = this.score.getText().toString();
            int newscore = Integer.valueOf(oldScore) + 1;
            this.score.setText(String.valueOf(newscore));
            boolean b = listFruit.remove(sameple);
            Logger.debug(TAG, "result:"+b);
            scorrect.start();
            change_number_click(0);
            img.setVisibility(View.INVISIBLE);
            randomSample();
        }else{//click wrong
            swrong.start();
        }
    }

public void randomSample(){
    isample = random.nextInt(listFruit.size());
    for(int i = 0;i<listFruit.size();i++){
        int id = getApplicationContext().getResources().getIdentifier(fruit.get(i).file, "drawable",
                getApplicationContext().getPackageName());
        Logger.debug(TAG, hasName.get(id)+"-->name");
    }
    Logger.debug(TAG, "------create sample------");
    Logger.debug(TAG, listFruit.size()+"-->list size");
    Logger.debug(TAG, isample+"-->sample");
    int id = getApplicationContext().getResources().getIdentifier(fruit.get(isample).file, "drawable",
            getApplicationContext().getPackageName());
    Logger.debug(TAG, id +"-->id sample");
    Logger.debug(TAG, hasName.get(id)+"-->name");
    this.sample.setImageResource(id);
    this.sample.setVisibility(View.VISIBLE);
    isample = id;
    Logger.debug(TAG, "-------------------------");
}

这是控制台上的结果:

07-20 02:33:50.694    annona-->name
07-20 02:33:50.694    apple-->name
07-20 02:33:50.694    banana-->name
07-20 02:33:50.694    ------create sample------
07-20 02:33:50.694    3-->list size
07-20 02:33:50.694    0-->sample
07-20 02:33:50.694    2130837563-->id sample
07-20 02:33:50.694    annona-->name
07-20 02:33:50.694    -------------------------
07-20 02:33:50.694    2130837563-->sample
07-20 02:33:50.786    Tick...
07-20 02:33:51.794    Tick...
07-20 02:33:52.694    2130837563-->ID
07-20 02:33:52.694    ----------------------
07-20 02:33:52.694    3--->list size
07-20 02:33:52.694    2130837563--->id
07-20 02:33:52.694    2130837563--->sample id
07-20 02:33:52.694    annona--->name
07-20 02:33:52.694    annona---> sample name
07-20 02:33:52.694    ----------------------
07-20 02:33:52.694    result:true
07-20 02:33:52.698    annona-->name
07-20 02:33:52.698    apple-->name
07-20 02:33:52.698    ------create sample------
07-20 02:33:52.698    2-->list size
07-20 02:33:52.698    0-->sample
07-20 02:33:52.698    2130837563-->id sample
07-20 02:33:52.698    annona-->name
07-20 02:33:52.698    -------------------------

如你所见。 listFruit是Arraylist

List<ImageView> listFruit = new ArrayList<>();

这是列表值

07-20 02:33:50.694    annona-->name
07-20 02:33:50.694    apple-->name
07-20 02:33:50.694    banana-->name

首先,它创建一个示例对象

07-20 02:33:50.694    ------create sample------
07-20 02:33:50.694    3-->list size
07-20 02:33:50.694    0-->sample
07-20 02:33:50.694    2130837563-->id sample
07-20 02:33:50.694    annona-->name

annona ,在 onClick 函数中,如果用户点击示例对象,我会删除

//hasID is hashtable, with key is id, and value is object in listFruit
ImageView sameple = (ImageView)hasID.get(isample);

返回 true 但我发现它总是删除listFruit ArrayList

中的最后一项
07-20 02:33:52.694    result:true
07-20 02:33:52.698    annona-->name
07-20 02:33:52.698    apple-->name

实际上,它必须在第一项删除annona,但不是。怎么了?

更多信息*

我将代码更改为

  Logger.debug(TAG, "----------------\n");
                Logger.debug(TAG, "Before remove:");
                for(int i = 0;i<listFruit.size();i++){
                    int id = getApplicationContext().getResources().getIdentifier(fruit.get(i).file, "drawable",
                            getApplicationContext().getPackageName());
                    Logger.debug(TAG, hasName.get(id)+"-->name");
                }
                Logger.debug(TAG, "remove ID:"+ksample);
                listFruit.remove(ksample);
                Logger.debug(TAG, "After remove:");
                for(int i = 0;i<listFruit.size();i++){
                    int id = getApplicationContext().getResources().getIdentifier(fruit.get(i).file, "drawable",
                            getApplicationContext().getPackageName());
                    Logger.debug(TAG, hasName.get(id)+"-->name");
                }
                Logger.debug(TAG,"------------------\n");

我通过索引删除,结果是:

07-20 03:05:59.694     Before remove:
07-20 03:05:59.694     annona-->name
07-20 03:05:59.694     apple-->name
07-20 03:05:59.694     remove ID:0
07-20 03:05:59.694     After remove:
07-20 03:05:59.694     annona-->name

我在 0 - annona 的位置删除了,但它仍然删除了最后一项:(

1 个答案:

答案 0 :(得分:1)

当您使用此代码打印列表内容时,您正在阅读fruit列表而不是listFruit列表,只能使用listFruit来获取大小。< / p>

for(int i = 0;i<listFruit.size();i++){
        int id = getApplicationContext().getResources().getIdentifier(fruit.get(i).file, "drawable",
                getApplicationContext().getPackageName());
        Logger.debug(TAG, hasName.get(id)+"-->name");
    }

由于您不会删除fruit中的任何项目,因此它不会更改,因此当您将其打印出来时,它只打印少一项(因为listFruit现在是一个项目较小)。

要解决此问题,您需要确保fruit中的项目与listFruit的顺序相同,并在删除项目的同时从fruit删除项目来自listFruit