Pablo Napoli wrote:Thanks! and I apologize for not knowing that it's not possible to delete a post. I could realize that when after posting I made a length on the array and i got 0.
Nor is it fine to make major changes to a post; I have reverted it to its original form. Your changes seem to have confused MS.
Liutauras Vilda wrote:. . . it IS NOT fine to delete as it is an asset. . . .
You have misunderstood the question. I meant, if I can use new String for my middle names, why couldn't Tim's great‑grandfather use new String for his middle names?
Nick Zapo wrote:. . . I would like to try to answer questions . . . 1: not possible to declare the length and initialize array at the same time.
Afraid that is probably not the correct explanation why you might get an out of bounds exception. Sorry.
3: you will get out of bound exception - array is initialized but is empty (no elemment on position 0).
Please show us some details, and explain why your solution will work. There are at least two other ways to iterate an array.
4: iterate . . .
That is for you to work out.
But why are you using .clone() in the getMiddleNames()?
Scope? Correct. In a for loop the loop index goes out of scope at the end of the loop.
Nick Zapo wrote:. . . Another thing is a scope of i and j . . . .
Use ++j rather than j++ in line 47.
If I move the ++ to the line 47 I will increase j after the condition check and provoke out of bound exception.
Only Johnny Cash would have a name like Sue. Now work out whether that assignment is or is not reflected inside the name object. Look in Effective Java by Joshua Bloch for “defensive copies” and also search the same book for the use of clone() on ordinary arrays.
. . . Why not just return this.middleNames.