函数
多元数组:
def main(args:Array[String]){
var triple = (100, "scala", "spark");
println(triple._1);
println(triple._2);
println(triple._3);
}
map:
def main(args:Array[String]) {
var ages = Map("rocky" -> 27, "Spark" -> 5)
for((k,v) <- ages) {
println("Key is " + k + ", value is " + v);
}
println("\n");
for((k,_) <- ages) {
println("key is " + k);
}
}
file:
def main(args : Array[String]) {
// var file = Source.fromFile("/Users/hujh/Documents/1.txt");
var file = Source.fromURL("http://www.baidu.com/");
for(line <- file.getLines()){
println(line);
}
}
Array:
def main(args:Array[String]) {
var array = Array(1,2,3,4,5);
for(i <- 0 until array.length) {
println(array(i));
}
for(elem <- array) {
println(elem);
}
}
package com.github.scala.hello
object funtion {
def main(args : Array[String]) {
// for( i <- 1 to 2; j <- 1 to 2) println( (i*100 +j) + " " )
// println;
//
// for( i <- 1 to 2; j <- 1 to 2 if i!=j) println( (i*100 +j) + " " )
// println;
// def addA(x : Int) = x + 100;
// def add = (x : Int) => x + 200;
// println( "addA(2) result:" + addA(2));
// println( " add(2) result:" + add(2));
// def fac(n:Int):Int = if(n<=0) 1 else n*fac(n-1);
// println(" the result is:" + fac(10));
// def combile(content:String, left:String = "[", right:String = "]") = left + content + right;
// println(combile("hello scala", "<<", ">>"));
def connected(args:Int*) = {
var result = 0;
for(arg <- args) result += arg;
result;
}
println("the result is " + connected(1,2,3,4,5));
}
}
lazy var file = Source.fromFile("d:\ddd.txt"); // 文件不存在
当有lazy时,执行该句是不报错,第一次真正使用时会报错,而没有lazy时,执行该句时报错。