-2

配列がありますint[] myArray = new int[] { A, B, C, D, E }; a、b、c、d、e は、前のコードから 6、4、5、1、1 の値にランダムに設定されます。

探す方法を見つける必要があります: IF 配列に 6、5、または 4 が含まれています。最初に 6、次に 5、次に 4 が必要です。配列に 6、4、1、2、 1 の場合、6 を見つけるだけで済みます。5 なしで 4 を使用することはできません。配列が 6,1,2,1,5 に設定されている場合は、6 と 5 が必要です。

配列内の「見つかった」数値 (6,5,4) を変数に入れ、それらをコンソールに出力する必要があります。

役に立つかもしれない考え、アイデア、チュートリアルはありますか?

ありがとうございました

4

1 に答える 1

1
public class Filterable<T> {

   private List<T> source

   public Filterable(List<T> aSource) { source = aSource; }

   public List<T> filter( Filter<T> aFilter ) {
       List<T> dest = new ArrayList<T>();
       for( T current : source ) {
           if( filter.contains(current) ) {
              dest.add( current );
           }
       }
       return dest;
   } 

   public static void main(String[] args) {
      String message = "V ubcr guvf vfa'g ubzrjbex orpnhfr ";
      message += "V tbg vg bss fgnpxbiresybj: ";
      message += "uggc://fgnpxbiresybj.pbz/dhrfgvbaf/21664702/wnin-frnepu-na-vagrtre-neenl-sbe-yvfg-bs-enaqbz-inyhrf";

      Filterable f1 = new Filterable( Arrays.asList( 6, 4, 1, 1, 2 ) );
      Filterable f2 = new Filterable( Arrays.asList( 6, 3, 1, 5, 4 ) );
      Filterable f3 = new Filterable( Arrays.asList( 6, 4, 1, 6, 5, 4 ) );

      List<Integer> r1 = f1.filter( new OrderedFilter( 6, 5, 4 ) );
      List<Integer> r2 = f2.filter( new OrderedFilter( 6, 5, 4 ) );
      List<Integer> r3 = f3.filter( new OrderedFilter( 6, 5, 4 ) );

      print( r1 );
      print( r2 );
      print( r3 );
      System.out.println(rot13(message));
   }

   public static void print(List<Object> r1) {
      boolean first = true;
      for( Object o : r1 ) {
         if( !first ) System.out.print(", ");
         System.out.printf( "%s", o );
         first = false;
      }
      System.out.println();
   }

   public static String rot13(String input) {
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < input.length(); i++) {
         char c = input.charAt(i);
         if       (c >= 'a' && c <= 'm') c += 13;
         else if  (c >= 'A' && c <= 'M') c += 13;
         else if  (c >= 'n' && c <= 'z') c -= 13;
         else if  (c >= 'N' && c <= 'Z') c -= 13;
         sb.append(c);
      }
      return sb.toString();
   }
}

public interface Filter<T> {
   public boolean contains(T object);
}

public OrderedFilter implements Filter<T> {
   private T[] lookForThese;
   private int currentMatch = 0;

   public OrderedFilter( T... values ) {
      lookForThese = values;
   }

   public boolean contains(T current) {
      if( currentMatch >= lookForThese.length ) return false;
      if( lookForThese[currentMatch].equals( current ) ) {
         currentMatch++;
         return true;
      }
      return false;
   }
}
于 2014-02-09T21:11:10.527 に答える