2016-11-03 4 views
1

Nehmen wir an, Sie haben ein Array wie dieses: String[] theWords = {"hello", "good bye", "tomorrow"}. Ich möchte alle Zeichenfolgen im Array entfernen oder ignorieren, die den Buchstaben "e" haben. Wie würde ich das machen? Mein Denken ist zu gehen:Java: Entfernen des Elements vom Array wegen des Zeichens

Ich bin mir nicht sicher, ob meine Logik funktioniert und ob ich sogar auf dem richtigen Weg bin. Jede Antwort wäre hilfreich. Danke vielmals.

+6

Blick in: 1) unter Verwendung von Arraylisten, nicht-Arrays, und 2) die String 'enthält (...)' Verfahren. –

+0

Warum verwenden Sie 'ArrayList' nicht? Wenn es Ihre Aufgabe ist, Arrays zu verwenden, dann sollten Sie 'contains' verwenden, um nach' character' oder 'word' zu suchen. –

+0

Ich habe ArrayList noch nicht in der Klasse gelernt und Array ist für mich noch relativ neu. –

Antwort

0

Sie können direkt Methode der String-Klasse verwenden, um zu überprüfen, ob "e" in Ihrer Zeichenfolge vorhanden ist. Das wird deine extra for-Schleife speichern.

+0

Dies ist keine vollständige Antwort dafür, wie das OP die Elemente in ein neues Array bringen kann. – 4castle

1

Ein einfacher Weg, um ein Array-Filter ist ein ArrayList mit if in einer für-jede Schleife zu füllen:

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

Eine andere Möglichkeit, in Java 8 Collection#removeIf zu verwenden:

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

Or Verwenden Sie :

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

Es wäre einfach le, wenn Sie ArrayList verwenden. 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

Das Problem Sie haben den Import ist, dass Sie die String-Array deklarieren und instanziieren müssen, bevor Sie selbst wissen, wie viele Elemente darin sein werden (da Sie nicht, wie viele Strings wissen würde nicht enthalten Sie "e", bevor Sie die Schleife durchlaufen. Wenn Sie eine ArrayList verwenden, müssen Sie die erforderliche Größe nicht kennen. Hier ist mein Code von Anfang bis Ende.

Zeichenfolge [] theWords = {"Hallo", "Auf Wiedersehen", "Morgen"};

//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)); 
Verwandte Themen