这在C / C ++中非常简单,但我想知道Scala中最优雅的方式是什么?谢谢!
答案 0 :(得分:2)
考虑位移操作,例如移位和屏蔽,以提取Scala / Java Public Function GetXmlEnvironment(ByVal xmlTree As XmlNode, ByVal objUser As XTUser, ByVal objModule As XTModuleInfo, ByVal objProject As XTProject, ByVal objPage As IXTPage) As XmlNode
Dim objXml As XmlDocument = New XmlDocument()
Dim xmlEnvironment As XmlElement
xmlEnvironment = objXml.CreateElement("Environment")
If (xmlTree.HasChildNodes()) Then
xmlTree.FirstChild.AppendChild(xmlTree.OwnerDocument.ImportNode(xmlEnvironment, True))
End If
'...'
End Function
的上下字节(以与C / C ++非常相似的方式);让
Short
等等
val xs = (253 to 257).map(_.toShort).toList
xs: List[Short] = List(253, 254, 255, 256, 257)
其中每个元组收集每个def upper(x: Short) = ((x >> 8) & 0xFF).toByte
upper: (x: Short)Byte
def lower(x: Short) = (x & 0xFF).toByte
lower: (x: Short)Byte
xs.map( x => (upper(x), lower(x)) ).toArray
res2: Array[(Byte, Byte)] = List((0,-3), (0,-2), (0,-1), (1,0), (1,1))
值的上部和下部字节。
展平上面元组数组的一种方法,
Short
答案 1 :(得分:0)
在Scala中这很容易做到,事实上,您几乎可以将问题中的英语句子翻译成代码。假设您有一个名为List[Short]
的{{1}}:
l
显然,您需要确保列表中的所有l.map(_.toByte).toArray
实际上都符合Short
,否则您将获得截断的结果。