Re: Inserting In a List
subhaba...@gmail.com wrote:
I am taking out the files in my desktop folder, as,
"Taking out" I understood initially as "deleting", but I gather you mean "printing" or
"displaying".
File folder = new File("C:\\Users\\subhabrata\\Desktop");
You can also use forward slashes.
Next, I am trying to take them out one by one as using for loop and name
of each file is converted to String,
If you indented properly your scope issues would be easier to see. The first block
doesn't have any, but the one further down does.
for( File name :folder.listFiles()){
String s1=name.toString();
System.out.println("####"+s1);
System.out.print( name );
}
I should think you'd use 'getPath()' or 'getName()' on the 'name' instance.
Till this there is no issue, now I am trying to insert name of all the files
in an array, generally it can be done, as,
ArrayList<String> myList = new ArrayList<String>();
That is not an array, that is a 'List'.
myList.add(s1);
But the problem I am facing is, if I put it as,
Again, proper indentation would reveal any scope issues.
for( File name :folder.listFiles()){
String s1=name.toString();
System.out.println("####"+s1);
System.out.print( name );
ArrayList<String> myList = new ArrayList<String>();
myList.add(s1);
}
Now here you have a scope issue. You create a new 'List' in each iteration, add only
one item to it, then throw it away. Nowhere do you create a 'List' that survives one
loop iteration.
I am getting name of files individually but I want to see them as the whole bunch like,
You show no code that demonstrates how you determine what "you" are "getting".
Please follow
http://sscce.org/
myList=["string1","string2","string3",...."StringN"]
In that case, declare your 'List' in a scope that survives the loop.
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.
Study "scope" and "access" in Java.
I am slightly new in Java so if anyone can kindly suggest how I should address it.
What is "slightly"?
--
Lew