3

文字列に次の文字列の可能性含まれていないかどうかを確認する必要があります。

MNC ブラ レブ マール RVC ウェイ GLZ WWW HYB

私の現在のコード:

 if(selectedLocation.equals("OTH"))
 {
    if(!currentClassLocation.equals("MNC") &&
                        !currentClassLocation.equals("BRA") &&
                        !currentClassLocation.equals("LEB") &&
                        !currentClassLocation.equals("MAR") &&
                        !currentClassLocation.equals("RVC") &&
                        !currentClassLocation.equals("WAY") &&
                        !currentClassLocation.equals("GLZ") &&
                        !currentClassLocation.equals("WWW") &&
                        !currentClassLocation.equals("HYB"))
                    {
                         //Awesome, I need this string! I operate on it here.
                    }
 }

要するに、for ループを使用できません。繰り返しなしで文字列にこれらのいずれも含まれていないかどうかを確認する方法はありますか?

4

8 に答える 8

9

次を使用しHashSetます。

Set<String> invalidSequences = new HashSet<String>();
invalidSequences.add("MNC");
invalidSequences.add("BRA");
invalidSequences.add("LEB");
// Remaining sequences ...

if (!invalidSequences.contains(currentClassLocation)) {
    // Awesome, I need this string...
}
于 2013-01-28T19:58:36.493 に答える
2

これらの文字列を Set に追加してから、contains を使用して検索してみてください。これは O(c) になります。

public class Filter {
   Set<String> exclusionSet = new HashSet<String>();

   public Filter( String... excludes ) {
       for( String exclusion : excludes ) {
          exclusionSet.add( exclusion );
       }
   }

   public boolean exclude( String src ) {
     return exclusionSet.contains( src );
  }


   public static void main( String[] args ) {
       Filter filter = new Filter( "MNC BRA LEB MAR RVC WAY GLZ WWW HYB".split(" ") );

       for( String arg : args ) {
           System.out.println( arg + " is excluded? " + filter.exclude( arg ) );
       }
   }
}
于 2013-01-28T20:00:22.693 に答える
1

文字列の HashSet を作成し、O(1) チェックを実行して、現在のクラスの場所が文字列のセットに存在するかどうかを確認します。

于 2013-01-28T20:00:22.573 に答える
0

forループを使用する場合は、変数を使用するだけです。

String[] data = {"BRA","LEB","MAR","RVC","WAY","GLZ","WWW","HYB"};

if(selectedLocation.equals("OTH"))
{
   boolean chk = false;
   for(int i = 0; i < data.length; i++)
      chk |= currentClassLocation.equals(data[i]);

   if(!chk){
      //Awesome, I need this string! I operate on it here.
   }

}
于 2013-01-28T20:11:46.157 に答える
0

設定するデータを手動で追加したくない場合にも使用できます。

import java.util.Arrays;


public class StringFind {
    public static void main(String[] args) {
        String stringtotest = "MNC";
        String dataString = "MNC BRA LEB MAR RVC WAY GLZ WWW HYB";
        String[] dataArray= dataString.split(" ");
        Arrays.sort(dataArray); // You can omit this if data is already sorted.
        if(Arrays.binarySearch(dataArray, stringtotest)<0){
            System.out.println("Does not Exists");
        }else{
            System.out.println("Exists");
        }
    }
}
于 2013-01-28T20:11:48.173 に答える
0

これのバリエーションを(文字列を使用して)試してください:

Pattern pattern = Pattern.compile("(.*(AIN|BIN|CIN|Blam).*)*");
Matcher matcher = pattern.matcher(string_to_test);

ここで Java 正規表現パターンをテストします: Java Regex Tester

于 2013-01-28T20:05:31.043 に答える
0

構成ファイルを使用して、無効なシーケンスのリストを構成可能にします。これは最も臭いビット、一連の魔法の文字の組み合わせです。それをセットに移動しても、それは役に立ちません。

于 2013-01-28T20:14:22.500 に答える
0

文字列配列の使用:

String[] arr = {"MNC", "BRA", "LEB", "MAR", "RVC", "WAY", "GLZ", "WWW", "HYB"};
List list = Arrays.asList(arr);
if (!list.contains(currentClassLocation)) {
   ...
}
于 2013-01-28T20:15:01.010 に答える