2016-11-03 31 views
1

String[] theWords = {"hello", "good bye", "tomorrow"}のような配列があるとします。私は、文字 'e'を持つ配列内のすべての文字列を削除/無視したい。それをどうやってやりますか?私の考えは次の通りです:Java:文字のために配列から項目を削除する

for (int arrPos = 0; arrPos < theWords.length; arrPos++) { //Go through the array 
    for (int charPos = 0; charPos < theWords[arrPos].length(); charPos++) { //Go through the strings in the array 
    if (!((theWords[arrPos].charAt(charPos) == 'e')) { //Finds 'e' in the strings 
     //Put the words that don't have any 'e' into a new array; 
     //This is where I'm stuck 
    } 
    } 
} 

私のロジックがうまくいくか、正しいトラックにいてもわかりません。どんな反応も役に立つでしょう。どうもありがとう。

+6

に見:1)のArrayListはなく、アレイ、および2)文字列 '...(含有)'メソッドを使用して。 –

+0

なぜ 'ArrayList'を使わないのですか?配列を使用する場合は 'contains'を使用して' character'や 'word'をチェックしてください –

+0

ArrayListをまだ学習しておらず、配列はまだ比較的新しいです。 –

答えて

0

Stringクラスのメソッドを直接使用して、文字列に "e"が含まれているかどうかを確認できます。それは余分なforループを節約します。アレイをフィルタリングする

+0

これは、OPが要素を新しい配列にどのように取得できるかに関する完全な答えではありません。 – 4castle

1

簡単な方法がため、各ループ内ifArrayListを移入することである。

List<String> noEs = new ArrayList<>(); 
for (String word : theWords) { 
    if (!word.contains("e")) { 
     noEs.add(word); 
    } 
} 

のJava 8に別の方法は、Collection#removeIfを使用することである。

List<String> noEs = new ArrayList<>(Arrays.asList(theWords)); 
noEs.removeIf(word -> word.contains("e")); 

または使用する:

String[] noEs = Arrays.stream(theWords) 
         .filter(word -> !word.contains("e")) 
         .toArray(String[]::new); 
0

シンプルArrayListを使用している場合は は、あなたが持っている問題は、あなたがあなたもあなたはいないだろうどのように多くの文字列を知ることはできませんので、(それであることを行っているどのように多くの要素を知っている前に、文字列配列を宣言し、インスタンス化する必要があるということですimport java.util.ArrayList;

ArrayList<String> theWords = new ArrayList<String>(); 
    ArrayList<String> yourNewArray = new ArrayList<String>;//Initializing you new array 
    theWords.add("hello"); 
    theWords.add("good bye"); 
    theWords.add("tommorow"); 



    for (int arrPos = 0; arrPos < theWords.size(); arrPos++) { //Go through the array 

     if(!theWords.get(arrPos).contains("e")){ 
      yourNewArray.add(theWords.get(arrPos));// Adding non-e containing string into your new array 
      } 
    } 
0

をインポートしますループを通過する前に 'e'を含む)。 代わりに、ArrayListを使用する場合は、事前に必要なサイズを知る必要はありません。私のコードは最初から最後までです。

String [] theWords = {"hello"、 "good bye"、 "tomorrow"};

//creating a new ArrayList object 
    ArrayList<String> myList = new ArrayList<String>(); 

    //adding the corresponding array contents to the list. 
    //myList and theWords point to different locations in the memory. 
    for(String str : theWords) { 
     myList.add(str); 
    } 

    //create a new list containing the items you want to remove 
    ArrayList<String> removeFromList = new ArrayList<>(); 
    for(String str : myList) { 
     if(str.contains("e")) { 
      removeFromList.add(str); 
     } 
    } 

    //now remove those items from the list 
    myList.removeAll(removeFromList); 

    //create a new Array based on the size of the list when the strings containing e is removed 
    //theWords now refers to this new Array. 
    theWords = new String[myList.size()]; 
    //convert the list to the array 
    myList.toArray(theWords); 

    //now theWords array contains only the string(s) not containing 'e' 
    System.out.println(Arrays.toString(theWords)); 
関連する問題