Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

List to Array conversion Type error (utterly confused)

807601Mar 23 2008 — edited Mar 23 2008
Hi all, this is my first post and as such I am new to the java language. I have googled my query to death and found nothing.

I have a list and an array declared by
List fileList = new ArrayList();
public String [] filePaths, fTypes = null;
then a recursive function populates the list with Strings
	private void getFileList(File root, List result) {
		if(root.isDirectory()) {
			File [] child = root.listFiles(filter);
		    if(child!=null && child.length>0) {
		    	for(int i=0; i<child.length; i++)
		    		getFileList(child, result);
}
} else {
result.add(root);
}
}
the problem is when I then try to convert the list into an array I have tried 
filePaths = fileList.toArray(new String[fileList.size()]);
, 
filePaths = (String[])fileList.toArray(new String[fileList.size()]);
 and
filePaths = fileList.toArray();
and I either get cannot convert String[] to Object[] or Exception in thread "main" java.lang.ArrayStoreException
	at java.lang.System.arraycopy(Native Method)
	at java.util.ArrayList.toArray(ArrayList.java:306)
	at search.<init>(search.java:11)
	at sand.main(sand.java:4)


can anybody tell me what I am doing wrong?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 20 2008
Added on Mar 23 2008
2 comments
643 views