0

In Java, consider the following piece of code:

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 '.' 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().

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. Raman
  • 211
  • 1
  • 4
  • 17
  • 1
    1. You should not use Vector (use ArrayList instead). 2. the variable `i` is not defined outside the scope of the for-loop. 3. please focus your question on what are you trying to do (not *how* you're trying to do it). – Nir Alfasi Apr 30 '18 at 16:44
  • this code, not matter how many `()` it is wrapped in is susectpible to `NullPointerException` and `ClassCastException` for the reasons in them marked duplicates which explain how to avoid them. –  Apr 30 '18 at 16:50
  • @feelingunwelcome Just because the code is susceptible to a `NullPointerException` doesn't mean that the question is a duplicate; OP seems to be curious if the syntax is safe to use. – Jacob G. Apr 30 '18 at 16:51
  • @alfasin wrote: 2. the variable i is not defined outside the scope of the for-loop: Thank you for your own response up at all. After all, that I think up that I had edited out crucial portions up of my own written code in from `(v.elementAt(i)).toString()` and `v.elementAt(i).toString()` in my own submitted first attempt in to `(v.elementAt(9)).toString()` and `v.elementAt(9).toString()`. Please try out again up! – V. Raman May 01 '18 at 06:39

1 Answers1

2

Are Java statements like v.elementAt(i).toString() containing multiple '.' 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. 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. 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()
Jacob G.
  • 28,856
  • 5
  • 62
  • 116
  • @alfasin wrote: You should not use Vector (use ArrayList instead). Why so? Are there really any benefits of using ArrayList over Vector? Anyway that, in any case that, I had only intentionally made use of Vector rather than, instead of ArrayList in order to provide with the following example. – V. Raman May 01 '18 at 06:52
  • If `v` is an `ArrayList` or `ArrayList` then there is no need to do `toString()` operation. Just we can get the elements by using `v.get(9)`. The same can be said of if `v` was a `Vector` or `Vector`. Only if the Collection `v` was a template of any given raw object data type, then the above example whatever I had mentioned enough up as above, will be appropriate enough as up. – V. Raman May 01 '18 at 06:52
  • Alternatively, if `v` is a Vector, then rather than, instead of calling `v.elementAt(9).toString()`, we could also use the same example too, if `v` were an ArrayList, a template of any given raw object data type. – V. Raman May 01 '18 at 06:52