locked
Array Index selection RRS feed

  • Question

  • I have a array of length 300 of type int. Most of the element is 0 and I want to get the index of the first element that is greater than 0. How can I achieve this.

    Kriz

    Wednesday, April 24, 2013 5:03 AM

Answers

  •  List<int> myArray = new List<int>
                                        {
                                            0,
                                            1,
                                            0
                                        };
               int index = myArray.IndexOf( myArray.First(x => x > 0));

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Wednesday, April 24, 2013 6:27 AM

All replies

  • Hi, there is no method for retreiving Index of an array for some predicate (first element > 0).

    So for this you can have multiple approaches, but most elementary approach is with loop and condition in loop.

    This is sample for that:

    int[] array = new int[300];
    //some code here for filling up the array
    
    int firstIndex = -1;
    
    for (int i = 0; i < array.Length; i++)
    {
        if (array[i] > 0)
        {
             firstIndex = i;
             break; //no neeed for loop any more
        }
    }
    
    //now you can use result in firstIndex variable, if it is -1 than item is not found for predicate inside for loop (array[i] > 0)

    If you were looking for first index of some value in array than things is much simplier because method exists for this - IndexOf - static method of Array class

    So for this scenario you can write this:

     int[] array = new int[300];
    //you should fill up array with something
    int firstIndex = Array.IndexOf(array, 1);

    Also if you are looking for last index in array for some value you have LastIndexOf method. For both methods you have also strongy typed (generic) varsion.

    Hope that it helps


    if (helpful) then Vote();

    Wednesday, April 24, 2013 5:44 AM
  • Hi,

    //assume this array list contains 300 records
    ArrayList arr = LoadData(); 
    int greaterIndex = -1;
    int arrValue = -1;
    for(int i = 0;i<arr.Length;i++)
    {
      if(int.Parse(arr[i].ToString()) > 0)
      {
         //if you want index then 
         greaterIndex = i;
    
         //if you want the value then
         arrValue = int.Parse(arr[i].ToString());
           break;
      }
    }
    Console.WriteLine(greaterIndex);
    Console.WriteLine(arrValue);


    Nagarjuna Dilip

    Wednesday, April 24, 2013 5:45 AM
  •  List<int> myArray = new List<int>
                                        {
                                            0,
                                            1,
                                            0
                                        };
               int index = myArray.IndexOf( myArray.First(x => x > 0));

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Wednesday, April 24, 2013 6:27 AM