package com.example.a1701a
import android.content.Intent
import android.os.Build
import android.support.annotation.RequiresApi
import java.io.File
fun main(args: Array<String>) {
// basicLanguage()
// collection()
// tupleDemo()
// equals()
// range()
// switchDemo(2)
/*var a = 1..100
var list = a.toList()
println(sum(*intArrayOf(1,2,3,4,5))) //这里的*表示把数组展开
println(sum(*list.toIntArray()))*/
// printArea(2,5)
oprete()
}
private fun basicLanguage() {
var age: Int = 12
var price: Double = 45.0
var distance: Float = 78f
var flag: Boolean = true
var shortType: Short = 45
var longType: Long = 99
var byteType: Byte = 12
var charType: Char = 'a'
arrayOf(12, 13, 13, "jijj")
// intArrayOf(1,2,3,"jijij")
age = 15
val name = "yuhui"
// name = "gaoyang"
val arrayOf = arrayOf("a", "b", "b", "e")
for (s in arrayOf) {
println(s)
}
var i: Int = 0
while (i < arrayOf.size) {
val get = arrayOf.get(i)
i++
println(get)
}
val nu:String? = "java"
nu!!.take(1)
println("kotlin")
}
@RequiresApi(Build.VERSION_CODES.N)
fun collection(){
/* val setConllection:Set<String> = setOf<String>("java", "android", "C++", "kotlin")
*//* for (s in setConllection) {
println(s)
}*//*
*//*val iterator = setConllection.iterator();
while (iterator.hasNext()) {
println(iterator.next())
}*//*
setConllection.forEach {
println(it)
}*/
var listOf = listOf<String>("a", "b", "c")
listOf.forEach {
println(it)
}
for (s in listOf) {
println(s)
}
val mutableSetOf = mutableSetOf<String>("hello,word")
mutableSetOf.add("java")
val mutableListOf = mutableListOf<String>("a2", "b2", "c2")
mutableListOf.add("hello")
mutableListOf.forEach {
println(it)
}
mapOf<String,String>("苹果" to "ipone8","华为" to "mate10")
val mutableMapOf = mutableMapOf<String, String>(Pair("苹果", "ipone8"), Pair("华为", "mate10"))
var desc = ""
mutableMapOf.forEach {
key, value -> desc="${desc}厂家:${key},名称:${value}\n"
println(desc)
}
}
fun tupleDemo(){
val 课程 = Pair<String,String>("android", "Java")
val 字母 = Triple("a", "b", "c")
println(课程.first+","+ 课程.second)
println(字母.first+","+字母.second+","+ 字母.third)
}
fun nullable(){
var address:String? = null
// address.take(2)
}
fun equals(){
val file1 = File("d:"+File.separator+"PS")
val file2 = File("d:"+File.separator+"PS")
println(file1 === file2)
println(file1 == file2)
}
fun range(){
val 一到一百 = 1..100
/*一到一百.forEach {
println(it)
}*/
/*val 一到一百的奇数 = 一到一百.step(2)
一到一百的奇数.forEach {
println(it)
}*/
val 字母 = 'a'..'z'
字母.forEach {
println(it)
}
/*val a是否在字母中 = 'a' in 字母
print("A是否在字母中:"+ a是否在字母中)*/
/* val 一百到一 = (1..100).reversed()
一百到一.forEach {
println(it)
}*/
/* val 倒计时 = 10.downTo(0)
倒计时.forEach {
Thread.sleep(1000)
println(it)
}*/
val 一百到二百 = 100.rangeTo(200)
一百到二百.forEach {
println(it)
}
}
fun switchDemo(num :Int ){
/* when (num) {
10,11,22 -> println("是0,1,2中的一个")
12 -> println("是1")
in 100..1100 -> println("输入的数字是在1到100之间")
in arrayOf(11,12,13,41) -> println("在数组中")
is Int -> println("是int类型")
else -> println("无结果")
}*/
when{
num < 1000 -> println("输入的参数小于1000")
num > 10 ->println("输入的参数大于10")
else -> println("无结果")
}
}
fun stringDemo() {
var courseName = "1701a开发"
println(courseName.length)
println(courseName.count())
println(courseName.isEmpty())
println(courseName.first())
println(courseName.last())
println(courseName.elementAt(2))
println(courseName.substring(2,4))
println(courseName.indexOf("1",2))
println(courseName.contains("1"))
println(courseName.equals("abc"))
println(courseName.contentEquals("abc"))
println(courseName == "abc")
println(courseName === "abc")
println(courseName.drop(2))
println(courseName.dropLast(2))
println(courseName.dropWhile { it.isWhitespace() || it.equals("abc")})
println(courseName.take(2))
println(courseName.takeLast(2))
println(courseName.replace("a","b"))
val toCharArray = courseName.toCharArray();
}
fun suare(r:Double):Double{
return Math.PI * r *r
}
fun sum(vararg arg:Int):Int{//vararg表示可变参数
var total = 0
for (x in arg) {
total += x;
}
return total
}
fun printArea(width:Int ,height:Int){
fun area(w:Int,h:Int):Int{//本地函数,又叫做嵌套函数
return w * h;
}
val area = area(width,height);
println("面积= $area")
}
fun printAreaUpdate(width:Int ,height:Int){
fun area() = width * height
val area = area()
println("面积= $area")
}
fun nameParam() {
val book = "基于kotlin的Android"
delFiles("hello",true,true,true)
delFiles(innoreCase= true,ext="hello",includeDirs= true,recursive=true)//命名参数的好处可以更换参数的调用位置
println(book.regionMatches(9,"深入Android",2,7,true))
}
fun delFiles(ext:String,innoreCase:Boolean,includeDirs:Boolean,recursive:Boolean){
valueOf(start = 2)
valueOf(1,end = 8)
}
fun valueOf(start:Int,end:Int= 100){//默认 参数
}
fun oprete() {//操作符
// 1+2;
println(1.plus(2))
val m = Matrix(8,8,8,8)
val n = Matrix(9,9,9,9)
val mn = m + n;
println("矩阵相加的结果是:$mn")
val mn2 = m.plus(n)
println("矩阵相加的结果是:$mn2")
/*!x x.not()
-y y.unaryMinus()*/
val boo = true
boo.not();
val num = 3
num.unaryMinus()
val numbers = arrayOf(5,6,7,8)
val a = 6 !in numbers
val b = !numbers.contains(6)
val i = numbers[0]
val get = numbers.get(0)
numbers.set(0,8)
}
class Matrix(val a: Int, val b: Int, val c: Int, val d: Int){
operator fun plus(m:Matrix):Matrix{
return Matrix(a + m.a,b + m.b,c+m.c,d+m.d)
}
}
class Matrix2(var a: Int, var b: Int, var c: Int, var d: Int){
operator fun plus(m:Matrix2):Matrix2{
return Matrix2(a + m.a,b + m.b,c+m.c,d+m.d)
}
operator fun set(horizontal: Int, vertical: Int, value: Int) {
val point = Pair(horizontal,vertical)
when(point){
Pair(0,0) -> this.a = value
Pair(0,1) -> this.b = value
Pair(1,0) -> this.c = value
Pair(1,1) -> this.d = value
else -> return
}
}
operator fun get(horizontal:Int,vertical: Int):Int{
val point = Pair(horizontal,vertical)
when(point){
Pair(0,0) -> return a
Pair(0,1) -> return b
else -> return 0
}
}
}
object MinValue{
operator fun invoke(a: Int,b: Int) = if(a > b ) a else b
operator fun invoke(x:Int ,y:Int,z:Int) = invoke(invoke(x,y),z)
}