## Creating Array using JSTL or EL

If you’re already on EL 3.0 (Tomcat 8+, WildFly 8+, GlassFish 4+, Payara 4+, TomEE 7+, etc), which supports new operations on collection objects, you can use \${[…]} syntax to construct a list, and \${{…}} syntax to construct a set. <c:set var=”alphabet” value=”\${[‘A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’,’I’,’J’,’K’,’L’,’M’,’N’,’O’,’P’,’Q’,’R’,’S’,’T’,’U’,’V’,’W’,’X’,’Y’,’Z’]}” scope=”application” /> If you’re not on EL 3.0 yet, use the \${fn:split()} … Read more

## Maximum sum of non consecutive elements

Dynamic programming? Given an array A[0..n], let M(i) be the optimal solution using the elements with indices 0..i. Then M(-1) = 0 (used in the recurrence), M(0) = A[0], and M(i) = max(M(i – 1), M(i – 2) + A[i]) for i = 1, …, n. M(n) is the solution we want. This is O(n). … Read more

## C++ vector of char array

You cannot store arrays in vectors (or in any other standard library container). The things that standard library containers store must be copyable and assignable, and arrays are neither of these. If you really need to put an array in a vector (and you probably don’t – using a vector of vectors or a vector … Read more

## Get random elements from array in Swift

Xcode 11 • Swift 5.1 extension Collection { func choose(_ n: Int) -> ArraySlice<Element> { shuffled().prefix(n) } } Playground testing var alphabet = [“A”,”B”,”C”,”D”,”E”,”F”,”G”,”H”,”I”,”J”,”K”,”L”,”M”,”N”,”O”,”P”,”Q”,”R”,”S”,”T”,”U”,”V”,”W”,”X”,”Y”,”Z”] let shuffledAlphabet = alphabet.shuffled() // “O”, “X”, “L”, “D”, “N”, “K”, “R”, “E”, “S”, “Z”, “I”, “T”, “H”, “C”, “U”, “B”, “W”, “M”, “Q”, “Y”, “V”, “A”, “G”, “P”, “F”, “J”] … Read more

## How to compare content of two arrays?

You can do array differences, if the result is the empty array, the 2 arrays contained the same elements: >> [1,2,3]-[3,1,2] #=> [] If you still have elements left, then not all elements of the first array were present in the second one: >> [1,2,5]-[3,1,2] #=> [5]

## add elements to object array

You can try Subject[] subjects = new Subject[2]; subjects[0] = new Subject{….}; subjects[1] = new Subject{….}; alternatively you can use List List<Subject> subjects = new List<Subject>(); subjects.Add(new Subject{….}); subjects.Add(new Subject{….}); // Then you can convert the List to Array like below: Subject[] arraySubjects = subjects.ToArray<Subject>()

## Best way to convert string to array of object in javascript?

I think that the best way of doing this, as Douglas Crockford (one of the biggests gurus of JavaScript) suggests in here is using the JSON native parser, as it is not only faster than the eval(), it’s also more secure. Native JSON parser is already available in: Firefox 3.5+ IE 8+ Opera 10.5+ Safari … Read more

## How do I insert an element at the correct position into a sorted array in Swift?

Here is a possible implementation in Swift using binary search (from http://rosettacode.org/wiki/Binary_search#Swift with slight modifications): extension Array { func insertionIndexOf(_ elem: Element, isOrderedBefore: (Element, Element) -> Bool) -> Int { var lo = 0 var hi = self.count – 1 while lo <= hi { let mid = (lo + hi)/2 if isOrderedBefore(self[mid], elem) { … Read more

## Appending string to Matlab array

You need to use cell arrays. If the number of iterations are known beforehand, I suggest you preallocate: N = 10; names = cell(1,N); for i=1:N names{i} = ‘string’; end otherwise you can do something like: names = {}; for i=1:10 names{end+1} = ‘string’; end