• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Array with empty braces and without size : String[] lion = new String[] {};

 
Ranch Hand
Posts: 37
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!. Can someone tell me how does it work?:

String[] lion = new String[] {};

First of all, it does compile. But I'm not sure about if this statement is creating a new array with zero positions. Due to if i try to access index number 0, I get an IndexOutOfBoundEx.

thanks!
 
Bartender
Posts: 2343
102
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you posted:

creates an empty array. Hence accessing any location would give you an index out of bounds.
 
Pablo Napoli
Ranch Hand
Posts: 37
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.


It is fine not to delete post, in fact, it IS NOT fine to delete as it is an asset.

Potentially there are some other users who didn't realize same thing yet, so for the community (especially beginners) it is beneficial to have the threads with the questions of this kind.
 
Marshal
Posts: 65365
248
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:. . . it IS NOT fine to delete as it is an asset. . . .

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.

There is a shorter way to initialise that array; if the declaration and initialisation are written on the same line, the type may be omitted:-Remember that an array can have any length not larger than Integer.MAX_VALUE, but it is usually not possible to instantiate the largest sizes because of memory limits. So a 0‑length array is not only permissible, but sometimes very useful. Look at this possible class:-What about people like me who don't have a middle name? Or the Prince of Wales, who has about three of them? What are you going to put down for my middle name? What about null? No nulls are dangerous. What about ""? Or are you going to put all thee middle names into one String object? Those solutions isn't right either. If we change the class slightly....Since the Prince of Wales doesn't have anything near 2³¹ − 1 middle names, you can easily instantiate his middle names like this:-...and you get exactly what you want, a length 3 array. In my case a 0‑length array exactly matches what I want:-I shall let you work out why new String[3] won't work for the Prince of Wales. Also work out why I wrote .clone() in line 10. Also why you get an exception thrown if you try to access my0LengthArray[0]. Also work out how to iterate an array of any length safely without any risk of missing out elements, or exceeding the length of the array, or having any out of bouds exceptions thrown.
 
Bartender
Posts: 20980
128
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pfttt. Only 3 middle names? My great-grandfather had seven middle names. He reportedly was so disgusted that he didn't give his sons any middle names at all.

Worse yet, he named one of them "Dee", which cause the poor fellow no end of grief in the military. Officers would demand his name and when he replied "Dee", they'd bark back, "NAME, not initials, dammit!".

I believe he eventually changed it to Donald.
 
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell Ritchie, I'm new here and just started to prepare for OCA. I would like to try to answer questions, please correct me if im wrong. 1: not possible to declare the length and initialize array at the same time. 3: you will get out of bound exception - array is initialized but is empty  (no elemment on position 0). 4: iterate with  help of "foreach" or for. But why are you using .clone() in the getMiddleNames()?
 
Tim Holloway
Bartender
Posts: 20980
128
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Nick!

Setting length and iniializing. Have you tried this"
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch (again)

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.

You have misunderstood the question. I meant, if I can use new String[0] for my middle names, why couldn't Tim's great‑grandfather use new String[7] for his middle names?

3: you will get out of bound exception - array is initialized but is empty  (no elemment on position 0).

Afraid that is probably not the correct explanation why you might get an out of bounds exception. Sorry.

4: iterate . . .

Please show us some details, and explain why your solution will work. There are at least two other ways to iterate an array.

But why are you using .clone() in the getMiddleNames()?

That is for you to work out.
 
Nick Zapo
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, those are my solutions how to iterate trough array.



String[] str = new String[0] is a valid array with size 0. You can get index out of bound exception if you try to set or get a value from this array ex. a[0] = "abc".
.clone() need to read/learn about this one, advantages disadvantages and how to use it properly.

Hey Tim,
yes i tried this one:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error on token "3", delete this token


 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a compile‑time error in that code.
If you correct that error, all of those will work correctly (), except the do loop as you said, but don't write myArray.length > i;
Everybody writes i < myArray.length;, so your use of > will cause no end of confusion.
Since this is the beginner's forum, there will be people unfamiliar with the use of Streams; please explain one of your solutions with a Stream more fully.
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kindly don't “correct” errors afterr they have been pointed out to you; I have everted your post to its original state.
[edit]Not “everted” but “reverted”.
 
Nick Zapo
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, sorry. So here is the final version

 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apology accepted

Why didn't you use an array initialiser in line 8‑15? What would have happened if you had written the following? What if I moved the ++ to line 47 instead?Can you see a potential advantage of for loops over do and while? It's in your code. Can you see what I did to line 41?
Have you worked out why I used clone() yet?
 
Nick Zapo
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no particular reason that I didnt use an array initialiser.
In the for loop the updateStatement is executed after the loop execution (body). And if I don't mess around with the i variable, from the initialization block, inside the body - everything will be fine. In while and do while, in this example, I increase the value of variables inside the body. So I need to pay attention at what point I increment the variables i and j. Another thing is a scope of i and j, I initialized both with the value 0 but after execution of the loops the value is not 0 anymore.If I move the ++ to the line 47 I will increase j after the condition check and provoke out of bound exception.
Yes I read about the Object.clone(), it creates and returns a shalllow copy of the object. So in this case the copy of the array middleNames. But I'm stil not sure why I would use it in the getMiddleNames method. Why not just return this.middleNames.
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nick Zapo wrote:. . .  Another thing is a scope of i and j . . . .

Scope? Correct. In a for loop the loop index goes out of scope at the end of the loop.

If I move the ++ to the line 47 I will increase j after the condition check and provoke out of bound exception.

Use ++j rather than j++ in line 47.

. . . Why not just return this.middleNames.

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.

Would my telescoping your lines 37‑38 and 45‑46 make any difference to the semantics of the two loops?
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!