locked
check null in ArrayList RRS feed

  • Question

  • hello,

    I have an ArrayList ar, with the simplified semantics: ar[i]=j means that there are j elements with value i (in some other collection).

    I instantiate it: ar = new ArrayList(1000);

     

    prob is, if I want to modify value ar[i] I first need to check whether ar[i] is null, which I do like this:

    myObjType myObj = (myObjType)ar[i]; (if myObj != null) {...}

    since I always need to check an ar's element before actually adding an element to ar, this  checking throws an ArgumentOutOfRangeException. how can I overcome this prob?

    I don't loop over i values, but take them from another collection (so processing ar does not happen in a nondecreasing order of its indices).

    thanks 





    Tuesday, March 29, 2011 4:23 PM

Answers

  • This is an example of where you would use a dictionary.

    IDictionary<T,int> count = new Dictionary<T,int>();

    where T is whichever type you're using as a key.

    Set it:

    count[i] = j; // never throws ArgumentOutOfRangeException

    Get the value:

    int j = count[i]; // throws KeyNotFoundException if i has not been added to the dictionary.

    Get the value safely:

    int value;
    
    
    
    if (count.TryGetValue(i, out value))
    
    {
    
    // key exists, no exception thrown
    
    }
    
    else
    
    {
    
    // key does not exist, no exception thrown
    
    
    
    }
    
    
    
    

    Tuesday, March 29, 2011 4:35 PM
  • An arraylist is dynamic and doesn't require a ubound be set.

    Adam


    Ctrl+Z
    • Proposed as answer by JohnGrove Tuesday, March 29, 2011 5:33 PM
    • Marked as answer by Mio_Miao Friday, April 1, 2011 11:24 AM
    Tuesday, March 29, 2011 4:47 PM
  • Also, if possible, use the List<T>. Then when checking to see if a value exists in the collections use the Contains method. 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          List<Int32> myList = new List<Int32>();
          myList.Add(32);
          myList.Add(8);
          myList.Add(1);
          myList.Add(26);
          if (myList.Contains(8))
          {
            Console.WriteLine("Contains 8");
          }
          Console.ReadLine();
        }
      }
    }
    


    John Grove, MCC - Senior Software Engineer
    • Proposed as answer by Adam_Turner Tuesday, March 29, 2011 6:29 PM
    • Marked as answer by Mio_Miao Friday, April 1, 2011 11:24 AM
    Tuesday, March 29, 2011 5:34 PM

All replies

  • This is an example of where you would use a dictionary.

    IDictionary<T,int> count = new Dictionary<T,int>();

    where T is whichever type you're using as a key.

    Set it:

    count[i] = j; // never throws ArgumentOutOfRangeException

    Get the value:

    int j = count[i]; // throws KeyNotFoundException if i has not been added to the dictionary.

    Get the value safely:

    int value;
    
    
    
    if (count.TryGetValue(i, out value))
    
    {
    
    // key exists, no exception thrown
    
    }
    
    else
    
    {
    
    // key does not exist, no exception thrown
    
    
    
    }
    
    
    
    

    Tuesday, March 29, 2011 4:35 PM
  • An arraylist is dynamic and doesn't require a ubound be set.

    Adam


    Ctrl+Z
    • Proposed as answer by JohnGrove Tuesday, March 29, 2011 5:33 PM
    • Marked as answer by Mio_Miao Friday, April 1, 2011 11:24 AM
    Tuesday, March 29, 2011 4:47 PM
  • Also, if possible, use the List<T>. Then when checking to see if a value exists in the collections use the Contains method. 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          List<Int32> myList = new List<Int32>();
          myList.Add(32);
          myList.Add(8);
          myList.Add(1);
          myList.Add(26);
          if (myList.Contains(8))
          {
            Console.WriteLine("Contains 8");
          }
          Console.ReadLine();
        }
      }
    }
    


    John Grove, MCC - Senior Software Engineer
    • Proposed as answer by Adam_Turner Tuesday, March 29, 2011 6:29 PM
    • Marked as answer by Mio_Miao Friday, April 1, 2011 11:24 AM
    Tuesday, March 29, 2011 5:34 PM
  • hello,

     

    i'm not adding values to the ArrayList in an index-based order.

    and i don't need to check whether a value exists in the ArrayList, but to check whether a value exists at a given index. 

    say i have:

     

    int j = 4; (j determined at runtime)

    if (ArrayList[4] != null)

    {process ArrayList[4] in some manner;

    //up to this point ArrayList of [0,1,2,3] may or may not have been initialised

    }

    thanks 

    Wednesday, April 6, 2011 9:04 AM
  • that did the trick.

     

    thanks Evan M.


    Wednesday, April 6, 2011 9:13 AM