Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > iterator problem with List of Files

Reply
Thread Tools

iterator problem with List of Files

 
 
Alan
Guest
Posts: n/a
 
      12-23-2007
I am trying to iterate through a List<File>, but the compiler
does not like when I try to assign the next item to a File type. It
says that file and it.next() are incompatible types, but each item of
filelist is supposed to be of type File. The error occurs at the
line:

File file = it.next();

The code may be found below. Note that the method listAllFiles
returns List<File>.

What am I doing wrong? Thanks, Alan


List<File> filelist = listAllFiles(directory, "*.java");

for (Iterator it = filelist.iterator(); it.hasNext()
{
File file = it.next();
System.out.println(file.getName());
}



 
Reply With Quote
 
 
 
 
Alan
Guest
Posts: n/a
 
      12-23-2007
Never mind! A type cast fixed it:

File file = (File) it.next();

 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      12-23-2007
Alan wrote:
> I am trying to iterate through a List<File>, but the compiler
> does not like when I try to assign the next item to a File type. It
> says that file and it.next() are incompatible types, but each item of
> filelist is supposed to be of type File. The error occurs at the
> line:
>
> File file = it.next();
>
> The code may be found below. Note that the method listAllFiles
> returns List<File>.
>
> What am I doing wrong? Thanks, Alan
>
>
> List<File> filelist = listAllFiles(directory, "*.java");
>
> for (Iterator it = filelist.iterator(); it.hasNext()
> {
> File file = it.next();
> System.out.println(file.getName());
> }


More simply:

for (File file: filelist)
{
System.out.println(file.getName());
}

but if you want to do it the old way, you could indicate what the
Iterator iterates:

for (Iterator<File> it = filelist.iterator(); it.hasNext()
{
File file = it.next();
System.out.println(file.getName());
}

Patricia


 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      12-23-2007
Alan wrote:
> Never mind! A type cast fixed it:
>
> File file = (File) it.next();
>

A type cast is the wrong way to fix it...

You shouldn't use "Iterator it", you should have used
"Iterator<File> it".

Hope this helps.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List iterator assignment fails, assert iterator not dereferencable David Bilsby C++ 5 10-09-2007 02:05 PM
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments