2016-11-03 47 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”。这将节省额外的循环。

+0

这不是OP如何将元素放入新数组的完整答案。 – 4castle

1

一个简单的方法来筛选的阵列是填充有if一个ArrayList在for-each循环:

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")); 

或者使用Stream#filter

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

你的问题是,你需要声明和实例String数组,你甚至不知道有多少元素要在它(因为你不知道有多少串不前在通过循环之前包含'e')。 相反,如果您使用ArrayList,则不需要事先知道所需的大小。这是我的代码从头到尾。

String [] theWords = {“hello”,“goodbye”,“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));