| | |
It Prints nothing from the ArrayList?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2008
Posts: 37
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.*; public class MainTwo { public static void main(String[] args) { for(File file : File.listRoots()){ search(file); } } public static void search(File f) { ArrayList<File> arrayList = new ArrayList<File>(); f.listFiles(); arrayList.add(f); System.out.println (f.getName()); } }
Why does it print nothing?
•
•
Join Date: Nov 2008
Posts: 37
Reputation:
Solved Threads: 0
i just tried a differrent approach, this code also prints out nothing
why does it print out nothing??
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.*; public class MainTwo { public static void main(String[] args) { File[] filesOnComputer = File.listRoots(); { ArrayList<File> listOfFiles = new ArrayList<File>(); for(int index = 0; index < filesOnComputer.length; index++) { listOfFiles.add(filesOnComputer[index]); System.out.println (filesOnComputer[index].getName()); } } } }
why does it print out nothing??
•
•
Join Date: May 2009
Posts: 3
Reputation:
Solved Threads: 0
i see two issues:
1) First u r calling listRoots, tht must print C:\
2) Secondly, u r not trying to get the iterate through the list of files that u got...
Let see ur code:
File[] filesOnComputer = File.listRoots();
this part shall give u the root of the directory structure (in most cases it would be C:, in case it is partioned it might also give D
then u will have to chk whether this is a directory, and if it is then u will have to call ur search function with little modification as:
public static void search(File f)
{
ArrayList arrayList = new ArrayList();
File[] files= f.listFiles(); // get all files under the direcrory
for(int index = 0; index < files.length; index++)
{
System.out.println ("here we are---"+files[index].getName());
arrayList.add(files[index].getName());
}
System.out.println (f.getName());
}
u might need to also call this recursively to see if the next selection is a file or a directory.
Try this :
public static void main(String[] args)
{
File[] filesOnComputer = File.listRoots();
{
ArrayList listOfFiles = new ArrayList();
System.out.println (filesOnComputer.length);
for(int index = 0; index < filesOnComputer.length; index++)
{
listOfFiles.add((File)filesOnComputer[index]);
if(filesOnComputer[index].isDirectory()){
System.out.println ("here we are-"+filesOnComputer[index].getPath());
search((File)listOfFiles.get(index));
}
else
System.out.println ("This is not the directory-"+filesOnComputer[index].getPath());
}
}
}
public static void search(File f)
{
ArrayList arrayList = new ArrayList();
File[] files= f.listFiles();
for(int index = 0; index < files.length; index++)
{
System.out.println ("here we are---"+files[index].getName());
arrayList.add(files[index].getName());
}
System.out.println (f.getName());
}
1) First u r calling listRoots, tht must print C:\
2) Secondly, u r not trying to get the iterate through the list of files that u got...
Let see ur code:
File[] filesOnComputer = File.listRoots();
this part shall give u the root of the directory structure (in most cases it would be C:, in case it is partioned it might also give D

then u will have to chk whether this is a directory, and if it is then u will have to call ur search function with little modification as:
public static void search(File f)
{
ArrayList arrayList = new ArrayList();
File[] files= f.listFiles(); // get all files under the direcrory
for(int index = 0; index < files.length; index++)
{
System.out.println ("here we are---"+files[index].getName());
arrayList.add(files[index].getName());
}
System.out.println (f.getName());
}
u might need to also call this recursively to see if the next selection is a file or a directory.
Try this :
public static void main(String[] args)
{
File[] filesOnComputer = File.listRoots();
{
ArrayList listOfFiles = new ArrayList();
System.out.println (filesOnComputer.length);
for(int index = 0; index < filesOnComputer.length; index++)
{
listOfFiles.add((File)filesOnComputer[index]);
if(filesOnComputer[index].isDirectory()){
System.out.println ("here we are-"+filesOnComputer[index].getPath());
search((File)listOfFiles.get(index));
}
else
System.out.println ("This is not the directory-"+filesOnComputer[index].getPath());
}
}
}
public static void search(File f)
{
ArrayList arrayList = new ArrayList();
File[] files= f.listFiles();
for(int index = 0; index < files.length; index++)
{
System.out.println ("here we are---"+files[index].getName());
arrayList.add(files[index].getName());
}
System.out.println (f.getName());
}
•
•
Join Date: Apr 2008
Posts: 997
Reputation:
Solved Threads: 147
Rather than the pre-1.5 old-style for loops and un-typed lists, replace
with
•
•
•
•
ArrayList arrayList = new ArrayList();
File[] files= f.listFiles(); // get all files under the direcrory
for(int index = 0; index < files.length; index++)
{
System.out.println ("here we are---"+files[index].getName());
arrayList.add(files[index].getName());
}
java Syntax (Toggle Plain Text)
ArrayList arrayList<File> = new ArrayList<File>(); for(File ff : f.listFiles()) { System.out.println ("here we are---"+ff.getName()); arrayList.add(ff.getName()); }
•
•
Join Date: Nov 2008
Posts: 37
Reputation:
Solved Threads: 0
i got the recursion working, couldnt quite implent the new for loop, moved on to the next step, ei. trying to compare files in two ArrayLists, and the first hiccup is as follows:
Hmm my compiler friend tells me cannot find symbol - method arrayList(Java.io.File)??
The java class library for ArrayList was not helpful to me.
Java Syntax (Toggle Plain Text)
ArrayList arrayList = new ArrayList(); ArrayList arrayListAgain = new ArrayList(); File[] files= f.listFiles(); for(int index = 0; index < files.length; index++) if (files[index].isFile()) { System.out.println ("here we are---"+files[index].getName()); arrayList.add(files[index]); arrayListAgain.add(files[index]); if (arrayList(files[index]).length() == arrayListAgain(files[index]).length() && arrayList(files[index]).hashCode() != arrayListAgain(files[index]).hashCode()) { //do something; } }
Hmm my compiler friend tells me cannot find symbol - method arrayList(Java.io.File)??
The java class library for ArrayList was not helpful to me.
•
•
Join Date: Sep 2008
Posts: 13
Reputation:
Solved Threads: 2
•
•
•
•
i got the recursion working, couldnt quite implent the new for loop, moved on to the next step, ei. trying to compare files in two ArrayLists, and the first hiccup is as follows:
Java Syntax (Toggle Plain Text)
ArrayList arrayList = new ArrayList(); ArrayList arrayListAgain = new ArrayList(); File[] files= f.listFiles(); for(int index = 0; index < files.length; index++) if (files[index].isFile()) { System.out.println ("here we are---"+files[index].getName()); arrayList.add(files[index]); arrayListAgain.add(files[index]); if (arrayList(files[index]).length() == arrayListAgain(files[index]).length() && arrayList(files[index]).hashCode() != arrayListAgain(files[index]).hashCode()) { //do something; } }
Hmm my compiler friend tells me cannot find symbol - method arrayList(Java.io.File)??
The java class library for ArrayList was not helpful to me.
Java Syntax (Toggle Plain Text)
ArrayList<File> arrayList = new ArrayList<File>(); ArrayList<File> arrayListAgain = new ArrayList<File>(); File[] files= f.listFiles(); for(int index = 0; index < files.length; index++) if (files[index].isFile()) { System.out.println ("here we are---" + files[index].getName()); arrayList.add(files[index]); arrayListAgain.add(files[index]); if (arrayList.get(index).length() == arrayListAgain.get(index).length() && arrayList.get(index).hashCode() != arrayListAgain.get(index).hashCode()) { //do something; } }
![]() |
Similar Threads
- Java Arraylist Help Please (Java)
- WritetoDisk method using ArrayList (Java)
- Using operator[] with an ArrayList (Java)
- factorizing numbers (Java)
- Storing Point2D.Double objects in an ArrayList. (Java)
- Java Programmers wanted. Need Help please (Java)
- Need Help with ArrayList sorting (Java)
- Linked arraylist (Java)
Other Threads in the Java Forum
- Previous Thread: Urgent Help!
- Next Thread: Switch Case
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arc arguments array arrays automation binary bluetooth c++ chat class classes client code codesnippet compiler component csv database doctype draw ebook eclipse error event exception fractal freeze game givemetehcodez graphics gui html ide image input integer intellij iphone j2me java java.xls javaprojects jmf jni jpanel jtable julia linux list loop loops mac map method methods mobile netbeans newbie number online oracle page parameter print problem program programming project recursion reporting rotatetext scanner screen sell server set size sms socket sort sql string superclass swing system template test testautomation threads time title tree tutorial-sample windows working





