在Scala中,字符串数组可以通过以下方式定义和操作:
val stringArray = Array("Hello", "World", "Scala")
stringArray.foreach(str => println(str))
stringArray :+ "New Element"
stringArray.length
stringArray(1) // 返回 "World"
stringArray.sorted
stringArray.filter(str => str.length > 3)
stringArray.mkString(", ")
val intArray = stringArray.map(_.toInt)
stringArray.map(_.toUpperCase)
stringArray.map(_.toLowerCase)
stringArray.toSet
stringArray.toList
stringArray.toVector
stringArray.toStream
stringArray.map(str => (str, str.length))
stringArray.map(str => (str, str.length, str.reverse))
val twoDimensionalArray = stringArray.map(str => str.split("").toArray)
val dictionary = stringArray.map(str => (str, str.length)).toMap
val tupleArray = stringArray.map(str => (str, str.length))
val arrayOfArrays = stringArray.map(str => str.split("").toArray)
val listOfLists = stringArray.map(str => str.split("").toList)
val vectorOfVectors = stringArray.map(str => str.split("").toVector)
val streamOfStreams = stringArray.map(str => str.split("").toStream)
val setOfSets = stringArray.map(str => str.split("").toSet)
val dictionaryOfDictionaries = stringArray.map(str => (str, str.length)).toMap
val tupleArrayOfTupleArrays = stringArray.map(str => (str, str.split("").toArray))
val twoDimensionalArrayOfArrays = stringArray.map(str => str.split("").toArray)
val twoDimensionalListOfLists = stringArray.map(str => str.split("").toList)
val twoDimensionalVectorOfVectors = stringArray.map(str => str.split("").toVector)
val twoDimensionalStreamOfStreams = stringArray.map(str => str.split("").toStream)
val twoDimensionalSetOfSets = stringArray.map(str => str.split("").toSet)
val twoDimensionalDictionaryOfDictionaries = stringArray.map(str => (str, str.split("").toMap))
val twoDimensionalTupleArrayOfTupleArrays = stringArray.map(str => (str, str.split("").toArray))
val threeDimensionalArrayOfArrays = stringArray.map(str => str.split("").map(char => char.toInt).toArray)
val threeDimensionalListOfLists = stringArray.map(str => str.split("").map(char => char.toInt).toList)
val threeDimensionalVectorOfVectors = stringArray.map(str => str.split("").map(char => char.toInt).toVector)
val threeDimensionalStreamOfStreams = stringArray.map(str => str.split("").map(char => char.toInt).toStream)
val threeDimensionalSetOfSets = stringArray.map(str => str.split("").map(char => char.toInt).toSet)
val threeDimensionalDictionaryOfDictionaries = stringArray.map(str => (str, str.split("").map(char => char.toInt).toMap))
val threeDimensionalTupleArrayOfTupleArrays = stringArray.map(str => (str, str.split("").map(char => char.toInt).toArray))
val fourDimensionalArrayOfArrays = stringArray.map(str => str.split("").map(char => char.toInt).map(int => int.toString).toArray)
val fourDimensionalListOfLists = stringArray.map(str => str.split("").map(char => char.toInt).map(int => int.toString).toList)
val fourDimensionalVectorOfVectors = stringArray.map(str => str.split("").map(char => char.toInt).map(int => int.toString).toVector)
val fourDimensionalStreamOfStreams = stringArray.map(str => str.split("").map(char => char.toInt).map(int => int.toString).toStream)
val fourDimensionalSetOfSets = stringArray.map(str => str.split("").map(char => char.toInt).map(int => int.toString).toSet)
val fourDimensionalDictionaryOfDictionaries = stringArray.map(str => (str, str.split("").map(char => char.toInt).map(int => int.toString).toMap))
val fourDimensionalTupleArrayOfTupleArrays = stringArray.map(str => (str, str.split("").map(char => char.toInt).map(int => int.toString).toArray))
val fiveDimensionalArrayOfArrays = stringArray.map(str => str.split("").map(char => char.toInt).map(int => int.toString).map(str => str.reverse).toArray)
val fiveDimensionalListOfLists = stringArray.map(str => str.split("").map(char => char.toInt).map(int => int.toString).map(str => str.reverse).toList)
val fiveDimensionalVectorOfVectors = stringArray.map(str => str.split("").map(char => char.toInt).map(int => int.toString).map(str => str.reverse).toVector)
val fiveDimensionalStreamOfStreams = stringArray.map(str => str.split("").map(char => char.toInt).map(int => int.toString).map(str => str.reverse).toStream)
val fiveDimensionalSetOfSets = stringArray.map(str => str.split("").map(char => char.toInt).map(int => int.toString).map(str => str.reverse).toSet)
val fiveDimensionalDictionaryOfDictionaries = stringArray.map(str => (str, str.split("").map(char => char.toInt).map(int => int.toString).map(str => str.reverse).toMap))
val fiveDimensionalTupleArrayOfTupleArrays = stringArray.map(str => (str, str.split("").map(char => char.toInt).map(int => int.toString).map(str => str.reverse).toArray))
val sixDimensionalArrayOfArrays = stringArray.map(str => str.split("").map(char => char.toInt).map(int => int.toString).map(str => str.reverse).map(str => str.length).toArray)
val sixDimensionalListOfLists = stringArray.map(str => str.split("").map(char => char.toInt).map(int => int.toString).map(str => str.reverse).map(str => str.length).toList)
val sixDimensionalVectorOfVectors = stringArray.map(str => str.split("").map(char => char.toInt).map(int => int.toString).map(str => str.reverse).map(str => str.length).toVector)
val sixDimensionalStreamOfStreams = stringArray.map(str => str.split("").map(char => char.toInt).map(int => int.toString).map(str => str.reverse).map(str => str.length).toStream)
val sixDimensionalSetOfSets = stringArray.map(str => str.split("").map(char => char.toInt).map(int => int.toString).map(str => str.reverse).map(str => str.length).toSet)
val sixDimensionalDictionaryOfDictionaries = stringArray.map(str => (str, str.split("").map(char => char.toInt).map(int => int.toString).map(str => str.reverse).map(str => str.length).toMap))
领取专属 10元无门槛券
手把手带您无忧上云