ID"); -6ren">
gpt4 book ai didi

android - ArrayList remove函数有bug?

转载 作者:行者123 更新时间:2023-11-30 02:00:49 25 4
gpt4 key购买 nike

这是我的代码

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

它返回但我看到它总是删除 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 位置删除了,但它仍然删除了最后一项 :(

最佳答案

当您使用这段代码打印列表的内容时,您正在从 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 的顺序相同,并从 fruit 中删除一个项目> 同时从 listFruit 中删除一个项目。

关于android - ArrayList remove函数有bug?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31508210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com