• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help With Understanding Arrays  RSS feed

 
Ranch Hand
Posts: 1785
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a method to increment an array by one in a loop. But I am trying to figure out how to check to see if the array is empty after the loop process so my code knows how to proceed.

If I set myarray = null will the myarray.length() return -1?

If I set myarray[0] then even if the loop does not produce a result myarray.length() will still be 0 which indicates the myarry has an element.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I set myarray = null will the myarray.length() return -1?

No. You'll get a NullPointerException.

I don't quite understand what you're trying to do, but arrays in Java have a fixed length. Once you've created the array, that's exactly how big it stays, and the length will always be the same. Maybe you could post a code snippet to clarify?

If you want a variable length array, you should be using a List instead (e.g. ArrayList).
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "increment an array by one"? Are you changing the size of the array? are you changing the values stored in an array? Are you changing WHERE various values are stored?

The only way I know of to determine if an array is empty is to scan it, looking at each element, and determine if it is empty. You have to check each one, and you can quit checking as soon as you find a non-empty slot.

If I set myarray = null will the myarray.length() return -1?

What happened when you tried it? That's the best way to tell. But the answer is that you'll get a NullPointerException (NPE). you can't call a method on a null reference.

If I set myarray[0]

If you set it to...WHAT?

Note that there is a difference between the LENGTH of the array, and what is in it. An egg carton will always have slots for 12 eggs, even if there are no eggs in it.
 
Steve Dyke
Ranch Hand
Posts: 1785
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
If I set myarray = null will the myarray.length() return -1?

No. You'll get a NullPointerException.

I don't quite understand what you're trying to do, but arrays in Java have a fixed length. Once you've created the array, that's exactly how big it stays, and the length will always be the same. Maybe you could post a code snippet to clarify?

If you want a variable length array, you should be using a List instead (e.g. ArrayList).


I am using the following code I got from the web to dynamically change my array length

 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just out of curiosity why do you use the keyword this? There isn't a variable that hides an instance variable.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, well, you're not changing the size of the array there. You're creating a completely new array and assigning that to your original array reference. But since you're making the array one bigger each time, how can it have a zero length?

And that really ought to be using a List. Lists are designed for this sort of thing - they don't need redimensioning, you just add and remove items.
 
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:Just out of curiosity why do you use the keyword this? There isn't a variable that hides an instance variable.
Because whoever wrote that used an IDE and wrote this. then ctrl-space or whatever. Then they forgot to remove the this.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!