What does that mean?Naziru Gelajo wrote:. . . by default any object (which is what a String is) that is blank has a null value. . . .
I think he meant to say or better say-every reference variable of type Object (or say String(IS-A Object)) if not initialized,is a null reference by default.Campbell Ritchie wrote:...
What does that mean?
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
Yes. Got it.praveen kumaar wrote:. . .
output:
. . . .
khadar valli wrote:Hi Naziru,
"Let's say i add an element called Juice 3 times, It comes up with juice just once and null 24 times....What could I be doing wrong? Thanks! "
PFB the method you had created for adding the flowers to the array.
Here you are initializing the index to zero. Whenever you call this method to add the element in this array, this method will replace the element which is available at that index.
So create such a method which adds the elements to the array based on its size.
Regards,
Khadar.
[edit]Remove incorrect color tags.
Since arrays are not dynamic, where to next?
Norm Radder wrote:
Since arrays are not dynamic, where to next?
See the ArrayList class. It is dynamic.
Norm Radder wrote:Are you trying to design code that will "extend" an array when the array gets full?
Since arrays are not dynamic, where to next?
But that is exactly what you have to do. Start adding at position 0 and increment the position every time something is added. A bit of thought should lead you to the solution.Naziru Gelajo wrote:. . . I just don't see how this can be done (adding to an array that is) at position starting at index 0 and incrementing by one each time an item is added. . . .
Campbell Ritchie wrote:
But that is exactly what you have to do. Start adding at position 0 and increment the position every time something is added. A bit of thought should lead you to the solution.Naziru Gelajo wrote:. . . I just don't see how this can be done (adding to an array that is) at position starting at index 0 and incrementing by one each time an item is added. . . .
it only displays the element at index 0.
Norm Radder wrote:
it only displays the element at index 0.
Do you mean it only displays one element of the array
or do you mean that only the first element in the array has a value and the other elements are all null?
If the second case is what is happening, look at how the index used for adding elements to the array is kept safe (not reset to zero every time it is used) and incremented after each element is added.
it only displays one element within the array
Norm Radder wrote:
it only displays one element within the array
Strange. How are you displaying the contents of the array? Try using the Arrays class's toString() method. It will show all the elements in the array:
look at how the index used for adding elements to the array is kept safe (not reset to zero every time it is used) and incremented after each element is added.
Norm Radder wrote:From an earlier post:
look at how the index used for adding elements to the array is kept safe (not reset to zero every time it is used) and incremented after each element is added.
Did you check that the index is being incremented every time an element is added and that the index is not being reset to zero between adds?
Norm Radder wrote:If the values of the flowerPack array can be null, then dereferencing an element of the array to call the equals() method will throw a NPE when the value is null
There are a couple of ways to handle that:
1) reverse the order of the variables: flowerType.equals(flowerPack[i])
2) test for null before calling equals: flowerPack[i] != null && flowerPack[i].equals(flowerType
Why does something so simple as reversing the order of the variables work?
Norm Radder wrote:
Why does something so simple as reversing the order of the variables work?
It works when the variable used to call the method is sure not to be null. The argument for equals() can be null.
Consider Paul's rocket mass heater. |