File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to find the elements present in the Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to find the elements present in the Array" Watch "How to find the elements present in the Array" New topic
Author

How to find the elements present in the Array

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 417
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

Joined: Jan 22, 2006
Posts: 78
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

Joined: Apr 15, 2006
Posts: 417
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

Joined: Nov 24, 2005
Posts: 14687
    
  16



[My Blog]
All roads lead to JavaRanch
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Thanks Jesper for the explanation. Sorry for not explaining anything in my post
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 417
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Apr 15, 2006
Posts: 417
thanks to you all!!
I got the answer!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find the elements present in the Array
 
Similar Threads
Marcus exam 3 # 26
Array Definition Question
Arranging members of array in a ascending order?
String Input and Bubble Sort
Array Declaration