• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why can't arrays be populated on the fly to an undefined amount

 
Ben Synes
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java arrays hmmmmmm.

Suppose you want to have an array without a specified element limit, then just use a while loop to fill it with integers until the while loop condition is reached. Why is not so straightforward in java, whereas in Ruby its just so easy to do.



But this is limiting the array to 100 elements, is there no workaround when you specify an array like so:



And then use a while loop to fill it???

I just get array out of bounds exception 0 etc etc...

 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because that's how arrays are defined in Java. At a minimum you have to know how many entries the array is going to contain before you create it. Typically if you want an open-ended array-like data structure then you use an ArrayList.

There's really not much point in griping about how some language construct doesn't work the way you would like it to work. I mean, sure, griping is a natural reaction but once you're done with that, the thing to do is to get on with it and accept the language the way it is. At this point in time the people who control the Java language aren't going to change it, to quote an irritating phrase my ex-boss used to use all the time, "It is what it is."

 
Ben Synes
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey I agree, its not than Im griping just curious as to what would be the difference for this kind of declaration which doesn't have a predefined size, but is valid. And would on the surface at least, appear to be populated dynamically.



It is, what it is, I'll bear that in mind as I progress onwards.

Thank you.

One last thing, so this declaration would specify an array, but with zero size, meaning it cannot be populated at all? The curly brackets being empty....

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Synes wrote:Hey I agree, its not than Im griping just curious as to what would be the different for this kind of declaration which doesn't have a predefined size, but is valid.


It's not different. You are not creating the array there. numArray is just a reference to an array. The array is created by the split() method, and I can assure you that when it is created, fixed number of entries is declared.
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I won't claim to know Ruby, but from what I understand "Arrays" in Ruby are really Lists. They are not arrays at all. The term Array in terms of how data structures are defined carry a specific meaning. An array is a data structure made up of multiple values, and the values are stored right next to each other in memory. Whereas a List is a data structure made up of multiple values, where each value tells you where the next value is stored. In a List, the values are not stored consecutively. They might be stored anywhere in memory.

This is why it's faster to iterate through the array. You have a location and the next value is right next to it. A List on the other hand is faster to insert into because you can just plop the value anywhere you want and update the other values to make it point to this value

Before someone comes and says Java also doesn't have arrays, let me do it myself. Non-primitive Arrays in Java are really arrays of references, not arrays of objects. The objects themselves are stored outside of the array, and the array contains a reference to the object. C/C++, OTH, stores the entire object inside the array itself. It makes it a little tricky in the C-world, because all your objects have to be the same size

Ruby is designed to be easy to use, and Lists are easier to use than Arrays. Generally, even Java developers tend to use Lists instead of Arrays, unless there is super-important performance reason to use arrays. If you are looking at the Java equivalent of a Ruby Array, you should look at the java.util.List interface
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use java.util.Arrays.fill() methods.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Synes wrote:. . . is there no workaround when you specify an array like so:



And then use a while loop to fill it???

I just get array out of bounds exception 0 etc etc...

That is perfectly valid Java code, with a perfectly valid result: a zero‑length array. The reason you suffered the Exception is that you used the wrong sort of loop. You should iterate an array with a for‑each loop, (but then it is read‑only) or a for loop written like this:That works correctly for every conceivable length array. Including zero.
 
Tony Docherty
Bartender
Posts: 2971
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One last thing, so this declaration would specify an array, but with zero size, meaning it cannot be populated at all?

Zero size arrays are useful.
For example: If you have a method that is declared to return an array of values but for the current data there are no values to return then you have 3 choices: throw an exception, return null or return a zero length array. Ignoring the exception option (because that is only for when things have gone wrong) then returning a zero length array is preferable to returning null as your code (assuming it is written correctly) does not need to contain special handling conditions to continue to work, whereas if you return a null you have to check for the null value to prevent a NullPointerExcpetion being thrown when you try to use the returned reference.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic