Article Directory
concept
scala Array concept is similar to Java, it can be used to store a set of data array. scala, there are two arrays, one is
定长数组
, the other is变长数组
Fixed-length arrays
concept
- Fixed-length array refers to an array of length is not allowed to change the
- Array elements is possible to change the
grammar
// 在scala中,数组的泛型使用`[]`来指定
// 使用`()`来获取元素
// 通过指定长度定义数组
val/var 变量名 = new Array[元素类型](数组长度)
// 用元素直接初始化数组
val/var 变量名 = Array(元素1, 元素2, 元素3...)
The sample code
//方式一
//创建
val a = new Array[Int](100)
//赋值
a(0) = 110
//输出
println(a(0))
//方式二
// 定义包含jave、scala、python三个元素的数组
val a = Array("java", "scala", "python")
//获取数组的长度
a.length
Variable-length arrays
concept
Refers to a variable-length array is variable length of the array, the array may be added to, deleted elements
definition
Create a variable-length arrays, you need to import the class in advance ArrayBuffer
import scala.collection.mutable.ArrayBuffer
grammar
//创建空的可变长度数组
val/var a = ArrayBuffer[元素类型]()
//创建带有初始元素的ArrayBuffer
val/var a = ArrayBuffer(元素1,元素2,元素3....)
The sample code
//导包
import scala.collection.mutable.ArrayBuffer
//定义一个长度为0的整型变长数组
val a = ArrayBuffer[Int]()
//定义一个包含"hadoop", "storm", "spark" 三个元素的变长数组
val a = ArrayBuffer("hadoop", "storm", "spark")
Add / modify / delete elements
- Use
+=
add elements- Use
-=
delete elements- Use
++=
append a variable-length array to array
The sample code
//往刚创建的 变长数组a 中添加 "flume" 这个元素
a += "flume"
// 删除一个元素
a -= "hadoop"
// 追加一个数组 该数组中包含 hive 和 sqoop 两个属性
a ++= Array("hive", "sqoop")
Iterate
Generally two ways
- Use
for表达式
the elements directly traverse the array- Use
索引
the elements to traverse the array
Direct traversal
The sample code
//定义一个数组
val a: Array[Int] =Array(1,2,3,4,5)
//直接遍历输出
for (i <- a ) {
println(i)
}
According to the index traversal
The sample code
//说明
//0 until n——生成一系列的数字,包含0,不包含n
//0 to n ——包含0,也包含n
//定义一个数组
val a: Array[Int] =Array(1,2,3,4,5)
//方式一
for (i <- 0 to a.length-1) {
println(i)
}
//方式二
for ( i <- 0 until a.length){
println(a(i));
}
//方式三 indices 获取索引
for (i <- a.indices) {
println(a(i));
}
An array of commonly used algorithms
Scala array package in some common computing operations, in the future when the data processing is not required to achieve our own again. The following is a few commonly used algorithms:
- --Sum summation method
- The method of selecting the maximum value --max
- Method for the minimum --min
- Sort --sorted method
Summing
Array
sum
methods can be accumulated for all the elements, then the results obtained
The sample code
val b: Array[Int] =Array(1,2,3,4,5,6,7,8,9)
println(b.sum)
Seeking maximum
An array of
max
methods for obtaining the maximum value of that element in the array
The sample code
//定义一个数组
val c: Array[Int] =Array(1,2,3,9,5,12,5,8,10)
//输出最大值
println(c.max)
For the minimum
The array of
min
methods for obtaining the minimum value of that element in the array
The sample code
//输出最小值
println(c.min)
Sequence
Array sorted
method, in ascending order of the array. And the reverse
method, the array may be reversed in order to achieve in descending order
The sample code
//定义一个数组
val c: Array[Int] =Array(1,2,3,9,5,12,5,8,10)
//升序
c.sorted
//降序
c.sorted.reverse