2016-10-01 58 views
1

我有两个不同的ArrayList情况下,Container类型之一,String类型之一。第一个是一个国家的“禁用物品”(字符串)清单,另一个是船上的集装箱清单。船在全国各地旅行,并在集装箱内搜索被禁货物。如果容器contains被禁止的货物,该容器应该被删除/删除。搜索值的一个ArrayList在另一个ArrayList的

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

我已经在船舶类去除容器的方法:

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

我想创建一个方法inspect船上的容器。我想为每个数组使用两个for-loop,但我似乎无法正确使用它!有人可以帮我使用两个循环来搜索数组吗?此外,我认为我需要在循环中使用迭代器(特别是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(); 
} 

回答

0

我不认为你需要2循环。您应该重复禁止的货物&只需将其从容器中移除即可。

此外,假设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); 
    } 
} 

如果ContainersContainer型的,它包含容器(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); 
     } 
    } 
} 
+0

'containers'是'Container'对象,而不是字符串列表。另外'inspect'大概是'Customs'的一个实例方法,所以它可以直接访问'bannedGoods'列表。 – nbrooks

+0

OP已经写了这样的第一行:'我有两个不同的ArrayLists相同类型的字符串' –

+0

看来,第二个列表是在'容器'内。查看ship构造函数,'containers = new ArrayList ();'。 – nbrooks

0

你可以坚持你正在使用的方法。但请记住,您需要使用迭代器的remove方法或不使用迭代器。因此,要使用remove方法,无论是实施Iterable或简单地使用索引,而不是迭代器:

for (int i = 0; i < bannedGoods.size(); i++) 
{ 
    for (int j = 0; j < containers.size();) // NOTE: no j++ here 
    { 
     Container c = containers.get(j); 
     if (c.contains(bannedGoods.get(i)) 
      c.removeContainer(j); 
     else 
      j++; // only if you don't remove the container increment 
       // j - when removing the next element gets current 
       // index 
    } 
} 
0

你实际上是非常接近的,你已经做了重点的面向对象编程原理的一个好工作而设计你的课程。我认为你现在需要关注的事情是对你的类型更加小心。下面是一些建议修改你的类(未显示Container,但我猜想它有一个public boolean contains (String s)方法来检查容器是否具有一定的良好s内。

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类,你会使用Iteratorremove方法来删除冒犯Container实例:

import java.util.*; 

public class Customs { 
    private String countryName; 
    private List<String> bannedGoods = new ArrayList<String>(); 

    public Customs(String country) { 
     countryName = country; 
    } 

    public void inspect(Ship ship) { 
     for (String good : bannedGoods) { 
      for (Iterator<Container> it = ship.iterator(); it.hasNext();) { 
       Container container = it.next(); 

       if (container.contains(good) { 
        it.remove(); 
       } 
      } 
     } 
    } 
} 
相关问题