| | |
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: 1,027
Reputation:
Solved Threads: 151
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
Views: 625 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for Java
actuate android api apple applet application arguments array arrays automation balls binary bluetooth business c++ chat class classes client code codesnippet collections component database defaultmethod dragging draw ebook eclipse error event exception file fractal froglogic game givemetehcodez graphics gui helpwithhomework hql html ide image input integer intersect invokingapacheantprogrammatically ip j2me java javaprojects jmf jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie number numbers object oracle parameter php print problem program programming project recursion scanner screen server set size sms socket sort sql string sun swing swt tcp test threads time transfer tree udp windows






