java 数组的方法_数组常用方法
[TOC]
# 定義
~~~
//初始化一個長度為8的定長數組,其所有元素均為0
val arr1 = new Array[Int](8)
//直接打印定長數組,內容為數組的hashcode值
println(arr1)
//將數組轉換為數組緩沖,就可以看到原數組的內容了
//toBuffer會將數組轉換為長數組緩沖
println(arr1.toBuffer)
//注意: 如果new,相當于調用了數組的apply方法,直接為數組賦值
//初始化一個長度為1的定長數組
val arr2 = Array[Int](10)
println(arr1.toBuffer)
//定義一個長度為3的定長數組
val arr3 = Array("hadoop", "storm", "spark")
//使用()來訪問元素
println(arr3(2))
~~~
# 賦值及取值
~~~
//等價于 arr2(1)=10
scala> arr2.update(1,10)
scala> arr2
res6: Array[Int] = Array(1, 10, 3)
~~~
也可以通過apply來取值
~~~
scala> arr2.apply(1)
res9: Int = 10
~~~
# 變長數組
需要打包
~~~
# _表示導入這個包下的所有
scala> import scala.collection.mutable._
import scala.collection.mutable._
scala> val ab = new ArrayBuffer[Int]()
ab: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala> val arrBuffer1 = scala.collection.mutable.ArrayBuffer[Int](10,9,8)
arrBuffer1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 9, 8)
~~~
## 追加元素
~~~
# 向尾部追加
scala> ab += 1
res0: ab.type = ArrayBuffer(1)
scala> ab += (3, 4, 5)
res1: ab.type = ArrayBuffer(1, 3, 4, 5)
# 追加一個數組緩沖
scala> ab ++= Array(6, 7)
res2: ab.type = ArrayBuffer(1, 3, 4, 5, 6, 7)
# 追加一個數組緩沖
scala> ab ++= ArrayBuffer(8, 9)
res3: ab.type = ArrayBuffer(1, 3, 4, 5, 6, 7, 8, 9)
# 向尾部追加
ab.append(123)
~~~
指定位置插入用insert
~~~
# 在0這個位置插入-1和0
ab.insert(0, -1, 0)
~~~
會生成一個新的數組
~~~
scala> ab :+ 99
res14: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(123, 3, 99)
scala> ab
res15: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(123, 3)
~~~
## 刪除
~~~
scala> ab
res5: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(-1, 0, 1, 3, 4, 5, 6, 7, 8, 9)
# 刪除數組某個位置的元素
# 在3這個位置移除2個
scala> ab.remove(3, 2)
scala> ab
res7: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(-1, 0, 1, 5, 6, 7, 8, 9)
~~~
## 定長數組和變長數組的轉換
~~~
scala> arr2.toBuffer
res7: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 10, 3)
scala> arr2.toArray
res8: Array[Int] = Array(1, 10, 3)
~~~
# 排序
~~~
scala> val arr = Array(1, 9, 5, 7, 3, 2)
arr: Array[Int] = Array(1, 9, 5, 7, 3, 2)
~~~
**升序**
~~~
scala>arr.sorted
res0: Array[Int] = Array(1, 2, 3, 5, 7, 9)
~~~
**降序**
~~~
scala> arr.sorted.reverse
res2: Array[Int] = Array(9, 7, 5, 3, 2, 1)
~~~
~~~
scala> arr.sortWith(_>_)
res1: Array[Int] = Array(9, 7, 5, 3, 2, 1)
~~~
也可以這樣寫
~~~
scala> arr.sortWith( (x, y) => x > y )
res3: Array[Int] = Array(9, 7, 5, 3, 2, 1)
~~~
**轉換后排序**
~~~
scala> val arr = Array(1,2,3,4,10,100)
arr: Array[Int] = Array(1, 2, 3, 4, 10, 100)
scala> arr.sortBy(x => x+"")
res0: Array[Int] = Array(1, 10, 100, 2, 3, 4)
~~~
# 數組轉換
yield關鍵字將原始的數組進行轉換會產生一個新的數組,原始的數組不變
~~~
scala> var arr = Array(1, 2, 3, 4, 5, 6, 7)
arr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7)
scala> val res = for(e
res: Array[Int] = Array(2, 4, 6, 8, 10, 12, 14)
scala> arr.map(_ * 2)
res0: Array[Int] = Array(2, 4, 6, 8, 10, 12, 14)
scala> arr
res1: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7)
~~~
# 過濾
偶數乘以10
~~~
scala> arr.filter(_ % 2 == 0).map(_ * 10)
res2: Array[Int] = Array(20, 40, 60)
~~~
# 常用方法
**求和**
~~~
scala> arr
res3: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7)
scala> arr.sum
res4: Int = 28
//reduce匯總
scala> arr.reduce(_+_)
res5: Int = 28
~~~
**給初始值求和**
~~~
scala> arr.fold(10)(_+_)
res1: Int = 38
~~~
**求最大值**
~~~
arr.max
~~~
**切分和壓平**
flatten和map()
~~~
scala> val a = Array("a b c", "d e f", "h i j")
a: Array[String] = Array(a b c, d e f, h i j)
scala> a.map(_.split(" "))
res1: Array[Array[String]] = Array(Array(a, b, c), Array(d, e, f), Array(h, i, j))
scala> a.flatMap(_.split(" "))
res4: Array[String] = Array(a, b, c, d, e, f, h, i, j)
~~~
# 轉換為String
~~~
scala> val arr2 = Array(1,2,3)
arr2: Array[Int] = Array(1, 2, 3)
scala> arr2.mkString(",")
res4: String = 1,2,3
~~~
# 多維數組
## 定義
~~~
scala> val arr4 = Array.ofDim[Int](3, 4)
arr4: Array[Array[Int]] = Array(Array(0, 0, 0, 0), Array(0, 0, 0, 0), Array(0, 0, 0, 0))
scala> arr4.mkString(" ")
res16: String = [I@22b8eaf0 [I@50dfceff [I@26a5007
scala> for(x
0,0,0,0
0,0,0,0
0,0,0,0
~~~
賦值
~~~
scala> arr4(1)(2) = 1
~~~
# 與java數組互相轉換
scala數組轉java數組
~~~
import scala.collection.mutable.ArrayBuffer
val arr4 = ArrayBuffer("1", "2", "3")
// scala to java
import scala.collection.JavaConversions.bufferAsJavaList
val javaArr = new ProcessBuilder(arr4)
println(javaArr.command())
~~~
java數組轉scala數組
~~~
import scala.collection.mutable.ArrayBuffer
val arr4 = ArrayBuffer("1", "2", "3")
//_表示導入這個包下的所有
import scala.collection.JavaConversions._
import scala.collection.mutable.Buffer
// scala to java
val javaArr = new ProcessBuilder(arr4)
//java to scala
val scalaArr: Buffer[String] = javaArr.command()
println(scalaArr)
~~~
總結
以上是生活随笔為你收集整理的java 数组的方法_数组常用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java entries_Java le
- 下一篇: java 调用远程服务_java调用(请