hello friends,

I got a problem with an object array.I'll write a sample code for understand the problem.I declared an array of objects named item.

item[] itemAr = new item[100];

And then I initialized some of them

itemAr[0]=****;
itemAr[1]=****;
itemAr[2]=****;

I want to know that is there a way to find out how many array elements are initialized like this.(In this case its just 3).I tried with item.length but it returns 100 which is the size of array.

thank you all.

a way to find out how many array elements are initialized like this

No easy way. You have to look at each element and see how it is set.
Arrays are pretty dumb. If you use a collection class like an ArrayList, you can ask it how many items it has in it.

No easy way. You have to look at each element and see how it is set.
Arrays are pretty dumb. If you use a collection class like an ArrayList, you can ask it how many items it has in it.

thank you for the reply.Can you please explain something more about ArrayList..? how to create like array one using it..

how to create like array one using it

Have you read the API doc?

Or search on this forum for sample code. There are dozens of examples.

Have you read the API doc?

Or search on this forum for sample code. There are dozens of examples.

ok I'll find out.thanks

hi..
In your statement u are initialize array with
item[] itemAr = new item[100];
that is wrong u can not initialize array in that way...u can initialize as..
dataType[] itemAr = new dataType[100];
here dataype may be an int, float,double char .etc...
so if u initialize a array with 100,so that array length is 100.
no mater how many element insert in array...but number of element less then 100.
otherwise ArrayIndexOutOfBoundsException generated..

Edited 5 Years Ago by dharma117: n/a

Comments
First part is incorrect and the rest is pretty useless.

hi..
In your statement u are initialize array with
item[] itemAr = new item[100];
that is wrong u can not initialize array in that way...u can initialize as..
dataType[] itemAr = new dataType[100];
here dataype may be an int, float,double char .etc...
so if u initialize a array with 100,so that array length is 100.
no mater how many element insert in array...but number of element less then 100.
otherwise ArrayIndexOutOfBoundsException generated..

My data type is "item" , so what's wrong with that..? :-O

Edited 5 Years Ago by baby_c: n/a

Nothing. You can create an ArrayList to hold Item class objects.
One small thing, Java naming conventions for classnames is that they start with a Capital letter.

Nothing. You can create an ArrayList to hold Item class objects.
One small thing, Java naming conventions for classnames is that they start with a Capital letter.

aha... thanks for the tip :)

This question has already been answered. Start a new discussion instead.