JL14
JL14

Reputation: 19

How can I remove words in an array?

Given an array. How do I find all of the words in the list that are greater than three characters and add them to a new list

public class RunnerClass {
   public static int counter;
   public static int index=0;
    public static void main(String[]args){
        /*String mainArr[]= new String[5];
        mainArr[0]="Taco";
        mainArr[1]="Pizza";
        mainArr[2]="Hi";
        mainArr[3]="tryy";
        mainArr[4]="live";*/
        String[]myWords = {"Hi", "taco", "g"};
        System.out.println(removeStopWord(myWords));
        System.out.println(counter);
    }

public static String[] removeStopWord(String [] arr){
    counter=0;
    //String [] methodArr = new String[counter];
    //int index=0;

    for(String keep : arr){
        if(3<keep.length()){
            counter++;
        }
    }
    String [] methodArr = new String[counter];
    index=0;


    for (int i = 0; i < arr.length ; i++){
        if (arr[i].length() > 3){
            methodArr[index]=arr[i];
            index++;
        }
    }

    return methodArr;

}

}

Upvotes: 1

Views: 186

Answers (4)

vibs
vibs

Reputation: 11

You can filter the words using java8 stream API

List<String> lst = Stream.of(myWords).filter( s->s.length()>3).collect(Collectors.toList());

Stream.of is an API to create a data stream of array and then use filter API by passing predicate to filter the stream and collect it in collection.

Upvotes: 1

Samleo
Samleo

Reputation: 605

1. Your code is actually working perfectly fine.

Perhaps why you think your code isn't working is because of the line System.out.println(removeStopWord(myWords));, which is probably returning you something like [Ljava.lang.String;@15db9742. That's just the className + @ + the hex of the hashCode of the array, as noted in this SO post.

Just replace that line with System.out.println(Arrays.toString(removeStopWord(myWords)));, importing java.util.Arrays if necessary.

2. Alternative with ArrayList

Right now, you are looping over the array twice first to count the valid words and then to create a new array with size counter. It's actually an appropriate thing to do, if memory is more of a concern than speed (there's not a lot of speed difference anyway).

If you want something more elegant but has more memory overhead due to dynamic memory allocation:

public static ArrayList<String> removeStopWord(String [] arr){
    ArrayList<String> methodList = new ArrayList<String>();

    for (int i = 0; i < arr.length ; i++){
        if (arr[i].length() > 3){
            methodList.add(arr[i]);
        }
    }


    return methodList;
}

Print the array with System.out.println(Arrays.toString(removeStopWord(mainArr).toArray()));

Upvotes: 0

Vikas
Vikas

Reputation: 7175

You can use ArrayList and stream APIs,

List<String> list=Arrays.asList("Hi", "taco", "g");
//filer the elements with length > 3 and then collect in a list       
List<String> result=list.stream().filter(ele->(ele.length())>3).collect(Collectors.toList());

Upvotes: 0

Capn Sparrow
Capn Sparrow

Reputation: 2070

You seem like you're on the right track.

You can tidy up a bit using the String#length() method to check whether length is less than 3 or not.

You could also loop just once by adding matching Strings to an ArrayList, which will grow automatically. You can convert the ArrayList back to a String[] just before you return.

This would eliminate the need for the counter. If you want to print the number of words, just print the size of the ArrayList or String[] after it's been returned.

Upvotes: 0

Related Questions