• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to find the elements present in the Array

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks to you all!!
I got the answer!!
reply
    Bookmark Topic Watch Topic
  • New Topic