array

JSON.stringify() array bizarreness with Prototype.js

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm trying to figure out what's gone wrong with my json serializing, have the current version of my app with and old one and am finding some surprising differences in the way JSON.stringify() works (Using the JSON library from json.org). In the old version of my app: JSON . stringify ({ "a" :[ 1 , 2 ]}) gives me this; "{\"a\":[1,2]}" in the new version, JSON . stringify ({ "a" :[ 1 , 2 ]}) gives me this; "{\"a\":\"[1, 2]\"}" any idea what could have changed to make the same library put quotes around the array brackets in the new

Python List vs. Array - when to use?

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: If you are creating a 1d array, you can implement it as a List, or else use the 'array' module in the standard library. I have always used Lists for 1d arrays. What is the reason or circumstance where I would want to use the array module instead? Is it for performance and memory optimization, or am I missing something obvious? 回答1: Basically, Python lists are very flexible and can hold completely heterogeneous, arbitrary data, and they can be appended to very efficiently, in amortized constant time . If you need to shrink and grow your array

How to declare 2D array in bash

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm wondering how to declare a 2D array in bash and then initialize to 0. In C it looks like this: int a [ 4 ][ 5 ] = { 0 }; And how do I assign a value to an element? As in C: a [ 2 ][ 3 ] = 3 ; 回答1: You can simulate them for example with hashes, but need care about the leading zeroes and many other things. The next demonstration works, but it is far from optimal solution. #!/bin/bash declare - A matrix num_rows = 4 num_columns = 5 for (( i = 1 ; i <= num_rows ; i ++)) do for (( j = 1 ; j <= num_columns ; j ++)) do matrix [ $i ,

How might I find the largest number contained in a JavaScript array?

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a simple JavaScript Array object containing a few numbers. [ 267 , 306 , 108 ] Is there a function that would find the largest number in this array? 回答1: Resig to the rescue: Array . max = function ( array ){ return Math . max . apply ( Math , array ); }; 回答2: You can use the apply function, to call Math.max : var array = [ 267 , 306 , 108 ]; var largest = Math . max . apply ( Math , array ); // 306 How it works? The apply function is used to call another function, with a given context and arguments, provided as an array.

Java Array Sort descending?

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there any EASY way to sort an array in descending order like how they have a sort in ascending order in the Arrays class ? Or do I have to stop being lazy and do this myself :[ 回答1: You could use this to sort all kind of Objects sort(T[] a, Comparator<? super T> c) Arrays.sort(a, Collections.reverseOrder()); Arrays.sort() cannot be used directly to sort primitive arrays in descending order. If you try to call the Arrays.sort() method by passing reverse Comparator defined by Collection.reverseOrder() , it will throw the error no suitable

How to put string in array, split by new line?

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a string with line breaks in my database. I want to convert that string into an array, and for every new line, jump one index place in the array. If the string is: My text1 My text2 My text3 The result I want is this: Array ( [0] => My text1 [1] => My text2 [2] => My text3 ) 回答1: You can use the explode function, using " \n " as separator : $your_array = explode("\n", $your_string_from_db); For instance, if you have this piece of code : $str = "My text1\nMy text2\nMy text3"; $arr = explode("\n", $str); var_dump($arr); You'd get this

How can I get the size of an array from a pointer in C?

匿名 (未验证) 提交于 2019-12-03 02:10:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I've allocated an "array" of mystruct of size n like this: if ( NULL == ( p = calloc ( sizeof ( struct mystruct ) * n , 1 ))) { /* handle error */ } Later on, I only have access to p , and no longer have n . Is there a way to determine the length of the array given just the pointer p ? I figure it must be possible, since free(p) does just that. I know malloc() keeps track of how much memory it has allocated, and that's why it knows the length; perhaps there is a way to query for this information? Something like... int length =

casting void** to 2D array of int - C

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: i am trying to cast a void** pointer to an int** 2D array in C here is the code that i am trying to work with (with all the extraneous bits removed): \*assume that i have a data structure called graph with some *element "void** graph" in it and some element "int order" */ void initialise_graph_data(graph_t *graph) { void **graph_data = NULL; int (*matrix)[graph->order]; size_t size = (graph->order * graph->order) * sizeof(int); graph_data = safe_malloc(size); /*safe malloc works fine*/ matrix = (int(*)[graph->order])graph_data; graph->graph

How do I initialize a member array with an initializer_list?

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm getting up to speed with C++0x, and testing things out with g++ 4.6 I just tried the following code, thinking it would work, but it doesn't compile. I get the error: incompatible types in assignment of ‘std::initializer_list<const int>’ to ‘const int [2]’ struct Foo { int const data[2]; Foo(std::initializer_list<int const>& ini) : data(ini) {} }; Foo f = {1,3}; 回答1: You can use a variadic template constructor instead of an initializer list constructor: struct foo { int x[2]; template <typename... T> foo(T... ts) : x{ts...} { // note the

How do I make a exact duplicate copy of an array?

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am a beginner in the Swift language. How would I make an exact duplicate of an array? I am having hard time finding information about duplicating an array in Swift. I tried using .copy() var originalArray = [1, 2, 3, 4] var duplicateArray = originalArray.copy() 回答1: Arrays have full value semantics in Swift, so there's no need for anything fancy. var duplicateArray = originalArray is all you need. If the contents of your array are a reference type, then yes, this will only copy the pointers to your objects. To perform a deep copy of the