aspose file tools*
The moose likes Beginning Java and the fly likes Why can't arrays be populated on the fly to an undefined amount Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why can Watch "Why can New topic
Author

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

Ben Synes
Ranch Hand

Joined: Jul 18, 2012
Posts: 47
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
Bartender

Joined: Oct 14, 2005
Posts: 18154
    
    8

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

Joined: Jul 18, 2012
Posts: 47
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

Joined: Jan 10, 2002
Posts: 60077
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2065
    
  22

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

Joined: Mar 20, 2001
Posts: 210

You can use java.util.Arrays.fill() methods.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
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

Joined: Aug 07, 2007
Posts: 1950
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can't arrays be populated on the fly to an undefined amount
 
Similar Threads
an array and two loops walk into a bar
can anyone explain how this is answer?
Strange system.out
Array Problem
array?