Home > Net >  About the key/value pair Dictionary <List <int>, int> some of the problems
About the key/value pair Dictionary <List <int>, int> some of the problems

Time:10-10

Dictionary has a either ContainsKey method, the effect of this method is to check whether there is the key in the key/value pair, in use today, I have a little trouble:

 

The Dictionary & lt; List Int> Mydic=new Dictionary Int> (a);


List Data=https://bbs.csdn.net/topics/new List ();

Data. The Add (1);
Data. The Add (2);
Data. The Add (3);

Mydic. Add (data, 0);



//and then create a new Lis With either ContainsKey check with two methods:


//method one:
List newData=https://bbs.csdn.net/topics/new List ();
Data. The Foreach (x=& gt; NewData. Add (x));

If (mydic. Either ContainsKey (newData))
{
The Console. Write (" exist ");
}




//method 2:
List newData=https://bbs.csdn.net/topics/new List ();
newData=https://bbs.csdn.net/topics/data;

If (mydic. Either ContainsKey (newData))
{
The Console. Write (" exist ");
}




Conclusion:

Only the second method to print out the exist,


Question:

(1) either ContainsKey find what is the nature of?
(2) what is the difference between the first and the second method?
(3) the first approach on how to improve?




CodePudding user response:

You need the first method is, to write a custom equal comparator is ok,

CodePudding user response:

reference 1/f, uncle tis response:
do you need the first method is, to write a custom equal comparator,


Don't understand is that the first kind of way to what went wrong?

CodePudding user response:

Either ContainsKey is actually equal judgment is the key element, the end is equal to key judgment
And you here, the key is List , this judgment on the basis of equal is whether the two List of references, rather than the inside of the List elements are equal to
In fact List Do the key meaningless also can not meet the expected effect

CodePudding user response:

reference stherix reply: 3/f
either ContainsKey is actually equal judgment is the key element, is equal to key final judgment
And you here, the key is List , this judgment on the basis of equal is whether the two List of references, rather than the inside of the List elements are equal to
In fact List Do the key meaningless also reach expected effect


Oh, oh, I think I see what you mean,
Do you think this way:
 

The Dictionary & lt; Int, int> Mydic=new Dictionary (a);

Mydic. Add (1, 5);

Int value=https://bbs.csdn.net/topics/1;
If (mydic. Either ContainsKey (value))
{
The Console. Write (" existence, value is: {0} ", mydic [value]);
}
The else
{
The Console. Write (" does not exist ");

}



In the above example head:
(1) for the arbitrary value of external input value, through either ContainsKey can tell, this value does not exist in mydic, exists, it can obtain the value of the corresponding

(2) the above example, simply reflects the find another value, through a value, did you want to me at the top of the posts, through the two values, or more than one value, obtain the corresponding value,,, so their courage to try the Dictionary Int> , and found this way seems to be not ideal

CodePudding user response:

You can start your List Derive a class, realize IEqualityComparer Interface
And then realize the Equals method, judge whether the child elements of the two List inside exactly equal
This class can be judged as a key

CodePudding user response:

Note that if the name of the class is xxxx , the interface will be IEqualityComparer

CodePudding user response:

refer to 6th floor stherix response:
note that if the class name is xxxx , the interface will be IEqualityComparer



Oh oh oh, crab crab

CodePudding user response:

(1) either ContainsKey find what is the nature of?
By key to judge whether there is, if you can give you a LIST of data,

(2) what is the difference between the first and the second method?

One is to find, is a assignment
(3) the first approach on how to improve?

 Dictionary & lt; Int, int> Mydic=new Dictionary (a); 

Mydic. Add (1, 5);

Int value=https://bbs.csdn.net/topics/1;
If (mydic. Either ContainsKey (value))
{
//if there is a can
Mydic [value]). The Add (value);
}
The else
{
//there is no
Mydic [value])=new List ()} {your values;

}

CodePudding user response:

Key is divided into value types and reference types, and compare the value type of the two values, a reference type is comparison of the two addresses, even if the content of the reference type is exactly the same but different address, that is a different key (with the exception of type string), and List Clearly is a reference type,

CodePudding user response:

The adjusted,

 System. Collections. Generic. Dictionary Mydic=new System. Collections. Generic. Dictionary (a); 

Mydic. Add (1, the new System. Collections. Generic. List ()} {your value);

The int value=https://bbs.csdn.net/topics/1;
If (mydic. Either ContainsKey (value))
{
//if there is a can
Mydic [value]. Add (value);
}
The else
{
//there is no
Mydic [value]=new System. Collections. Generic. List ()} {your values;

}

CodePudding user response:

Look at your KEY had better use a simple type, if use LIST He can go to the TypeID and processing,

CodePudding user response:

The
references to the tenth floor XBodhi. Response:
revised,

 System. Collections. Generic. Dictionary Mydic=new System. Collections. Generic. Dictionary (a); 

Mydic. Add (1, the new System. Collections. Generic. List ()} {your value);

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related