【问题标题】:I want change variable to value of hashtables in arraylist我想将变量更改为 arraylist 中哈希表的值
【发布时间】:2016-05-20 07:36:44
【问题描述】:

我做了[Hashtable hash]比如

hash(i, 1)
hash(j, 2)

我还制作了一个 [arraylist sArray],其中包括“i”或“j”,例如

sArray[0] : hello                 
sArray[1] : first number is i  
sArray[2] : second number is j  
sArray[3] : bye  

现在,我想将 sArray 中的“i”和“j”更改为哈希值。
我该怎么做?

【问题讨论】:

  • 没有明确的方法可以做到这一点。更好地解释用例。也许不需要两个集合

标签: c# arraylist hashtable


【解决方案1】:

如果我理解正确,我认为这是c#中的代码

//Your example
int i = 1;
int j = 2;

var hash = new System.Collections.Hashtable();
hash[i] = -173.5;
hash[j] = 37;

var sArray = new System.Collections.ArrayList();
sArray.Add("hello");
sArray.Add("first number is " + hash[i].ToString());
sArray.Add("second number is " + hash[j].ToString());
sArray.Add("bye");

// more general, you could have different i and j position
i = 3;
j = 4;

hash[i] = 33.3;
hash[j] = -44.4;

sArray[1] = "number in " + i.ToString() + " position is " + hash[i].ToString();
sArray[2] = "number in " + j.ToString() + " position is " + hash[j].ToString();

// I think following option is more easy to read and fast if iterated
i = 5;
j = 6;

hash[i] = 55.5;
hash[j] = -66.6;

sArray[1] = String.Format("number in {0} position is {1}", i, hash[i]);
sArray[2] = String.Format("number in {0} position is {1}", j, hash[j]);

【讨论】:

    猜你喜欢
    • 2017-05-11
    • 2015-01-20
    • 2015-07-13
    • 1970-01-01
    • 2021-07-08
    • 2021-08-18
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多