我有一个测验程序,但是其中的一小部分没有工作。基本上,我有一个
无论如何,当我想使用
(关键搜索是另一个类,它接受一个字符串(关键字),并在测验数组列表中搜索它。它返回一个不包含关键字的问题列表,因此让我从整体问题数组中删除它们。)
输出结果如下:
ArrayList<Object>
,它保存着10个(或更多,不重要)问题对象,我可以在以后参考它们。无论如何,当我想使用
ArrayList.remove(index)
时,它并没有删除那个对象!我真的不知道该怎么办。(关键搜索是另一个类,它接受一个字符串(关键字),并在测验数组列表中搜索它。它返回一个不包含关键字的问题列表,因此让我从整体问题数组中删除它们。)
ArrayList<Integer> indexAt = new ArrayList();
indexAt = keyWord.indexAt(); //returning the questions that need to be removed
System.out.println("(1) Number of Questions: " + Questions.size()); //debugging
for(int i = 0; i < indexAt.size(); i++)
{
Questions.remove(indexAt.get(i));
}
//prints out the questions that were removed (for my own reasoning)
for(int i = 0; i < indexAt.size(); i++)
{
System.out.println(indexAt.get(i));
}
System.out.println("(2) Number of Questions: " + Questions.size()); //debugging
Sorting sort = new Sorting(Questions);
输出结果如下:
(1) 问题数量:10
1
2
3
4
6
(2) 问题数量:10
应该已经删除了索引1、2、3、4、6!!
remove
使用整数,因为对象将作为键而不是索引进行删除(可能找不到键)。 - CosmicGiantQuestions
没有遵循Java的变量命名规范。 - CosmicGiant