2016-07-14 13 views
-2

Ich muss den String-Wert wie Katze und Fisch aus der String-Variable entfernen. Ich habe einen Beispiel-Java-Code gemacht, um String-Werte zu entfernen.Entfernen Sie den String-Wert mit Java

public static void deleteAccNode(String refId){ 
    String refId [] = {"Fish","Dog","Dolphins","rowboat","Fish","sailor","Cat","cats","Cat","Fish", 
     "Fish","Tree","Fishes","Cat","Cat","CAT","Cat"}; 

     try { 
      System.out.println("GeneralLength"+refId.length); 
      for (int n = refId.length; n <= 1;){ 
      if(refId.equals("Cat")){ 
       System.out.println("cat : :"+refId); 
       How to remove the String value?remove("Cat"); 
       }else if(refId.equals("Fish")){ 
        System.out.println("Fish : :"+refId); 
        How to remove the String value?remove("Fish"); 
       } 
      n--; 
      } 
     } catch (NullPointerException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

Erforderliche Ausgabe: Hund, Delphine, rowboat, Seemann, Katzen, Baum, Fische, CAT Aber, Stromausgang: Nullpointer

+2

so ... was hält Sie an? – Stultuske

+0

Was genau ist das Problem? –

+1

'String =" Fisch "," Hund "," Delphine "," Ruderboot "," Fisch "," Matrose "," Katze "," Katzen "," Katze "," Fisch ", +" Fisch ", "Baum", "Fische", "Katze", "Katze", "KATZE", "Katze"; "So etwas habe ich noch nie zuvor gesehen. Ich denke nicht, dass es kompiliert? – ifly6

Antwort

1

Auf diese Weise nur eine ist, viel mehr gibt, könnten Sie habe das gerade gegoogelt.

String[] stringArray = { "Cat", "Fish", "CatFish", "Elephant" }; 
    final List<String> newArrayList = new ArrayList<>(); 
    for (final String string : stringArray) 
    { 
    if (!(string.equals("Cat") || string.equals("Fish"))) 
    { 
     newArrayList.add(string); 
    } 
    } 

    stringArray = (String[]) newArrayList.toArray();