-
December 16th, 2007, 11:14 PM
#1
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?
-
December 16th, 2007, 11:42 PM
#2
Re: ArrayList
use System.Collections.Generic.LinkedList
-
December 17th, 2007, 01:04 AM
#3
Re: ArrayList
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?
-
December 17th, 2007, 01:56 AM
#4
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).
-
December 17th, 2007, 01:58 AM
#5
Re: ArrayList
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?
-
December 17th, 2007, 03:40 AM
#6
Re: ArrayList
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
-
December 17th, 2007, 04:06 AM
#7
Re: ArrayList
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??
-
December 17th, 2007, 04:42 AM
#8
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.
-
December 17th, 2007, 05:09 AM
#9
Re: ArrayList
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?
-
December 17th, 2007, 08:30 AM
#10
Re: ArrayList
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
-
December 17th, 2007, 09:54 PM
#11
Re: ArrayList
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?
-
December 18th, 2007, 03:05 AM
#12
Re: ArrayList
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
-
December 18th, 2007, 10:42 AM
#13
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.
-
December 18th, 2007, 10:35 PM
#14
Re: ArrayList
^ I second the motion
Busy
-
December 18th, 2007, 10:41 PM
#15
Re: ArrayList
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|