我有一个数组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.我不能使用4而不是5.如果数组设置为6,1,2,1,5,那么我需要6和5.
我需要将数组中的“找到”数字(6,5,4)放入变量并将它们打印到控制台。
可能有帮助的想法,想法和教程?
谢谢
答案 0 :(得分: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;
}
}