Array, is there a way?

Is there a way with arrays to find out how many records are currently inside the array?Thanks you
[111 byte] By [Mystic15a] at [2007-11-26 12:45:43]
# 1
Arr.getLength(), maybe?
JimmyMa at 2007-7-7 16:24:55 > top of Java-index,Java Essentials,Java Programming...
# 2
> Arr.getLength(), maybe?No. You need arr.length:int lengthOfArray = arr.length;
doremifasollatidoa at 2007-7-7 16:24:55 > top of Java-index,Java Essentials,Java Programming...
# 3
I tried to use this but i can't get it to work?It always tell me method cannot be resolved.It's an Array of objects by the way if this makes any difference.Thanks for repsonding :)
Mystic15a at 2007-7-7 16:24:55 > top of Java-index,Java Essentials,Java Programming...
# 4
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.
Mystic15a at 2007-7-7 16:24:55 > top of Java-index,Java Essentials,Java Programming...
# 5

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.

jbisha at 2007-7-7 16:24:55 > top of Java-index,Java Essentials,Java Programming...
# 6

> 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.

doremifasollatidoa at 2007-7-7 16:24:55 > top of Java-index,Java Essentials,Java Programming...
# 7

> 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.

Mystic15a at 2007-7-7 16:24:55 > top of Java-index,Java Essentials,Java Programming...
# 8
http://www.google.com/search?hl=en&q=Java+%2B+ArrayListGoogle is your friend. :)Yes an ArrayList is DEFINITELY what you want to use for what you're after.PS
puckstopper31a at 2007-7-7 16:24:55 > top of Java-index,Java Essentials,Java Programming...
# 9
NeverMind, nothing to see here!~Tim
SomeoneElsea at 2007-7-7 16:24:55 > top of Java-index,Java Essentials,Java Programming...
# 10
okies thank you for that, i'll look over that and see if i can make heads and tails of it, just to check i'll be fine to store object in here? All i can see at the moment is collections.
Mystic15a at 2007-7-7 16:24:55 > top of Java-index,Java Essentials,Java Programming...
# 11

> 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

jbisha at 2007-7-7 16:24:55 > top of Java-index,Java Essentials,Java Programming...
# 12

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.

Mystic15a at 2007-7-7 16:24:55 > top of Java-index,Java Essentials,Java Programming...
# 13

> 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.

jbisha at 2007-7-7 16:24:55 > top of Java-index,Java Essentials,Java Programming...