FilenameFilter() question?


Could any advise, in this example if I wanted store the wav file names in the array wavList, but add them to the string[] so the wav names start at position 1.

Would I need to copy wav names into the existing string[] and then copy them in a for loop into another string[] starting at 1?


public class WavFiles {

     String [] wavList = null;
     public WavFiles ()
          File dir = new File (".");
          FilenameFilter filter = new FilenameFilter ()
               public boolean accept(File dir, String name) {
                    return (name.toLowerCase().endsWith(".wav"));
          wavList = dir.list(filter);
          for (int i = 0; i < wavList.length; i++)
               System.out.println (wavList[i] + "\n");
     public static void main(String[] args)
          WavFiles wav = new WavFiles();
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You could do this:

String newWavList = new String[wavList.length + 1];
System.arraycopy(wavList, 0, newWavList, 1, wavList.length);

that's the fastest way You can do it

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AntoniRyszardAuthor Commented:
So you think it would not be possible to remove the wavList = dir.list(filter); line and change this into a for loop. Which could start at position 1?

>>so the wav names start at position 1.

Why would you want to do that? If you want to identify them with a 1-based index you can do

String file = array[i - 1]

zzynxSr. Software engineerCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.