• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to find the elements present in the Array

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to find the elements which i have filled in the array.Suppose,
int A[] =new int[5];
A[0] = 1;
A[4] = 0;
now is there any way i can find out that i have initialized only 2 values.
Exact problem is that if i am iterating through the array, A[1],A[2],A[3] are also initialized to 0 and A[4] was also 0.So, i can't find out that i have filled A[4].
Kindly let me know.
 
catherine matthews
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I think the reason why you are getting all the other array elements as 0 is because by default the array is initialized to 0, since it is an Integer.
If it is a String you would get null. I you do or dont specify null it would still give you a null value. See the example below


Hope i helped you and didnt confuse you.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are saying is very correct but i want to ask if there is a way to find out the elements which i have initialized.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raj Kumar Bindal:
What you are saying is very correct but i want to ask if there is a way to find out the elements which i have initialized.

If you want to do this with an array of int, then the anwer is no. There's nothing special about the array elements that you have not initialized yourself, so there's no way to see if they have been initialized by you.

As Satou shows, you could use an array of Integer object references instead. The elements that you have not initialized will be null.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper for the explanation. Sorry for not explaining anything in my post
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your answer but it is also not the perfect answer.
I will give you an int array,suppose Array A,and i will not tell you what is there in this array ,that's all and now you have to find out which locations has been filled.
I think you people got what i want to say.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you people got what i want to say.

I think that what I've posted does it.
But if you insist on using 'int' and not 'Integer', then you can't.
Unless you define a special value meaning 'Not Set'. Like -1.

Make sure to also read Jesper's post carefully
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to you all!!
I got the answer!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic