2016-10-01 3 views
1

ArrayListのインスタンスがあり、タイプがContainerで、型がStringのものがあります。 1つは国の「禁止品」(弦)のリストで、もう1つは船のコンテナのリストです。船は国を通過し、コンテナは禁止品目が検索されます。コンテナ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するメソッドを作成しようとしています。私は2つのfor-loopsをそれぞれの配列に使いたいのですが、正しくできないようです!誰かが2つのループを使って配列を検索するのを手助けできますか?さらに、ループ内でイテレータ(具体的には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); 
    } 
} 

場合は、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); 
     } 
    } 
} 
+0

'containers'は' Container'オブジェクトではなく、文字列のリストです。また 'inspect'はおそらく' Customs'のインスタンスメソッドなので、 'bannedGoods'リストに直接アクセスできます。 – nbrooks

+0

OPはこれを最初の行として書いています: '同じタイプのStringの2つの異なるarrayListを持っています ' –

+0

2番目のリストは' Container'内にあるようです。船のコンストラクタ 'containers = new ArrayList ();'を参照してください。 – nbrooks

0

あなたは現在使用している方法に固執することができます。ただし、イテレータのremoveメソッドを使用するか、イテレータを使用しないことが必要です。設計しているときは、実際にはかなり近いです

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

、あなたは、オブジェクト指向プログラミングの原則に焦点を当てたのは良い仕事をしてきた。だからあなたのremoveメソッドを使用し、いずれかIterableを実装するか、単に代わりにイテレータのインデックスを使用するにはあなたのクラス。今注目しなければならないことは、あなたのタイプにもっと注意を払っているだけだと思います。以下にいくつかはContainerが示されていないが、私はそれが容器の内部、特定の良いsを持っているかどうかをチェックするpublic boolean contains (String 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クラスの中で、あなたがContainerインスタンスを怒ら削除するIteratorremoveメソッドを使用すると思いますが:

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(); 
       } 
      } 
     } 
    } 
} 
関連する問題