当Hashmap可以完成相同的工作时,有必要引入Pair类吗?
我看到Pair被引入Java版本8
我有来自服务器的响应,它是NSString,看起来像这样
resp=handshake&clientid=47D3B27C048031D1&success=true&version=1.0
Run Code Online (Sandbox Code Playgroud)
我想将它转换为键值对,如字典或数组.我找不到任何有用的内置函数来解码NSString到NSdictionary并替换&with space并没有解决我的问题,任何人都可以给我任何想法或是否有任何函数来解决这个问题?
我在"列表(KeyValuePair(Of)"集合中执行For Each循环时遇到问题.
我正在将ParamArray传递给函数
ParamArray args() As List(Of KeyValuePair(Of String, Integer))
Run Code Online (Sandbox Code Playgroud)
我正在尝试在Collection上做For Each
For Each arg As KeyValuePair(Of String, Integer) In args
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
类型'System.Collections.Generic.List(Of System.Collections.Generic.KeyValuePair(Of String,Integer))'的值不能转换为'System.Collections.Generic.KeyValuePair(Of String,Integer)'
我只是无法弄清楚如何做到这一点,我找不到任何如何做到这一点的例子.
我现在唯一的想法是做
For Each arg As Object In args
Run Code Online (Sandbox Code Playgroud)
然后将对象转换为KeyValuePair,但必须有一个更简洁的方法来做到这一点......
我无法创建一个类或我自己的对象,所以我想我会使用a List<KeyValuePair>来存储两个属性,然后将这个对象绑定到一个组合框.
但是,我无法看到如何在组合框中设置valueField和TextField.
代码.
List<KeyValuePair<int, string>> kvpObject =
new List<KeyValuePair<int, string>>();
foreach (User u in m_users) {
kvpObject.Add(new KeyValuePair<int, string>(u.ID, u.Name));
}
// Bind Add Users combobox
cmboBox.DataSource = kvpObject;
cmboBox.ValueField = "????" // Maybe something like kvpObject[0]..
cmboBox.TextField = "????";
cmboBox.DataBind();
Run Code Online (Sandbox Code Playgroud)
有谁知道我需要把它放进去????.
码:
var list = new List<KeyValuePair<int, string[]>>();
list .Add(new KeyValuePair<int, string[]>(1, new string[] { "1", "One"}));
list .Add(new KeyValuePair<int, string[]>(2, new string[] { "2", "Two"}));
list .Add(new KeyValuePair<int, string[]>(3, new string[] { "3", "Three"}));
list .Add(new KeyValuePair<int, string[]>(4, new string[] { "4", "Four"}));
Run Code Online (Sandbox Code Playgroud)
需要仅选择KeyValuePairs列表中的值并构建数组.我试过这个.
var values = list.Select(v => v.Value.ToList()).ToArray();
Run Code Online (Sandbox Code Playgroud)
期待string[]这样的.
{"1", "One", "2", "Two", "3", "Three", "4", "Four"}
Run Code Online (Sandbox Code Playgroud)
但它会返回List<string>[]这样的
{{"1", "One"}, {"2", "Two"}, {"3", "Three"}, {"4", "Four"}}
Run Code Online (Sandbox Code Playgroud)
也试过了
var values = list.Select(v => v.Value.ToArray()).ToArray();
Run Code Online (Sandbox Code Playgroud)
但它回来了 …
我有一个ComboBox,里面装满了两种不同类型的混合物品.类型是
KeyValuePair<Int32, FontFamily>
Run Code Online (Sandbox Code Playgroud)
要么
KeyValuePair<Int32, String>
Run Code Online (Sandbox Code Playgroud)
现在有些时候我只对所选项目的Key感兴趣,它始终是Int32.
访问所选项目的密钥最简单的方法是什么?我在想类似的东西
Int32 key = ((KeyValuepair<Int32, object/T/var/IdontCare>)combobox.SelectedItem).Key;
Run Code Online (Sandbox Code Playgroud)
但那不起作用.
所以我只有
Int32 key;
if(combobox.SelectedItem.GetType().Equals(typeof(KeyValuePair<Int32, FontFamily)))
{
key = ((KeyValuePair<Int32, FontFamily)combobox.SelectedItem).Key;
}
else if(combobox.SelectedItem.GetType().Equals(typeof(KeyValuePair<Int32, String)))
{
key = ((KeyValuePair<Int32, String)combobox.SelectedItem).Key;
}
Run Code Online (Sandbox Code Playgroud)
哪个有效,但我想知道是否有更优雅的方式?
我正在开发一个ASP.Net Web API项目.在我的项目中,我试图以这种格式从动作中返回JSON.
{
"Apple": null,
"Microsoft": null,
"Google": 'http://placehold.it/250x250'
}
Run Code Online (Sandbox Code Playgroud)
正如您在JSON中看到的那样,它是键值对.键和值都是动态的.所以我试图NameValueCollection从行动中返回以获得该格式.请参阅下面的代码.
public class CompaniesController : ApiController
{
// GET api/<controller>
public HttpResponseMessage Get()
{
NameValueCollection collection = new NameValueCollection();
collection.Add("Microsoft", "microsoft.com");
collection.Add("Google", "google.com");
collection.Add("Facebook", "facebook.com");
return Request.CreateResponse(HttpStatusCode.OK, collection, "application/json");
}
}
Run Code Online (Sandbox Code Playgroud)
但后来我访问了该操作,它返回JSON,如下所示.
如您所见,它只返回Keys.姓名不包括在内.那不是我想要的格式.请问如何修复我的代码以获取返回的名称值对.
我正在开发一个React应用程序,我需要转换这样的键值对象:
{
0: 'John',
1: 'Tim',
2: 'Matt'
};
Run Code Online (Sandbox Code Playgroud)
对于像这样的值的数组:
['John', 'Tim', 'Matt']
Run Code Online (Sandbox Code Playgroud)
我该如何做到这一点?
const obj = {
0: 'John',
1: 'Tim',
2: 'Matt'
};
const arr = /* ??? */;
Run Code Online (Sandbox Code Playgroud) 我有以下 PHP 代码,它通过键名称搜索数组并返回等于搜索的结果。只是出于好奇,我该怎么做,无论我搜索什么结果都会是名称键值等于“A”的元素?很快我怎样才能检索名称为“A”的元素?
\n\n我尝试了以下方法,但 \xc2\xb4t 有效:
\n\n$jSearchResult[] = $ajProducts[$i]->name = "A";\nRun Code Online (Sandbox Code Playgroud)\n\n请帮助我,因为我只能\xc2\xb4t弄清楚如何通过键值检索数组的元素,但我确信它一定是非常简单的。
\n\n<?php\n\n//DATA coming from the BROWSER\n$sSearch = $_GET[\'search\'];\n//TURN it into UPPERCASE\nstrtoupper( $sSearch );\n\n//GETTING from the TEXT FILE:\n$sajProducts = file_get_contents( \'products.txt\' );\n$ajProducts = json_decode( $sajProducts );\n\n$match_found = false;\n//Collect all matching result in an array \n$jSearchResult = array();\n//LOOPING THROUGH THE ARRAY OF PRODUCTS\nfor ( $i=0; $i< count( $ajProducts ); $i++ ) {\n\n if ( $sSearch == $ajProducts[$i]->name ) {\n $jSearchResult[] = $ajProducts[$i]->name;\n $match_found = true; \n }\n}\n//if …Run Code Online (Sandbox Code Playgroud) 我有一个对象数组,我需要转换为键值对,我的代码是
我有一个模型:model setting.php
public function currency_info(){
$currency_info = [
['code' => 'AED', 'name' => 'United Arab Emirates Dirham'],
['code' => 'ANG', 'name' => 'NL Antillian Guilder'],
['code' => 'ARS', 'name' => 'Argentine Peso'],
['code' => 'AUD', 'name' => 'Australian Dollar'],
['code' => 'BRL', 'name' => 'Brazilian Real'],
]
}
Run Code Online (Sandbox Code Playgroud)
和控制器为:
设置控制器.php
public function index()
{
$setting = new Setting();
$currency_list = $setting->currency_info();
$result = [];
foreach ($currency_list as $currency) {
$result[$currency->code] = $currency->name;
}
return $result;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
ErrorException (E_NOTICE) …
keyvaluepair ×10
c# ×4
arrays ×2
object ×2
php ×2
asp.net ×1
casting ×1
collections ×1
combobox ×1
ecmascript-6 ×1
foreach ×1
hashmap ×1
java ×1
javascript ×1
key-value ×1
laravel ×1
linq ×1
nsdictionary ×1
nsstring ×1
objective-c ×1
vb.net ×1
winforms ×1