简体   繁体   English

Java /包含多个“。”的语句 经营者

[英]Java / Statements containing multiple '.' operators

In Java, consider the following piece of code: 在Java中,请考虑以下代码:

Vector v = new Vector();
for(int i = 1; i <= 10; i++)
{
    v.addElement(Integer.toString(i));
}
System.out.println(v.elementAt(9).toString());

Are Java statements like v.elementAt(9).toString() containing multiple '.' v.elementAt(9).toString()类的Java语句是否包含多个“。” operators perfectly fine enough at all or do they cause any given type of conflict during some given period of time after all? 操作员完全足够好,还是在一定时间内会导致任何给定类型的冲突?

So far, I have only always kept them as safe enough in parenthesis, by making use of putting away off parenthesis around individual statements, so as not to create with any given type of conflict at all, after all. 到目前为止,我只通过在单个语句周围放上括号来使它们在括号内的安全性足够高,以至于根本不会造成任何给定类型的冲突。

(v.elementAt(9)).toString() . (v.elementAt(9)).toString()

So far, I have never created with any given type of ambiguous statements like that during all of my own more than over a decade of programming and coding experience. 到目前为止,我在十多年的编程和编码经验中从未创造过任何给定类型的歧义语句。

v.elementAt(9).toString() . v.elementAt(9).toString()

Are Java statements like v.elementAt(i).toString() containing multiple '.' v.elementAt(i).toString()类的Java语句是否包含多个“。” perfectly fine enough at all or do they cause any given type of conflict during some given period of time after all? 完全足够好,还是它们在一定时间内会引起任何特定类型的冲突?

They are perfectly fine, with some caveats. 它们非常好,但有一些警告。 If v.elementAt(i) is null , then calling v.elementAt(i).toString() will throw a NullPointerException . 如果v.elementAt(i)null ,则调用v.elementAt(i).toString()将抛出NullPointerException If you select i as some value that is either negative or larger than the Vector , I suspect an ArrayIndexOutOfBoundsException will be thrown as well. 如果您选择i作为某个负值或大于Vector ,我怀疑也会抛出ArrayIndexOutOfBoundsException You can think of this syntax as the composition of functions (left-to-right), which is opposite how it is in mathematics. 您可以将这种语法视为函数的组成(从左到右),这与数学中的相反。

In your specific example, this syntax is logically equivalent: 在您的特定示例中,此语法在逻辑上是等效的:

(v.elementAt(i)).toString()

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

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