Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin2!goblin.stu.neva.ru!aioe.org!eternal-september.org!feeder.eternal-september.org!mx05.eternal-september.org!.POSTED!not-for-mail From: Steven Simpson Newsgroups: comp.lang.java.programmer Subject: Re: Inserting In a List Date: Tue, 02 Apr 2013 13:53:00 +0100 Organization: A noiseless patient Spider Lines: 99 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: mx05.eternal-september.org; posting-host="7e194865abb1454ff7685570b1bd8709"; logging-data="10491"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19O3St0TFFcKEK/JN5s3Sf9/9i/5u4Eld0=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4 In-Reply-To: Cancel-Lock: sha1:6zVR15dFGeE005uGxYuwr+5DktU= Xref: csiph.com comp.lang.java.programmer:23182 On 02/04/13 11:11, subhabangalore@gmail.com wrote: > for( File name :folder.listFiles()){ > String s1=name.toString(); > System.out.println("####"+s1); > System.out.print( name ); > ArrayList myList = new ArrayList(); > myList.add(s1); > } (Stefan has already noted that the list must be created before the loop. What you have here is one list created per item in the folder, and each of these lists is discarded.) > I am getting name of files individually but I want to see them as the whole bunch like, > > myList=["string1","string2","string3",...."StringN"] (Arved and Roedy have mentioned some ways to get this, though I think you are after something different. Read on...) > My aim is to check the names then in another for loop and if match is not found with some defined name update the list. Your loop seems to have two purposes: 1) display all items in a certain format; 2) build an internal List object. Perhaps I've misinterpreted your aim, but your wish to generate a list of names in a string with a given format leads me to think you're under the impression that you need to display the names somehow in order to perform the check you mention. This is not necessary (nor useful, except for purposes of diagnosing problems). You should do the check on the List object, not on the formatted String. Firstly, if you must create a List, there's a quick way to create a fixed-size one from the array provided by listFiles(): List files = Arrays.asList(folder.listFiles()); Note that this is a list of File, not String; the code below does not need a List. If you really do need a List, you could do something similar with folder.list() instead of folder.listFiles(). Next, you could scan the List for a matching file. Loop over it, and record when you find it: boolean found = false; for (File file : files) { if (file.getName().equals("foo")) { found = true; break; // No need to go further. } } However, unless you need to keep the List around for other purposes, you could just use the array directly from listFiles(): boolean found = false; for (File file : folder.listFiles()) { if (file.getName().equals("foo")) { found = true; break; // No need to go further. } } But it would be even simpler, if you're just looking for a particular leafname (like "foo"), rather than a leafname matching a given pattern (like anything beginning with "foo"), to create a File object representing that name, and checking whether the file exists: File candidate = new File(folder, "foo"); boolean found = candidate.exists(); Note that, confusingly, java.io.File represents a file name, not a file, so 'new File(...)' does not actually create a file. > and if match is not found with some defined name update the list. Not sure how you intend to update the List. If it's supposed to represent the list of files in the folder, updating the List won't create the missing file. Perhaps you want the rest of the program to suppose it exists; I don't know. If you really do want to update the list, you'll have to create it as one you can resize: List files = new ArrayList(Arrays.asList(folder.listFiles())); Read this inside out: 1. listFiles(): Get the list of filenames as an array. 2. asList(): Create a 'List view' of the array. 3. new ArrayList(): Create a modifiable list with the same contents as the array. -- ss at comp dot lancs dot ac dot uk