collections

Enum to dictionary in C#

放肆的年华 提交于 2019-12-17 10:33:46
问题 I have searched this online, but I can't find the answer I am looking for. Basically I have the following enum: public enum typFoo : int { itemA : 1, itemB : 2 itemC : 3 } How can I convert this enum to Dictionary so that it stores in the following dictionary? Dictionary<int,string> mydic = new Dictionary<int,string>(); And mydic would look like this: 1, itemA 2, itemB 3, itemC Any ideas? 回答1: See: How do I enumerate an enum in C#? foreach( typFoo foo in Enum.GetValues(typeof(typFoo)) ) {

Difference between HashMap and ArrayList in Java?

蹲街弑〆低调 提交于 2019-12-17 10:23:23
问题 In Java, ArrayList and HashMap are used as collections. But I couldn't understand in which situations we should use ArrayList and which times to use HashMap . What is the major difference between both of them? 回答1: You are asking specifically about ArrayList and HashMap, but I think to fully understand what is going on you have to understand the Collections framework. So an ArrayList implements the List interface and a HashMap implements the Map interface. So the real question is when do you

Jquery how to find an Object by attribute in an Array

痞子三分冷 提交于 2019-12-17 10:23:09
问题 Given I have an array of "purpose" objects: //array of purpose objects: var purposeObjects = [ {purpose: "daily"}, {purpose: "weekly"}, {purpose: "monthly"} ]; (for simplicity i am omitting other attributes) Now I want to have a method that returns a specific one of the objects if a matching purpose name is found. This is not working: function findPurpose(purposeName){ return $.grep(purposeObjects, function(){ return this.purpose == purposeName; }); }; findPurpose("daily"); but it actually

Jquery how to find an Object by attribute in an Array

江枫思渺然 提交于 2019-12-17 10:22:10
问题 Given I have an array of "purpose" objects: //array of purpose objects: var purposeObjects = [ {purpose: "daily"}, {purpose: "weekly"}, {purpose: "monthly"} ]; (for simplicity i am omitting other attributes) Now I want to have a method that returns a specific one of the objects if a matching purpose name is found. This is not working: function findPurpose(purposeName){ return $.grep(purposeObjects, function(){ return this.purpose == purposeName; }); }; findPurpose("daily"); but it actually

HashMap initialization parameters (load / initialcapacity)

与世无争的帅哥 提交于 2019-12-17 10:17:11
问题 What values should I pass to create an efficient HashMap / HashMap based structures for N items? In an ArrayList , the efficient number is N (N already assumes future grow). What should be the parameters for a HashMap ? ((int)(N * 0.75d), 0.75d)? More? Less? What is the effect of changing the load factor? 回答1: Regarding the load factor, I'll simply quote from the HashMap javadoc: As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher

Why Collections.sort uses merge sort instead of quicksort? [closed]

≯℡__Kan透↙ 提交于 2019-12-17 10:07:27
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . We know that quick sort is the fastest sorting algorithm. The collections.sort used merge sort algorithm instead of quick sort. But Arrays.sort uses quick sort. What is the reason Collections.sort uses merge sort

System.InvalidOperationException: Collection was modified

折月煮酒 提交于 2019-12-17 09:59:55
问题 I am getting a following exception while enumerating through a queue: System.InvalidOperationException: Collection was modified; enumeration operation may not execute here is the code excerpt: 1: private bool extractWriteActions(out List<WriteChannel> channelWrites) 2: { 3: channelWrites = new List<WriteChannel>(); 4: foreach (TpotAction action in tpotActionQueue) 5: { 6: if (action is WriteChannel) 7: { 8: channelWrites.Add((WriteChannel)action); 9: lock(tpotActionQueue) 10: { 11: action

How can I have a collection of objects that differ by their associated type?

三世轮回 提交于 2019-12-17 09:59:39
问题 I have a program that involves examining a complex data structure to see if it has any defects. (It's quite complicated, so I'm posting example code.) All of the checks are unrelated to each other, and will all have their own modules and tests. More importantly, each check has its own error type that contains different information about how the check failed for each number. I'm doing it this way instead of just returning an error string so I can test the errors (it's why Error relies on

Using a prepared statement and variable bind Order By in Java with JDBC driver

被刻印的时光 ゝ 提交于 2019-12-17 09:46:51
问题 I'm using jdbcTemplate to make JDBC connections to a mySQL DB prepared statements to protect myself as much as possible from SQL injection attacks in need to accept requests from the user to sort the data on any of a dozen different columns the following statement jdbcTemplate.query("SELECT * FROM TABLE1 ORDER BY ? ?", colName, sortOrder); Of course this doesn't work, because the variable bindings aren't supposed to specify column names just parameter values for expressions in the query. So..

Can LINQ use binary search when the collection is ordered?

倖福魔咒の 提交于 2019-12-17 08:54:27
问题 Can I somehow "instruct" LINQ to use binary search when the collection that I'm trying to search is ordered. I'm using an ObservableCollection<T> , populated with ordered data, and I'm trying to use Enumerable.First(<Predicate>). In my predicate, I'm filtering by the value of the field my collection's sorted by. 回答1: As far as I know, it's not possible with the built-in methods. However it would be relatively easy to write an extension method that would allow you to write something like that