简体   繁体   English

需要帮助来解释继承代码中的for语句

[英]Need assistance interpreting a for statement in inherited code

Consider for(File file : files ) in the following code. 在下面的代码中考虑for(File file : files ) I've never seen this syntax before. 我以前从未见过这种语法。 I understand from context and behavior that it'a a for loop based on the number of records...similar to (for x=0;x<length.foo;++x) . 我从上下文和行为中了解到,这是一个基于记录数的for循环...类似于(for x=0;x<length.foo;++x) Beyond that though, I'm not sure. 除此之外,我不确定。 Is this shorthand for a for loop that I've not learned yet? 这是我尚未学习的for循环的简写吗? Is this a loop specifically for objects? 这是专门针对对象的循环吗? More importantly... how do I describe it when I want to google information about that particular syntax? 更重要的是,当我想在Google上搜索有关该特定语法的信息时,该如何描述?

CLARIFICATION: a second question, I'm also curious how this method of recursive file listing stores the list of filenames in file . 澄清:第二个问题,我也很好奇这种递归文件列表方法如何在file存储文件名列表。 Is this an array? 这是数组吗? a collection? 收藏吗? or...? 要么...? I'd like to know how I need to read the resulting file. 我想知道我需要如何读取生成的文件。

public static void main(String... aArgs) throws FileNotFoundException
{
    File startingDirectory= new File("CGT");
    List<File> files = WorkingFileListing2.getFileListingNoSort(startingDirectory);
    for(File file : files )
    {
       System.out.println(file);  //print filenames
    }
}

It is the so-called "for each" loop . 这就是所谓的“ for each”循环

I'm also curious how this method stores the list of filenames in file . 我也很好奇这种方法如何在file存储文件名列表。

It doesn't. 没有。 It iterates over the elements of files , assigning them in turn to file (one at a time!). 它遍历files的元素,依次将它们分配给file (一次分配一个!)。 If you run the code, each iteration of the loop will print one filename. 如果运行代码,则循环的每次迭代将打印一个文件名。

how do I describe it when I want to google information about that particular syntax? 当我想在Google上搜索有关该特定语法的信息时,该如何描述?

Google "java for-each". Google“ java for-each”。

It looks to me like the equivalent of a 'foreach' in C#. 在我看来,它相当于C#中的“ foreach”。 Searching Google for 'Java foreach' got me this: Detail on Java For-Each loop 在Google上搜索“ Java foreach”得到了我这一点: Java For-Each循环的详细信息

这称为foreach循环 ,当您不想从数组中删除对象时,仅用于对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM