The length of the array (which is fixed) is:
int[] someArray = {1,2,3};
int l = someArray.length;
However, by the way you asked the question, I am guessing this is not what you want. I would guess that you are "adding" items to the array and you want to know how many were added. In this case, you either keep a counter or, if you consider a null element to be empty, then you can count the number of non-null entries.
If the above is actually what you want, you should consider using an ArrayList instead.
> arr.length returns the length of the array not how
> many records are inside the array i believe. I tried
> it early and thats what it gave me.
Huh? What do you mean by "how many records are inside the array"? If you mean you assigned values at some indexes but not all, leaving the other indexes "null", and you want to know how many are non-null, you'll need to loop over the array yourself and count.
> However, by the way you asked the question, I am
> guessing this is not what you want. I would guess
> that you are "adding" items to the array and you want
> to know how many were added. In this case, you
> either keep a counter or, if you consider a null
> element to be empty, then you can count the number of
> non-null entries.
>
> If the above is actually what you want, you should
> consider using an ArrayList instead.
Yes this is what i wish to do, would an Array List be better then?
I have no idea what one is though however.
> Yes this is what i wish to do, would an Array List be
> better then?
>
> I have no idea what one is though however.
Yes, because the ArrayList .size() method returns the number of elements that have been added (and accounts for ones that have been removed). Also, ArrayLists grow dynamically so you do not need to know how many elements you will have upfront.
The API can be found here:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html
Or pre-version 5:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html
While you are at it, you should learn about all the collections. Not knowing what exactly you are trying to do, there may be others that fit your needs better.
Search Google and this site for some training material.
This one looks useful:
http://java.sun.com/docs/books/tutorial/collections/index.html
Message was edited by:
jbish
Thank for the help guys, i'm reading through the stuff trying to take it in.
What i need it for is i'm retrieve information from a database table and i'm making each row into an object, once i've done this i'm then adding these objects to another object which contains hashsets of all the objects.
In this case it's Student information which is being made into a Student object in this ArrayList and i'm then taking the information from the Array list and adding it to the Department object which has a set of all the students.
I've been working on this so long my heads just turned to mush.
Thanks again for the help.
> Thank for the help guys, i'm reading through the
> stuff trying to take it in.
>
> What i need it for is i'm retrieve information from a
> database table and i'm making each row into an
> object, once i've done this i'm then adding these
> objects to another object which contains hashsets of
> all the objects.
>
> In this case it's Student information which is being
> made into a Student object in this ArrayList and i'm
> then taking the information from the Array list and
> adding it to the Department object which has a set of
> all the students.
>
> I've been working on this so long my heads just
> turned to mush.
>
> Thanks again for the help.
You're welcome. And it sounds like you are headed (mush and all) in the right direction.