CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: ArrayList

  1. #1
    Join Date
    Feb 2005
    Posts
    568

    ArrayList

    i declare an ArrayList obj... is there anyway can provide me add an element not in sequencial mode?? example i insert element index 1 before index 0?

  2. #2
    Join Date
    May 2007
    Posts
    437

    Re: ArrayList

    use System.Collections.Generic.LinkedList
    ashu
    always use code tag

  3. #3
    Join Date
    Feb 2005
    Posts
    568

    Re: ArrayList

    Quote Originally Posted by ashukasama
    use System.Collections.Generic.LinkedList
    It also can't allowed me insert in any index... i have to know the current index before i can insert...
    Any other array which allow me insert to any index?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: ArrayList

    Both the generic List class and the non-generic ArrayList class has an Insert method which takes an index to specify where to insert a new item.

    In general, prefer to use the generic collections over the older non-generic counterparts (because the generic ones are typesafe).

  5. #5
    Join Date
    Feb 2005
    Posts
    568

    Re: ArrayList

    Quote Originally Posted by Arjay
    Both the generic List class and the non-generic ArrayList class has an Insert method which takes an index to specify where to insert a new item.

    In general, prefer to use the generic collections over the older non-generic counterparts (because the generic ones are typesafe).
    I'm using ArrayList but when i try to insert at index 1 before index 0, an exception occur... anyway to omit this?

  6. #6
    Join Date
    Dec 2006
    Posts
    203

    Re: ArrayList

    Quote Originally Posted by lsy
    I'm using ArrayList but when i try to insert at index 1 before index 0, an exception occur... anyway to omit this?
    It can't be done with an arraylist. You have two options.

    If you want to use the ArrayList or List you have to insert it at index 0 and then when another comes, insert that at index 0, so the first one becomes index 1 (I assume this isn't what you're looking for, though)

    The other is to use a dictionary as such:

    Code:
                string a = "A";
                string b = "B";
                Dictionary<int, string> dict = new Dictionary<int, string>();
    
                // if you want "A" to have value 1 and "B" to have value 0
                dict.Add(1, a);
                dict.Add(0, b);
    
                // Find "A" again
                Console.WriteLine(dict[1]);
    Sincerely,

    Martin Svendsen

  7. #7
    Join Date
    Feb 2005
    Posts
    568

    Re: ArrayList

    Quote Originally Posted by Homogenn
    It can't be done with an arraylist. You have two options.

    If you want to use the ArrayList or List you have to insert it at index 0 and then when another comes, insert that at index 0, so the first one becomes index 1 (I assume this isn't what you're looking for, though)

    The other is to use a dictionary as such:

    Code:
                string a = "A";
                string b = "B";
                Dictionary<int, string> dict = new Dictionary<int, string>();
    
                // if you want "A" to have value 1 and "B" to have value 0
                dict.Add(1, a);
                dict.Add(0, b);
    
                // Find "A" again
                Console.WriteLine(dict[1]);
    if i add in the key not in sequencial way... how can i use loop to read back all the key value??

  8. #8
    Join Date
    May 2007
    Posts
    437

    Re: ArrayList

    if i add in the key not in sequencial way... how can i use loop to read back all the key value??
    Not clear
    Last edited by ashukasama; December 17th, 2007 at 04:49 AM.
    ashu
    always use code tag

  9. #9
    Join Date
    Feb 2005
    Posts
    568

    Re: ArrayList

    Quote Originally Posted by ashukasama
    Not clear
    dict.Add(1, "hyj");
    dict.Add(6, "de");
    dict.Add(10,"asdf");
    if use the count it will return 3 so how can i know which key is with value?

  10. #10
    Join Date
    Dec 2006
    Posts
    203

    Re: ArrayList

    Quote Originally Posted by lsy
    dict.Add(1, "hyj");
    dict.Add(6, "de");
    dict.Add(10,"asdf");
    if use the count it will return 3 so how can i know which key is with value?
    Problem is that the dictionary can contain the same value with several keys, just like an ArrayList can contain the same value with several indexes. You'll have to 'hackfix' it for this reason:

    Code:
    // Print out values
    foreach (string s in dict.Values)
                    Console.WriteLine(s);
    
    // Print out values with keys:
    foreach (string s in dict.Values)
    {
      Console.Write("Value: " + s + " Keys: ");
      foreach (int key in dict.Keys)
        if (dict[key] == s)
          Console.Write(key + " ");
      Console.Write("\n");
    }

    Though, why'd you want to? ;o
    Sincerely,

    Martin Svendsen

  11. #11
    Join Date
    Feb 2005
    Posts
    568

    Talking Re: ArrayList

    Quote Originally Posted by Homogenn
    Problem is that the dictionary can contain the same value with several keys, just like an ArrayList can contain the same value with several indexes. You'll have to 'hackfix' it for this reason:

    Code:
    // Print out values
    foreach (string s in dict.Values)
                    Console.WriteLine(s);
    
    // Print out values with keys:
    foreach (string s in dict.Values)
    {
      Console.Write("Value: " + s + " Keys: ");
      foreach (int key in dict.Keys)
        if (dict[key] == s)
          Console.Write(key + " ");
      Console.Write("\n");
    }

    Though, why'd you want to? ;o
    is it possible the dictionary contains 1 key and 2 values and how the value get read?

  12. #12
    Join Date
    Dec 2006
    Posts
    203

    Re: ArrayList

    Quote Originally Posted by lsy
    is it possible the dictionary contains 1 key and 2 values and how the value get read?
    No. One key can only point to one value, like one index can only point to one value. Otherwise it can't find one of them.
    Sincerely,

    Martin Svendsen

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: ArrayList

    Lsy, if you give us a general, high level description of what you are trying to accomplish, we may be able to suggest an approach.

  14. #14
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: ArrayList

    ^ I second the motion
    Busy

  15. #15
    Join Date
    Feb 2005
    Posts
    568

    Re: ArrayList

    Quote Originally Posted by Arjay
    Lsy, if you give us a general, high level description of what you are trying to accomplish, we may be able to suggest an approach.
    i'm looking for something like an array or structure with 1 index hold multiple value! so the index will be my reference point for that particular record or values...

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured