pls a little help to a :-/guy,

In my main method am working on a collection of numbers in an array.
I want to sort the numbers efter then would like to see which index number holds the value 14.

As you can see in what i did so far sort works all right in the background, and returns index number 7 as what holda 14.

But there are two other 14 teens . my question then is how can I go about this to show the other index place numbers of other 14 teens?

this is what i did .

int[]  nums  =  {  5,  14,    3,    9,  8,  17,  14,  24,  -1,  14};    
          
          Array.Sort(nums);    
      
            //  Searching  for  all index number of 14s.    
          int  idx  =  Array.BinarySearch(nums,  14);    
      
          Console.WriteLine("Index  of  14  is  "  +  idx);
            Console.ReadLine();

if some one could help me out. My sincere thanks .

Recommended Answers

All 2 Replies

Look at Array.IndexOf and Array.LastIndexOf , which will give you the index of the first and last occurrence of the int in your array.

To fetch all the occurrences, you can use a loop as well. Something like this:

ArrayList indx = new ArrayList();
for (int i=0;i<nums.Length;i++)
{
    if (nums[i] == 14)
    indx.Add(i);
}

At the end of the loop, the ArrayList indx will hold all the indexes that hold the number 14. You can read more about ArrayList. you can do a lot more with it as compared to Array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.