Foreword
Hello, my dear friends, we also meet in this article, I want you could solve some problems, you should insist to learn knowledge, then you will better than before. Only through study could make us let out of stupid or fool.
Array
We need create a file named is day2.scala, put the below codes in this file.
import java.math.BigIntegerobject day2 {def main(args:Array[String]): Unit = {System.out.println("Scala is make me happy!!");Array_operation();}def Array_operation(): Unit = {val big = new BigInteger("12345");//create object and setting itself typeSystem.out.println("The object big: "+big+"\n");val strings = new Array[String](4);strings(0) = "Scala";strings(1) = " is ";strings(2) = "make me ";strings(3) = "happy!!\n\n";for (n <- 0 to 3){System.out.print(strings(n));}System.out.print("Update function: ");val String2 = new Array[String](4);String2.update(0,"Scala ");String2.update(1,"is ");String2.update(2,"make me ");String2.update(3,"happy!!");for (m <- 0.to(3)){System.out.print(String2.apply(m))}System.out.println("\n")String2(3) = "happy";String2.update(0,"C++ ");System.out.print(String2(0));System.out.print(String2(1));System.out.print(String2(2));System.out.print(String2(3));}
}
Output result:
From the above program, we know Scala could translate object to parameter, it’s scala function coding style, it’s differet from java and python. We defined Array
need point itself type
and itself length
, val
defined object is immutable, like the below picture:
we also could use update function corresponding Array position to assign value to element. Though val
is immutable, Array Object
is variable type, val haven’t effect the Array. Also change Array element could use Subscript
to achieve or through update()
function to achieve.
List
we look the number type list at first. Like the below codes.
def List_operation(): Unit = {System.out.println("\n");var num_list = List(1,2,3,4,5);System.out.println("num_list:"+num_list);var num_list2 = List(2,9,4,8,7);System.out.println("num_list2:"+num_list2);num_list = num_list2;System.out.println("change num_list:"+num_list);System.out.println("num_list(3):"+num_list(3));//return list subscript is 3 elementval List_num3 = 12::54::87::Nil;System.out.println("List_num3:"+List_num3);val Null_list = List();val Null_list2 = Nil;System.out.println("Null_list:"+Null_list+"\nNull_list2:"+Null_list2);val combine_list = num_list ::: num_list2;System.out.println("Combine_list:"+combine_list);}
Output Result:
From the above program, we know List Object
could be signed value【List】, it could through subscript gain the corresponding element, it could be created by ::
and Nil as end, because Int type haven't :: function
, so you must do like it. Define empty List have two ways, one is use ::
, anthor is List()
to achieve it. List could combine and themselves same elements haven’t disapper. List couldn’t change itself element like the below pictures:
Now! we look the character type List.
def List_operation(): Unit = {System.out.println("\n");var num_list = List(1,2,3,4,5);System.out.println("num_list:"+num_list);var num_list2 = List(2,9,4,8,7);System.out.println("num_list2:"+num_list2);num_list = num_list2;System.out.println("change num_list:"+num_list);System.out.println("num_list(3):"+num_list(3));//return list subscript is 3 elementval List_num3 = 12::54::87::Nil;System.out.println("List_num3:"+List_num3);val Null_list = List();val Null_list2 = Nil;System.out.println("Null_list:"+Null_list+"\nNull_list2:"+Null_list2);val combine_list = num_list ::: num_list2;System.out.println("Combine_list:"+combine_list);val string_List = List("Scala","Java","C++","R","Julia","Python","C","Pascal");System.out.println("The Computer Program Language:"+string_List);var Count = string_List.count(ch => ch.length == 6);val Forward_separate_list = string_List.drop(2);System.out.println("Forward_separate_List:"+Forward_separate_list);val backward_separate_list = string_List.dropRight(2);System.out.println("Backward_separate_list:"+backward_separate_list);val judge_element = string_List.exists(s => s.toString == "Java");System.out.println("Judge_element:"+judge_element);val judge_list = string_List.filter(chr => chr.length == 5);System.out.println("Judge_list:"+judge_list);val first_element = string_List.headSystem.out.println("First_element:"+first_element);System.out.println("Last_element:"+string_List.last)val pop_last_element_list = string_List.init;System.out.println("pop_list:"+pop_last_element_list);System.out.println("Judge List is Empty:"+string_List.isEmpty);System.out.println("Count the list elements:"+string_List.length);System.out.println("Return length isn't 1 elements list:"+string_List.filterNot(S => S.length == 1));System.out.println("Return except first element others elements component list:"+string_List.tail);}
Output Result:
About the Codes explain in the scholiums, also it has other function, you could learn with yourself.
The article Codes like the below exhibition.
import java.math.BigIntegerobject day2 {def main(args:Array[String]): Unit = {System.out.println("Scala is make me happy!!");Array_operation();List_operation();}def Array_operation(): Unit = {val big = new BigInteger("12345");//create object and setting itself typeSystem.out.println("The object big: "+big+"\n");val strings = new Array[String](4);strings(0) = "Scala";strings(1) = " is ";strings(2) = "make me ";strings(3) = "happy!!\n\n";for (n <- 0 to 3){System.out.print(strings(n));}System.out.print("Update function: ");val String2 = new Array[String](4);String2.update(0,"Scala ");String2.update(1,"is ");String2.update(2,"make me ");String2.update(3,"happy!!");for (m <- 0.to(3)){System.out.print(String2.apply(m))}System.out.println("\n")String2(3) = "happy";String2.update(0,"C++ ");System.out.print(String2(0));System.out.print(String2(1));System.out.print(String2(2));System.out.print(String2(3));}def List_operation(): Unit = {System.out.println("\n");var num_list = List(1,2,3,4,5);System.out.println("num_list:"+num_list);var num_list2 = List(2,9,4,8,7);System.out.println("num_list2:"+num_list2);num_list = num_list2;System.out.println("change num_list:"+num_list);System.out.println("num_list(3):"+num_list(3));//return list subscript is 3 elementval List_num3 = 12::54::87::Nil;System.out.println("List_num3:"+List_num3);val Null_list = List();val Null_list2 = Nil;System.out.println("Null_list:"+Null_list+"\nNull_list2:"+Null_list2);val combine_list = num_list ::: num_list2;System.out.println("Combine_list:"+combine_list);val string_List = List("Scala","Java","C++","R","Julia","Python","C","Pascal");System.out.println("The Computer Program Language:"+string_List);var Count = string_List.count(ch => ch.length == 6);val Forward_separate_list = string_List.drop(2);System.out.println("Forward_separate_List:"+Forward_separate_list);val backward_separate_list = string_List.dropRight(2);System.out.println("Backward_separate_list:"+backward_separate_list);val judge_element = string_List.exists(s => s.toString == "Java");System.out.println("Judge_element:"+judge_element);val judge_list = string_List.filter(chr => chr.length == 5);System.out.println("Judge_list:"+judge_list);val first_element = string_List.headSystem.out.println("First_element:"+first_element);System.out.println("Last_element:"+string_List.last)val pop_last_element_list = string_List.init;System.out.println("pop_list:"+pop_last_element_list);System.out.println("Judge List is Empty:"+string_List.isEmpty);System.out.println("Count the list elements:"+string_List.length);System.out.println("Return length isn't 1 elements list:"+string_List.filterNot(S => S.length == 1));System.out.println("Return except first element others elements component list:"+string_List.tail);}
}
In the end
I gratitude for your watch my articles, I hope you could point mistakes in my passages, I very thanks for you, maybe today is terrible, tomorrow also terrible, but you insist to last, you will stronger than before. Cheers!! my dear friends, your Pan【One of common men】.