iterator problem with List of Files

Discussion in 'Java' started by Alan, Dec 23, 2007.

  1. Alan

    Alan Guest

    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());
    }
     
    Alan, Dec 23, 2007
    #1
    1. Advertising

  2. Alan

    Alan Guest

    Never mind! A type cast fixed it:

    File file = (File) it.next();
     
    Alan, Dec 23, 2007
    #2
    1. Advertising

  3. 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
     
    Patricia Shanahan, Dec 23, 2007
    #3
  4. Alan

    Daniel Pitts Guest

    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/>
     
    Daniel Pitts, Dec 23, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,467
  2. greg
    Replies:
    6
    Views:
    483
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    698
    Jim Langston
    Oct 30, 2005
  4. David Bilsby
    Replies:
    5
    Views:
    2,097
    David Bilsby
    Oct 9, 2007
  5. Jim Anderson

    problem with iterator (map iterator)

    Jim Anderson, Jan 10, 2014, in forum: C++
    Replies:
    3
    Views:
    159
    Luca Risolia
    Jan 13, 2014
Loading...

Share This Page