在ArrayList中搜索另一个ArrayList

时间:2016-10-01 18:24:59

标签: java arrays arraylist

我有两个不同的ArrayList个实例,一个类型为Container,另一个类型为String。第一个是国家的“禁止货物”(字符串)清单,另一个是船上的容器清单。该船在该国旅行,并在集装箱中搜查禁止的货物。如果容器contains是禁止的货物,则应删除/删除该容器。

public Customs(String country)
{
    countryName = country;
    bannedGoods = new ArrayList<String>();
}

public Ship(String n, double weight)
{
    emptyWeight = totalWeight = weight;
    name = n;
    containers = new ArrayList<Container>();
}    

我已经在Ship类中有一个删除容器的方法:

public void removeContainer(int i) 
{
    if(i >= 0 && i < containers.size()) {
        Container r = containers.remove(i);
        totalWeight = totalWeight - r.getWeight();
    }       
}

我正在尝试为容器创建一个inspect船的方法。我想为每个数组使用两个for循环,但我似乎无法正确使用它!有人可以帮助我使用两个循环来搜索数组吗?另外,我认为我需要在循环中使用迭代器(具体为remove函数),但这对我来说也很困惑。迭代器remove方法应该替换我在类船上写过的方法吗?这就是我所拥有的:

public void inspect(Ship ship) 
{
    for (String good : bannedGoods) {
        for (String con : containers) {
            if (con.contains(good) {
                container.remove();
            }
        }
    }

这是我对迭代器的尝试:

for(String good : bannedGoods) {
    Iterator<String> it = ship.containers.iterator();
        while (it.hasNext())
            if (ship.contains(good))
                it.remove();
}

3 个答案:

答案 0 :(得分:0)

我认为你不需要2个循环。你应该重复禁止的货物&amp;只需将其从容器中取出即可。

另外,假设containers列表的类型为string,因为在第一行中提到了这一点:I have two different arrayLists of the same type String

public void inspect(Ship ship, ArrayList<String> bannedGoods){
    if (ship == null || bannedGoods == null || bannedGoods.isEmpty())
        return;
    for(String good : bannedGoods){
        ship.containers.remove(good);
    }
}

如果Containers的类型为Container且它包含可通过方法(Arraylist of string)访问的容器get_containers()列表,则以下方法可行:

public void inspect(Ship ship, ArrayList<String> bannedGoods){
    if (ship == null || bannedGoods == null || bannedGoods.isEmpty())
        return;
    for(String good : bannedGoods){
        for(Container container : ship.containers){
            container.get_containers().remove(good);
        }
    }
}

答案 1 :(得分:0)

您可以坚持使用目前正在使用的方法。但请记住,您需要使用迭代器的remove方法或不使用迭代器。因此,要使用remove方法,请实现void moveAllMaxAtEnd(list A) { int max=0; link tmp=A->first; link curr=tmp->next; int i,count=0; while(curr!=NULL){ //This first while is where I find the max item if(curr->item>=max){ max=curr->item; count++; //not used, may be cleaned up } //else{ //else is not needed the code would be executed in next loop anyway curr=curr->next; //} } link prev=A->first; link curr1=prev->next; link tmp1; while(curr1->next!=NULL){ //In this loop I am trying to put the if(curr1->item==max){ //max items at the end. prev->next=curr1->next; tmp1=curr1; // not prev->next; // prev->next=curr1->next; cur1 has not changed you re-do exactly the same // curr1->next=tmp1; curr1->next=NULL; //it will be the last element } else{ prev=prev->next; curr1=curr1->next; } } curr1->next = tmp1; //add the element we cut before } 或仅使用索引而不是迭代器:

Iterable

答案 2 :(得分:0)

你实际上非常接近,并且在设计课程时你已经很好地专注于面向对象的编程原理。我认为你现在需要关注的事情就是对你的类型更加小心。下面是对您的类的一些建议修改(Container未显示,但我假设它有一个public boolean contains (String s)方法,用于检查容器内部是否有一个好的s。 / p>

import java.util.*;

public class Ship implements Iterable<Container> {
    private double emptyWeight, totalWeight, weight;
    private String name;
    private List<Container> containers = new ArrayList<Container>();

    public Ship(String n, double weight) {
        emptyWeight = totalWeight = weight;
        name = n;
    }

    private void removeContainer(int i) {
        if (i >= 0 && i < containers.size()) {
            Container r = containers.remove(i);
            totalWeight = totalWeight - r.getWeight();
        }       
    }

    public Iterator<Container> iterator() {
        return new Iterator<Container> {
            private index = 0;
            private Container previous = null;

            public boolean hasNext() {
                return index < containers.size();
            }

            public Container next() {
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }
                previous = containers.get(index++);

                return previous;
            }

            public void remove() {
                if (previous == null) {
                    throw new IllegalStateException();
                }

                removeContainer(containers.indexOf(previous));

                previous = null;
            }
        };
    }
}

我建议将removeContainer保留在Ship课程中,因为它负责跟踪容器移除后其重量的变化情况。出于同样的原因,不允许外部类直接访问其containers列表。这样,您可以阻止其他代码在不更新weight的情况下添加或删除该列表中的值。我建议将containers列表设为私有,并公开Iterator以允许该类的用户与容器进行交互。

Customs课程中,您可以使用Iterator的{​​{1}}方法删除有问题的remove个实例:

Container