3 Replies - 966 Views - Last Post: 06 April 2011 - 03:50 PM Rate Topic: -----

#1 vienhuynh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 24-April 10

How do I check if my array still have more room ?

Posted 06 April 2011 - 11:33 AM

Like I set a size, but how do i check if the array still have more room?
Is This A Good Question/Topic? 0
  • +

Replies To: How do I check if my array still have more room ?

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: How do I check if my array still have more room ?

Posted 06 April 2011 - 11:37 AM

What do you mean?
Post an example in code.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How do I check if my array still have more room ?

Posted 06 April 2011 - 02:56 PM

View Postvienhuynh, on 06 April 2011 - 01:33 PM, said:

Like I set a size, but how do i check if the array still have more room?

No way. An array is always full. Aka all the slots are occupied.
You will have to handle to use a counter by yourself to count where you are.

int[] array = new int[10];
int nbInUse = 0;

....
array[nbInUse++] = 10;
array[nbInUse++] = 20;
array[nbInUse++] = 30;

if(array.length == nbInUse) ... no more slots
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,390
  • Joined: 27-December 08

Re: How do I check if my array still have more room ?

Posted 06 April 2011 - 03:50 PM

To expand some, with Objects, all the elements are initialized to null. You can think of those slots as "empty" because they do not reference any Objects in memory. Arrays of primitive types are initialized to default values (0 for numbers and chars, and false for booleans). Those have actual values, and cannot be thought of as "empty" in the same way as with Objects. You'll still have to use a counter to keep track of the number of elements, as pbl described.

Hope this helps some. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1