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

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

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>()

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