Digitalwolf
Digitalwolf

Reputation: 493

Removing an item from an arrayList in java

I have a program that takes input from a file, saves each word in the file as a token and then adds each token to an array list.

The problem is that the arrayList comes up as for example ["cat","dog"," "," ","bird"], and I don't want the spaces in the arrayList.

And the file that is read is set up as shown below:

cat dog


bird

It is obvious that it is the blank lines cause the spaces, but the blank lines are necessary.

Anyway, my code is below:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class NewMain{

public static void main(String[] args){

    try{
        FileInputStream fstream = new FileInputStream("Filename");

        //Get the object of datainputstream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;

        List<String> listOfWords = new ArrayList<String>();
       while((strLine = br.readLine()) != null){
        String [] tokens = strLine.split("\\s+");
        String [] words = tokens;
        for(String word : words){
            listOfWords.add(word);
            System.out.print(word);
            System.out.print(" ");      
        } 
        System.out.print("\n");
    }
       System.out.println(listOfWords);

        List<String> space = new ArrayList<String>();
        String[] spaces = {" "};
        space.addAll(Arrays.asList(spaces));

        editList(listOfWords,space);

        System.out.println(listOfWords);
in.close();
    }
    catch(Exception e){
        System.err.println("Error: " + e.getMessage());    
    }  
}

public static void editList(Collection<String> list1, Collection<String> list2){
    Iterator<String> it = list1.iterator();
        while(it.hasNext()){         
       if(list2.contains(it.next())) {
                it.remove();
            }  
       }
}
} 

The String[] spaces = {" "}; should remove the blank spaces, as I have tested it by removing spaces from an non-file arrayList. And the strange thing is that if I change it to String[] spaces = {"cat"}; it will remove cat from the arrayList.

Upvotes: 0

Views: 3032

Answers (3)

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

Reputation: 77454

Try removing empty strings - since you split via the whitespace pattern \s+, you will not have " " in your list, but "":

String[] spaces = {""};

But instead of removing them afterwards, don't add them in the first place!

if (word.length() == 0) continue;
listOfWords.add(word);

(and add any similar filters you need!)

This is not just simpler. It is also much more efficient. Removing an element from an array list costs O(n). So the complexity of the code you used for filtering is O(n^2) (you could get this down to O(n) by copying into a second list). Not adding the elements in the first place is essentially for free; your parsing will even become a bit faster this way - still in O(n), but faster than filter in a second step.

Upvotes: 2

Priyank Doshi
Priyank Doshi

Reputation: 13151

in your for loop add an if condition:

for(String word : words){
            if(!word.equals(""))  /* OR if( (word.length > 0) )*/  {
            listOfWords.add(word);
            System.out.print(word);
            System.out.print(" ");   
           }   
        } 

Upvotes: 2

Sayo Oladeji
Sayo Oladeji

Reputation: 741

The reason is quite obvious. A possible solution is to use this:

strLine = br.readLine().trim()

then implement your while loop as:

while (strLine != null && !strLine.isEmpty()) { //do stuff }

Upvotes: 3

Related Questions