运行环境:jdk1.7、scala-2.10.4
package study
/**
* 数据结构操作:由于不可变类型,所以每次都需要新定义参数接收
*/
object Constunctor {
//使用lazy修修饰,懒加载
lazy val li2 = List(1, 2, 4, 100)
def main(args: Array[String]): Unit = {
val lis = new List2()
val tu = new Tuple2()
//元祖
//tu.getElemt
//List截取
lis.truncate()
//List添加指定位置元素
lis.addtest()
//操作map
lis.addmap()
//reduce\flodRigth\flodLeft操作
lis.reduceOrEqual
//添加元素和去重
lis.addElement()
//筛选
lis.choice()
//遍历list集合
lis.foreachElement()
}
//元组
class Tuple2() {
def getElemt(): Any = {
var tup = (12, 'a', "ds", List(21, 23, 11))
println("元祖第一个元素:" + tup._1)
var lis = tup._4
for (m <- 0 to lis.length - 1) println("元祖中的集合:" + lis(m))
}
}
//集合
class List2() {
//测试截取元素,前包后不包
def truncate(): Any = {
var tra = List(12, 2, 5, 21)//下标以0开始
println("截取元素,前包后不包:" + tra.slice(1, 3))
}
//操作::,添加元素到第一个位置
def addtest(): Any = {
var li2 = List(1, 2, 4)
li2 = 23 :: li2 //添加23到List集合的第一位中
li2 = li2.:+(12) // 添加元素到最后一位中
println("添加元素后的结果::-------->" + li2)
//组成一个新的List===List(List(23, 1, 2, 4 ,12), 21)
var le = li2 :: 21 :: Nil
println("添加List元素后:" + le)
}
//map操作
def addmap(): Any = {
val lis5 = List(1, 22, 3, 4)
val mp = lis5.map{ a => { a + 3}}//也是给每个元素加3
val mp2 = lis5.map(_ + 3) //匿名函数给lis5的每个元素加3
println("map操作后的结果:")
println("list1:"+mp)
println("list2:"+mp2)
}
//reduce和./()操作
def reduceOrEqual(): Any = {
/**
* (z /: it)(op) 自左向右在it所指元素的相邻元素间应用二元操作op,初始值为z。
(it :\ z)(op) 自右向左在it所指元素的相邻元素间应用二元操作op,初始值为z。
it.foldLeft(z)(op) 与(z /: it)(op)相同。
it.foldRight(z)(op) 与(it :\ z)(op)相同。
it reduceLeft op 自左向右对非空迭代器it所指元素的相邻元素间应用二元操作op。
it reduceRight op 自右向左对非空迭代器it所指元素的相邻元素间应用二元操作op。
*/
//(z /: it)(op)操作
val li4 = List(2, 6, 4)
val ss = li4./:(2)({
(m, n) => { println(m + "----->" + n+";") };
n
}
)
println("集合执行./(pa)后的返回结果:" + ss)
//foled操作
val liw = List(2, 5, 4, 1,6)
val res = liw.foldLeft(1)((m, n) => {
println(m + "------>" + n+";")
n
})
println("rigth操作")
val rigth=liw.foldRight(2)((m,n)=>{
println(m + "------>" + n+";")
m
})
println("集合执行 it.foldLeft(z)(op)后的返回结果:" + res)
println("集合执行 it.foldRight(z)(op)后的返回结果:" + rigth)
val redu = List(2, 3, 9,6,7)
//reduce操作
println("集合执行_+_后的结果:" + redu.reduce(_ + _))
println("集合执行_-_后的结果:" +redu.reduce(_ - _))
}
//添加元素
def addElement(): Any = {
val li3 = List(1, 2, 2, 1, 3, 3, 34)
val ll = li3.distinct
println("去重后的结果")
for (y <- ll) print(y + ",")
val a1 = li3.+:("23")
println("添加元素后的结果" + a1)
println("连续相加后的结果:" + li3.reduce(_ + _))
println("连续相减后的结果:" + li3.reduce(_ - _))
//添加list集合数据
var dataSet = List(List("2017-05-05 13:00:02","www.tudou12.com","134.04.50.22","2010-06-23 12:20:12",27592,9))
for(i <- 0 to 20){
dataSet=dataSet:::List(List("2017-03-05 13:00:02","www.tudou12.com","134.04.50.22","2010-06-23 12:20:12",2759+i,i))
}
dataSet.foreach(element=>println(element))
//打印结果:
// List(2017-05-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 27592, 9)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2759, 0)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2760, 1)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2761, 2)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2762, 3)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2763, 4)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2764, 5)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2765, 6)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2766, 7)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2767, 8)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2768, 9)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2769, 10)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2770, 11)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2771, 12)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2772, 13)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2773, 14)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2774, 15)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2775, 16)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2776, 17)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2777, 18)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2778, 19)
// List(2017-03-05 13:00:02, www.tudou12.com, 134.04.50.22, 2010-06-23 12:20:12, 2779, 20)
}
//多条件的筛选
def choice(): Any = {
li2.foreach { x => if (x % 2 == 0) if (x % 5 == 0) println("满足条件的值:" + x) }
}
//遍历
def foreachElement(): Any = {
println("第三个元素是:" + li2(2))
println()
li2.foreach { x => print("li2的元素是:" + x+";") }
}
}
}
结果:
截取元素,前包后不包:List(2, 5)
添加元素后的结果::-------->List(23, 1, 2, 4, 12)
添加List元素后:List(List(23, 1, 2, 4, 12), 21)
map操作后的结果:
list1:List(4, 25, 6, 7)
list2:List(4, 25, 6, 7)
2----->2;
2----->6;
6----->4;
集合执行./(pa)后的返回结果:4
1------>2;
2------>5;
5------>4;
4------>1;
1------>6;
rigth操作
6------>2;
1------>6;
4------>1;
5------>4;
2------>5;
集合执行 it.foldLeft(z)(op)后的返回结果:6
集合执行 it.foldRight(z)(op)后的返回结果:2
集合执行_+_后的结果:27
集合执行_-_后的结果:-23
去重后的结果
1,2,3,34,添加元素后的结果List(23, 1, 2, 2, 1, 3, 3, 34)
连续相加后的结果:46
连续相减后的结果:-44
满足条件的值:100
第三个元素是:4
li2的元素是:1;li2的元素是:2;li2的元素是:4;li2的元素是:100;