array

Best way to convert IList or IEnumerable to Array

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a HQL query that can generate either an IList of results, or an IEnumerable of results. However, I want it to return an array of the Entity that I'm selecting, what would be the best way of accomplishing that? I can either enumerate through it and build the array, or use CopyTo() a defined array. Is there any better way? I went with the CopyTo-approach. 回答1: Which version of .NET are you using? If it's .NET 3.5, I'd just call ToArray() and be done with it. If you only have a non-generic IEnumerable, do something like this: IEnumerable

Complex C declaration

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I was just going through some code on the Internet and found this: float * (*(*foo())[SIZE][SIZE])() How do I read this declaration? Is there a specific set of rules for reading such complex declarations? 回答1: I haven't done this in a while! Start with foo and go right. float * (*(* foo() )[SIZE][SIZE])() foo is a function with no arguments... Can't go right since there's a closing parenthesis. Go left: float * (*( * foo() )[SIZE][SIZE])() foo is a function with no arguments returning a pointer Can't go left further, so let's cross the

What does the JSLint error 'body of a for in should be wrapped in an if statement' mean?

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I used JSLint on a JavaScript file of mine. It threw the error: for( ind in evtListeners ) { Problem at line 41 character 9: The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype. What does this mean? 回答1: First of all, never use a for in loop to enumerate over an array. Never. Use good old for(var i = 0; i<arr.length; i++) . The reason behind this is the following: each object in JavaScript has a special field called prototype . Everything you add to that field is going to be accessible

Multiply array of vectors with array of matrices; return array of vectors?

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've got a numpy array of row vectors of shape (n,3) and another numpy array of matrices of shape (n,3,3). I would like to multiply each of the n vectors with the corresponding matrix and return an array of shape (n,3) of the resulting vectors. By now I've been using a for loop to iterate through the n vectors/matrices and do the multiplication item by item. I would like to know if there's a more numpy-ish way of doing this. A way without the for loop that might even be faster. //edit 1: As requested, here's my loopy code (with n = 10 ): arr

Why sizeof(param_array) is the size of pointer?

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I want to get the length of an array, say int array[] = {1, 2, 3, 4} . I used sizeof to do that. int length ( int array []) { return sizeof ( array ) / sizeof ( int ); } int main () { int array [] = { 1 , 2 , 3 , 4 }; printf ( "%d\n" , length ( array )); // print 1 printf ( "%d\n" , sizeof ( array ) / sizeof ( int )); // print 4 } So, why the sizeof(array) in function length returns the pointer size of array ? But in function main , it works. And, how should I modify the length function to get an array's length? 回答1: A special C

Illegal string offset Warning PHP

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I get a strange PHP error after updating my php version to 5.4.0-3. I have this array: Array ( [host] => 127.0.0.1 [port] => 11211 ) When I try to access it like this I get strange warnings print $memcachedConfig['host']; print $memcachedConfig['port']; Warning: Illegal string offset 'host' in .... Warning: Illegal string offset 'port' in ... I really don't want to just edit my php.ini and re-set the error level. Thanks for any help! 回答1: Please try this way.... I have tested this code.... It works.... $memcachedConfig = array("host" => "127

Get all defined classes of a parent class in php

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I need to determine, after all files have been included, which classes extend a parent class, so: class foo{ } class boo extends foo{ } class bar extends foo{ } and I'd like to be able to grab an array like: array('boo','bar'); 回答1: If you need that, it really smells like bad code, the base class shouldn't need to know this. However, if you definitions have been included (i.e. you don't need to include new files with classes you possibly have), you could run: $children = array(); foreach(get_declared_classes() as $class){ if($class

$unwind empty array

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a collection of users where each document has following structure: { "_id": "<id>", "login": "xxx", "solved": [ { "problem": "<problemID>", "points": 10 }, ... ] } The field solved may be empty or contain arbitrary many subdocuments. My goal is to get a list of users together with the total score (sum of points ) where users that haven't solved any problem yet will be assigned total score of 0. Is this possible to do this with a single query (ideally using aggregation framework)? I was trying to use following query in aggregation

How do I do indexOfObject or a proper containsObject

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: With an array: How do I do indexOfObject or a proper containsObject ? I mean I know I could just bridge the Array to NSArray and do it there ^^ But there must be a 'native' way of doing this P.S. for the containsObject I guess I could filter the array too but for indexOf ? 回答1: You can use the built-in find , and thus avoid bridging to Objective-C ― but only if your element type is Equatable . (If it isn't Equatable, you can make it so with a comparison function and an extension.) Example: func == (lhs:Piece,rhs:Piece) -> Bool { return lhs

Why is an array not assignable to Iterable?

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: with Java5 we can write: Foo[] foos = ... for (Foo foo : foos) or just using an Iterable in the for loop. This is very handy. However you can't write a generic method for iterable like this: public void bar(Iterable foos) { .. } and calling it with an array since it is not an Iterable: Foo[] foos = { .. }; bar(foos); // compile time error I'm wondering about the reasons behind this design decision. 回答1: Arrays can implement interfaces ( Cloneable and java.io.Serializable ). So why not Iterable ? I guess Iterable forces adding an iterator