array

What is the need of collection framework in java?

匿名 (未验证) 提交于 2019-12-03 03:10:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: What is the need of Collection framework in Java since all the data operations(sorting/adding/deleting) are possible with Arrays and moreover array is suitable for memory consumption and performance is also better compared with Collections. Can anyone point me a real time data oriented example which shows the difference in both(array/Collections) of these implementations. 回答1: Arrays are not resizable. Java Collections Framework provides lots of different useful data types, such as linked lists (allows insertion anywhere in constant time),

Passing Java array to Scala

匿名 (未验证) 提交于 2019-12-03 03:10:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Although I've been using Scala for a while and have mixed it with Java before, I bumped on a problem. How can I pass a Java array to Scala? I know that the other way around is fairly straightforward. Java to Scala is not so however. Should I declare my method in Scala? Here is a small example of what I'm trying to achieve: Scala: def sumArray(ar: Array[Int]) = ... Java: RandomScalaClassName.sumArray(new int[]{1,2,3}); Is this possible? 回答1: absolutely! The Array[T] class in Scala is mapped directly to the Java type T[] . They both have

Searching a NSArray for the nearest number(s)

匿名 (未验证) 提交于 2019-12-03 03:10:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there an easy way to search an NSArray of numbers to find the nearest (or exact if it exists) matches to a user-input number? Say I have an array like this: 7, 23, 4, 11, 18, 2 , and the user enters 5 . The program returns the three nearest values in descending order of closeness: 4, 7, 2 , and most importantly gives the NSArray index of the three objects: 2, 0, 5 . 回答1: Update: see below for a better solution than my first one. Here's a solution using NSDictionary wrappers for each number and its index, with sorting using a comparator

Why implement a Hashtable with a Binary Search Tree?

匿名 (未验证) 提交于 2019-12-03 03:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: When implementing a Hashtable using an array, we inherit the constant time indexing of the array. What are the reasons for implementing a Hashtable with a Binary Search Tree since it offers search with O(logn)? Why not just use a Binary Search Tree directly? 回答1: If the elements don't have a total order (i.e. the "greater than" and "less than" is not be defined for all pairs or it is not consistent between elements), you can't compare all pairs, thus you can't use a BST directly, but nothing's stopping you from indexing the BST by the hash

How to insert a new element in between all elements of a Ruby array?

匿名 (未验证) 提交于 2019-12-03 03:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an Array and want to insert a new element in between all elements, someway like the join method. For example, I have [1, [], "333"] and what I need is [1, {}, [], {}, "333"] Note a new empty hash was inserted in between all elements. Edit: Currently what I have is: irb(main):028:0> a = [1, [], "333"] => [1, [], "333"] irb(main):029:0> a = a.inject([]){|x, y| x << y; x << {}; x} => [1, {}, [], {}, "333", {}] irb(main):030:0> a.pop => {} irb(main):031:0> a => [1, {}, [], {}, "333"] irb(main):032:0> I want to know the best way. 回答1: [1,

Swift Text File To Array of Strings

匿名 (未验证) 提交于 2019-12-03 03:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I was wondering what the simplest and cleanest to read a text file into an array of strings is in swift. Text file: line 1 line 2 line 3 line 4 Into an array like this: var array = ["line 1","line 2","line 3","line 4"] I would also like to know how to do a similar thing into struct like this: Struct struct{ var name: String! var email: String! } so take a text file and put it into struct's in an array. Thanks for the help! 回答1: First you must read the file: let text = String(contentsOfFile: someFile, encoding: NSUTF8StringEncoding, error:

“int *nums = {5, 2, 1, 4}” causes a segmentation fault

匿名 (未验证) 提交于 2019-12-03 03:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: int *nums = {5, 2, 1, 4}; printf("%d\n", nums[0]); causes a segfault, whereas int nums[] = {5, 2, 1, 4}; printf("%d\n", nums[0]); doesn't. Now: int *nums = {5, 2, 1, 4}; printf("%d\n", nums); prints 5. Based on this, I have conjectured that the array initialization notation, {}, blindly loads this data into whatever variable is on the left. When it is int[], the array is filled up as desired. When it is int*, the pointer is filled up by 5, and the memory locations after where the pointer is stored are filled up by 2, 1, and 4. So nums[0]

How do I convert json array to bash array of strings with jq?

匿名 (未验证) 提交于 2019-12-03 03:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: How do I parse a json array of objects to a bash array with those objects as strings? I am trying to do the following: CONVO = $ ( get_json_array | jq '.[]' ) for CONVERSATION in $CONVERSATIONS do echo "${CONVERSATION}" done But the echo prints out lines instead of the specific objects. The format of the object is: { "key1" : "value1" , "key2" : "value2" } and I need to pass it to an api: api_call '{ "key1":"value1", "key2": "value2"}' 回答1: The problem is that jq is still just outputting lines of text; you can't necessarily

Passing an empty array as default value of an optional parameter [duplicate]

匿名 (未验证) 提交于 2019-12-03 03:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This question already has an answer here: Setting the default value of a C# Optional Parameter 3 answers How does one define a function that takes an optional array with an empty array as default? public void DoSomething(int index, ushort[] array = new ushort[] {}, bool thirdParam = true) results in: Default parameter value for 'array' must be a compile-time constant. 回答1: You can't create compile-time constants of object references. The only valid compile-time constant you can use is null , so change your code to this: public void

Can I use the pipeline operator in F# to pass an argument to a constructor?

匿名 (未验证) 提交于 2019-12-03 03:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This code reverses a string: let reverse (s : string) = new string(s.ToCharArray() |> Array.rev) Can this be rewritten using the pipeline operator to pass the required argument to the string() constructor? For example, this seems more idiomatic: // Doesn't compile: let reverse (s : string) = s.ToCharArray() |> Array.rev |> new string Similarly, why can't I use the string operator in the following way? let reverse2 (s : string) = s.ToCharArray() |> Array.rev |> string Here it is in action: > reverse2 "foo" ;; val it : string = "System.Char[]"