如果其他 ArrayList 中的条件为真,如何从 ArrayList 中删除项目
How to remove item from ArrayList if condition in other ArrayList is true
我有一个包含三列的 JTable,每一列都填充了一个由 ArrayList 组成的数组。我正在尝试制作一个搜索系统,用户将在其中搜索第一列中的值,并且 JTable 的行将被过滤掉,以便只有包含搜索框中指定字符串的行显示在table 按下按钮后。在另一个 table 上,这是通过过滤使用此循环使用的 ArrayList 来实现的:
String s = searchBar.getText();
ArrayList<String> fn = new ArrayList<>();
fn.addAll(names); //names is the arraylist that contains all the values that will be filtered
for(Iterator<String> it = fn.iterator(); it.hasNext(); ) {
if (!it.next().contains(s)) {
it.remove();
}
此代码用于过滤掉数组,但我想做的是仅在其中一个 ArrayList 不包含 s String 的情况下过滤 3 个 ArrayList。
我试过这样做:
String s = searchBar.getText();
ArrayList<String> fn = new ArrayList<>();
ArrayList<String> fp = new ArrayList<>();
fn.addAll(names); //names is the arraylist that contains all the values that will be filtered
fp.addAll(numbers)//one of the other arraylists that I want to filter
for(Iterator<String> it = fn.iterator(), itp = fp.iterator(); it.hasNext() && itp.hasNext(); ) {
if (!it.next().contains(s)) {
itp.remove();
it.remove();
}
当我 运行 这段代码时,我在线程 "AWT-EventQueue-0" java.lang.IllegalStateException 中的线程 "itp.remove();" 中得到了一个异常。
有没有一种方法可以仅基于其中一个从两个数组中删除?
所以我设法通过使用 ArrayList 中的 remove 方法而不是 Iterator 中的 remove 方法来修复它。我知道这不是推荐的做法,但它似乎没有带来任何负面影响,所以我暂时保留它。
我使用的代码是:
int i = 0;
for (Iterator<String> it = fn.iterator(); it.hasNext(); i++) {
if (!it.next().contains(s)) {
it.remove(); //Iterator's remove
fp.remove(i);// ArrayList's remove which avoids the error
}
}
感谢所有帮助过的人
很高兴您修复了异常。不管怎样,当我说到反向迭代时,我的意思是类似的
首先,一些检查像
if(fn.size()==fp.size()){
// and after that go to delete.
for (int i=fn.size(); i>0;i--) {
if (fn.contains(s)) {
fn.remove(i);
fp.remove(i);
} }}
无论如何,你我的方法不适合多线程,因为 ArrayList 不是并发对象,它也是 remove 方法
我有一个包含三列的 JTable,每一列都填充了一个由 ArrayList 组成的数组。我正在尝试制作一个搜索系统,用户将在其中搜索第一列中的值,并且 JTable 的行将被过滤掉,以便只有包含搜索框中指定字符串的行显示在table 按下按钮后。在另一个 table 上,这是通过过滤使用此循环使用的 ArrayList 来实现的:
String s = searchBar.getText();
ArrayList<String> fn = new ArrayList<>();
fn.addAll(names); //names is the arraylist that contains all the values that will be filtered
for(Iterator<String> it = fn.iterator(); it.hasNext(); ) {
if (!it.next().contains(s)) {
it.remove();
}
此代码用于过滤掉数组,但我想做的是仅在其中一个 ArrayList 不包含 s String 的情况下过滤 3 个 ArrayList。 我试过这样做:
String s = searchBar.getText();
ArrayList<String> fn = new ArrayList<>();
ArrayList<String> fp = new ArrayList<>();
fn.addAll(names); //names is the arraylist that contains all the values that will be filtered
fp.addAll(numbers)//one of the other arraylists that I want to filter
for(Iterator<String> it = fn.iterator(), itp = fp.iterator(); it.hasNext() && itp.hasNext(); ) {
if (!it.next().contains(s)) {
itp.remove();
it.remove();
}
当我 运行 这段代码时,我在线程 "AWT-EventQueue-0" java.lang.IllegalStateException 中的线程 "itp.remove();" 中得到了一个异常。 有没有一种方法可以仅基于其中一个从两个数组中删除?
所以我设法通过使用 ArrayList 中的 remove 方法而不是 Iterator 中的 remove 方法来修复它。我知道这不是推荐的做法,但它似乎没有带来任何负面影响,所以我暂时保留它。 我使用的代码是:
int i = 0;
for (Iterator<String> it = fn.iterator(); it.hasNext(); i++) {
if (!it.next().contains(s)) {
it.remove(); //Iterator's remove
fp.remove(i);// ArrayList's remove which avoids the error
}
}
感谢所有帮助过的人
很高兴您修复了异常。不管怎样,当我说到反向迭代时,我的意思是类似的
首先,一些检查像
if(fn.size()==fp.size()){
// and after that go to delete.
for (int i=fn.size(); i>0;i--) {
if (fn.contains(s)) {
fn.remove(i);
fp.remove(i);
} }}
无论如何,你我的方法不适合多线程,因为 ArrayList 不是并发对象,它也是 remove 方法