【问题标题】:ArrayList remove function has bug?ArrayList 删除函数有错误?
【发布时间】:2015-07-20 02:52:40
【问题描述】:

这是我的代码

  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");

我按 index 删除,结果如下:

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 位置删除了,但它仍然删除了最后一项:(

【问题讨论】:

    标签: android arraylist


    【解决方案1】:

    当您使用从fruit 列表而不是listFruit 列表中读取的此代码打印列表的内容时,您只使用listFruit 来获取大小。

    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 的顺序相同,并在从listFruit 中删除项目的同时从fruit 中删除一个项目。

    【讨论】:

    • 哦,这是我的愚蠢错误,我头疼好几个小时才发现哪里出了问题。
    猜你喜欢
    • 2012-06-12
    • 2018-11-15
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    相关资源
    最近更新 更多