array

Converting Array to List

匿名 (未验证) 提交于 2019-12-03 02:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In order to convert an Integer array to a list of integer I have tried the following approaches: Initialize a list(Integer type), iterate over the array and insert into the list By using Java 8 Streams: int[] ints = {1, 2, 3}; List<Integer> list = new ArrayList<Integer>(); Collections.addAll(list, Arrays.stream(ints).boxed().toArray(Integer[]::new)); Which is better in terms of performance? 回答1: The second one creates a new array of Integers (first pass), and then adds all the elements of this new array to the list (second pass). It will

Convert array to slice in Go

匿名 (未验证) 提交于 2019-12-03 02:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This seems like it would be a fairly common thing and abundant examples across the interwebs, but I can't seem to find an example of how to convert an [32]byte to []byte . I have a function that I call from an external lib that returns an array func Foo() [32]byte {...} I then need to pass that result to a different function for further processing. func Bar(b []byte) { ... } Unforunately, if I try to call d := Foo() Bar(d) I get cannot convert d (type [32]byte) to type []byte Doing []byte(d) isn't much better. How do I do this, especially

Ruby: How to group a Ruby array?

匿名 (未验证) 提交于 2019-12-03 02:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a Ruby array > list = Request.find_all_by_artist("Metallica").map(&:song) => ["Nothing else Matters", "Enter sandman", "Enter Sandman", "Master of Puppets", "Master of Puppets", "Master of Puppets"] and I want a list with the counts like this: {"Nothing Else Matters" => 1, "Enter Sandman" => 2, "Master of Puppets" => 3} So ideally I want a hash that will give me the count and notice how I have Enter Sandman and enter sandman so I need it case insensitive. I am pretty sure I can loop through it but is there a cleaner way? 回答1: list

Counting unique element in large array

匿名 (未验证) 提交于 2019-12-03 02:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: One of my colleague was asked following question in an interview. Given a huge array which stores unsigned int. Length of array is 100000000. Find the effective way to count the unique number of elements present in the array. E.g arr = {2,34,5,6,7,2,2,5,1,34,5} O/p: Count of 2 is 3, Count of 34 is 2 and so on. What is the effective algorithms to do this? I thought at first dictionary/hash would be one of the option, but since array is very large it is in-efficient. Is there any way to do this? Thanks, chota 回答1: Heap sort is O(nlogn) and in

Adding Array of Markers in Google Map

匿名 (未验证) 提交于 2019-12-03 02:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I need some help in creating an array of markers in Google Map so that it can be more efficient, instead of create a marker one by one. I tried the following but it doesn't work. Anyone have advice? //create array to store a set of location var collection = new Array(); //a set of locations stored in array collection[0] = new google.maps.LatLng(13.742167701649997, 100.50721049308777); collection[1] = new google.maps.LatLng(13.74428, 100.5404525); collection[2] = new google.maps.LatLng(13.744108, 100.543098); var pointMarkerImage = new Array(

How to Flatten a Multidimensional Array?

匿名 (未验证) 提交于 2019-12-03 02:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is it possible, in PHP, to flatten a (bi/multi)dimensional array without using recursion or references? I'm only interested in the values so the keys can be ignored, I'm thinking in the lines of array_map() and array_values() . 回答1: You can use the Standard PHP Library (SPL) to "hide" the recursion. $a = array(1,2,array(3,4, array(5,6,7), 8), 9); $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a)); foreach($it as $v) { echo $v, " "; } prints 1 2 3 4 5 6 7 8 9 回答2: As of PHP 5.3 the shortest solution seems to be array_walk

How to get the previous and next elements of an array loop in JavaScript?

匿名 (未验证) 提交于 2019-12-03 02:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 选择语言 中文(简体) 日语 英语 中文(繁体) 由 翻译 强力驱动 问题: In a simple array loop of Javacript as for ( var i = 0 ; i < array . length ; i ++) { var previous = array [ i - 1 ]; var current = array [ i ]; var next = array [ i + 1 ]; } I need to get the previous and next elements in an unlimited cycle. For example, The previous element of the first element in the array is the array last element The next element of the last element in the array is the array first element What can be the most efficient way to do this. The only way I can think of is to check if the

Issue with Laravel Rules &amp; Regex (OR) operator

匿名 (未验证) 提交于 2019-12-03 02:46:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm having a small issue with my Laravel rules and regex operation : Basically a rule is an array as such : 'room'=>'required|alpha_num|min:2|max:10', The problem i'm having is when using regex and the | (or) operator such as : 'cid'=>'required|regex:/^((comp)|(soen)|(engr)|(elec))\d{3}$/i', I'm getting a server error saying : ErrorException preg_match(): No ending delimiter '/' found I'm guessing the preg_match is stopping at the first | inside the /.../ . Is there anyway to write the above code to make it work ? Full code : public static

Reshape 3d matrix to 2d matrix

匿名 (未验证) 提交于 2019-12-03 02:46:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a 3d matrix ( n-by-m-by-t ) in MATLAB representing n-by-m measurements in a grid over a period of time. I would like to have a 2d matrix, where the spatial information is gone and only n*m measurements over time t are left (ie: n*m-by-t ) How can I do this? 回答1: You need the command reshape : Say your initial matrix is (just for me to get some data): a=rand(4,6,8); Then, if the last two coordinates are spatial (time is 4, m is 6, n is 8) you use: a=reshape(a,[4 48]); and you end up with a 4x48 array. If the first two are spatial and

Initializing a boolean array to false

匿名 (未验证) 提交于 2019-12-03 02:46:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have this piece of code below. How do I initialize each element = false? boolean[] seats = new boolean[10] I saw a similar question. But, the second line didnt make sense to me (Can you explain the 2nd line?). Boolean[] array = new Boolean[size]; Arrays.fill(array, Boolean.FALSE); 回答1: The default value for the elements in a boolean[] is false. You don't need to do anything. The reason it's necessary for Boolean[] is because the default value is null . To initialize to true, use the overload of Arrays.fill that accepts a boolean[] .