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?
Printable View
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?
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...Quote:
Originally Posted by ashukasama
Any other array which allow me insert to any index?
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?Quote:
Originally Posted by Arjay
It can't be done with an arraylist. You have two options.Quote:
Originally Posted by lsy
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??Quote:
Originally Posted by Homogenn
Not clearQuote:
if i add in the key not in sequencial way... how can i use loop to read back all the key value??
dict.Add(1, "hyj");Quote:
Originally Posted by ashukasama
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:Quote:
Originally Posted by lsy
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?Quote:
Originally Posted by Homogenn
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.Quote:
Originally Posted by lsy
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 second the motion :D
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...Quote:
Originally Posted by Arjay