java while 循环中隐式变量的名称是什么?

发布于 2024-12-05 08:54:26 字数 264 浏览 0 评论 0原文

在 Perl 中,您可以编写类似的内容

while(!eof){  
    doSomething(x,y);  
    print $_;  
}

,第二条语句使用 $_ 打印出循环所进行的迭代,这是 Perl 用于迭代循环的不可见变量的名称。 Java 中该变量的名称是什么?

我意识到您可以通过在循环外部声明一个变量并递增它来完成同样的事情,但如果有人知道如何做到这一点,这种方式肯定会更优雅。

In Perl you can write something like

while(!eof){  
    doSomething(x,y);  
    print $_;  
}

and the second statement prints out the iteration the loop is up to using $_, the name of the invisible variable Perl uses to iterate through the loop. What is the name of that variable in Java?

I realise that you can accomplish the same thing by declaring a variable outside the loop and incrementing it, but this way is definitely more elegant if anyone knows how to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初与友歌 2024-12-12 08:54:26

Java 中没有像 Perl 中那样的隐式变量。你只需要在 Java 中更加明确...

There's no implicit variable in Java as in Perl. You just have to be more explicit in Java...

凉栀 2024-12-12 08:54:26

Java中没有这个概念。

在 Groovy 中,至少您可以在函数方法上设置一个 it 变量。

myList = [1,2,3];
myList.each {
   println it
}

或读取文件,就像在您的示例中一样......

new File('test.txt').eachLine {
   println it
}

There is no such concept in Java.

In Groovy, at least you get an it variable set on the functional methods.

myList = [1,2,3];
myList.each {
   println it
}

or to read a file, like in your example...

new File('test.txt').eachLine {
   println it
}
唱一曲作罢 2024-12-12 08:54:26

你错了; perl 不会在您显示的 while 循环中隐式对 $_ 执行任何操作。

您要么考虑 for/foreach 循环,其中 $_ 是默认迭代变量(如果您未指定),要么考虑 while 的特殊情况,其中表达式是 readline 或 glob 操作,其中对 的赋值$_ 已为您添加。

You are mistaken; perl does not implicitly do anything with $_ in the while loop you show.

You are either thinking of for/foreach loops, where $_ is the default iteration variable if you do not specify one, or of the special case of a while with the expression being a readline or glob operation, where an assignment to $_ is added for you.

遗心遗梦遗幸福 2024-12-12 08:54:26

你甚至可以更加含蓄

foreach (<LINE>){  
    doSomething(x,y);  
    print;   #If you don't specify what to print, it prints "$_"
}

然而,曾经可以接受的东西现在已经不再可以接受了。这很像在代码中不使用 use strictuse warnings 。不使用这些编译指示可以让您做很多有趣的事情。您可能不打算做的事情以及使您的代码很难理解的事情。

几乎所有 Perl 程序员都不再使用隐式的 $_@_ 因为它只会让你的代码更难理解并且可能导致错误(比如假设while 还使用隐式 $_ ;-) )。

Java(和大多数语言)没有这样的默认变量,因为这并不是一个好主意。像 CPAN 和扩展正则表达式这样的 Perl 优秀思想现在已经被复制到其他语言中。像默认变量这样的东西还没有。

使用这些东西会很有趣——就像一个秘密解码器环,让您知道谁是真正的 Perl 黑客,谁不是。通读所有混乱的 Perl 代码并看看谁真正理解 Perl 的内部工作原理是很有趣的。

然而,它也为其他语言的开发人员提供了他们所需的证据,证明 Perl 极其草率且无法理解。结果,Perl 的接受度受到了影响。

you can even be more implicit:

foreach (<LINE>){  
    doSomething(x,y);  
    print;   #If you don't specify what to print, it prints "$_"
}

However, what was once acceptable is no longer acceptable. It's much like not using use strict and use warnings in your code. Not using these pragmas allowed you to do so many interesting things. Things that you might not have intended to do and things that make your code really hard to understand.

Almost all Perl programmers no longer use the implicit $_ and @_ because it's just makes your code all that more difficult to understand and can be a cause errors (like assuming that while also uses an implicit $_ ;-) ).

Java (and most languages) don't have such a default variable because it's not really all that good an idea. Good Perl ideas like CPAN and expanded regular expressions have now been copied in other languages. Things like default variables have not.

Using this stuff can be fun -- like a secret decoder ring that allowed you to know who's a real Perl hacker and who isn't. It's fun to read through all that obfuscated Perl code and see who really understands the inner workings of Perl.

However, it has also given developers in other languages the proof they need that Perl was hopelessly sloppy and impossible to understand. And, as a result, Perl has suffered in general acceptance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文