0

I am trying to filter out specific strings in an array with the following code, but it keeps crashes when I try to run it.

public String[] lister(String[] fl){
    String[] filelist = {};
    int j = 0;
    for(int i = 0; i < fl.length; i++){
        if (fl[i].contains("Incident ")){
            filelist[j] = fl[i];
            j++;
        }
    }
    return filelist;
}

I already tried some things, and it seems to be that

filelist[j] = fl[i];

is the culprit. When I run

public String[] lister(String[] fl){
    String[] filelist = {};
    int j = 0;
    for(int i = 0; i < fl.length; i++){
        if (fl[i].contains("Incident ")){
            //filelist[j] = fl[i];
            j++;
        }
    }
    filelist = fl;
    return filelist;
}

It doesn't crash, even though it runs the forloop. Obviously the result is not what I need.

I don't understand why it does this? For some reason it feels as if it doesn't see 'filelist' initialized in the forloop.

2
  • 1
    You are trying to declare an array of unknown length which is not possible. Use ArrayList instead or give your array a fixed length. Commented Jan 12, 2016 at 13:33
  • What is happening that is not expected? Commented Jan 12, 2016 at 13:34

3 Answers 3

2

Instead of array use ArrayList.

List<String> filelist = new ArrayList();
for(int i = 0; i < fl.length; i++){
    if (fl[i].contains("Incident ")){
        filelist.add(fl[i]);
    }
}
return filelist;
Sign up to request clarification or add additional context in comments.

2 Comments

@Cer If my answer was helpful, please mark it as accepted (Also consider other answers and mark the best one as accepted).
@HiI'mFrogatto fair enough :D
1

Not possible to declare an array of unknown length, arrays are constant in length. Its better to use java.util.List implementation like ArrayList or List.

Or give your array a size like

String[] filelist = new String[3];

Solution 1: Using ArrayList

public String[] lister(String[] fl){
    List<String> filelist = new ArrayList();
    for(int i = 0; i < fl.length; i++){
        if (fl[i].contains("Incident ")){
            filelist.add(fl[i]);
        }
    }
    return filelist;
}

Solution 2: Giving array a fixed length (Useful only if you know the length already)

public String[] lister(String[] fl){
    String[] filelist = new String[3]; // or any size
    for(int i = 0; i < fl.length; i++){
        if (fl[i].contains("Incident ")){
            filelist[i] = fl[i];
        }
    }
    return filelist;
}

Note: Variable j is not really required

Comments

0

That's because the length of the variable filelist is 0, as you give no values {}, so you are getting ArrayIndexOutOfBoundsException as filelist[j] will always exceed the array bounds whatever the value of j is, so you have to define it like the following:

String[] filelist = new String[fl.length];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.