当前位置: 首页 > news >正文

咨询管理公司seocui cn

咨询管理公司,seocui cn,上海网站优化推广,德宏企业网站建设公司6基本类型 声明变量 val(value的简写)用来声明一个不可变的变量,这种变量在初始赋值之后就再也不能重新赋值,对应Java中的final变量。 var(variable的简写)用来声明一个可变的变量,这种变量在初始…

基本类型

在这里插入图片描述

声明变量

在这里插入图片描述
val(value的简写)用来声明一个不可变的变量,这种变量在初始赋值之后就再也不能重新赋值,对应Java中的final变量。
var(variable的简写)用来声明一个可变的变量,这种变量在初始赋值之后仍然可以再被重新赋值,对应Java中的非final变量。

类型自动推导

kotlin还能对我们的声明的变量进行类型的自动推导:
在这里插入图片描述

易混淆的Long类型标记

在这里插入图片描述

Kotlin的数值类型转换

在这里插入图片描述

无符号类型

目的是为了兼容C
在这里插入图片描述

Kotlin的字符串

在这里插入图片描述


fun main() {var a = 2val b = "Hello Kotlin"//    val c = 12345678910l // compile error.val c = 12345678910L // okval d = 3.0 // Double, 3.0f Floatval e: Int = 10//val f: Long = e // implicitness not allowedval f: Long = e.toLong() // implicitness not allowedval float1: Float = 1fval double1 = 1.0val g: UInt = 10uval h: ULong = 100000000000000000uval i: UByte = 1uprintln("Range of Int: [${Int.MIN_VALUE}, ${Int.MAX_VALUE}]")println("Range of UInt: [${UInt.MIN_VALUE}, ${UInt.MAX_VALUE}]")val j = "I❤️China"println("Value of String 'j' is: $j") // no need bracketsprintln("Length of String 'j' is: ${j.length}") // need bracketsSystem.out.printf("Length of String 'j' is: %d\n", j.length)val k = "Today is a sunny day."val m = String("Today is a sunny day.".toCharArray())println(k === m) // compare references.println(k == m) // compare values.val n = """<!doctype html><html><head><meta charset="UTF-8"/><title>Hello World</title></head><body><div id="container"><H1>Hello World</H1><p>This is a demo page.</p></div></body></html>""".trimIndent()println(n)
}

public class JavaBasicTypes {public static void main(String... args) {int a = 2;final String b = "Hello Java";long c = 12345678910l; // ok but not good.long d = 12345678910L; // okint e = 10;long f = e; // implicit conversion// no unsigned numbers.String j = "I❤️China";System.out.println("Value of String 'j' is: " + j);System.out.println("Length of String 'j' is: " + j.length());System.out.printf("Length of String 'j' is: %d\n",  j.length());String k = "Today is a sunny day.";String m = new String("Today is a sunny day.");System.out.println(k == m); // compare references.System.out.println(k.equals(m)); // compare values.String n = "<!doctype html>\n" +"<html>\n" +"<head>\n" +"    <meta charset=\"UTF-8\"/>\n" +"    <title>Hello World</title>\n" +"</head>\n" +"<body>\n" +"    <div id=\"container\">\n" +"        <H1>Hello World</H1>\n" +"        <p>This is a demo page.</p>\n" +"    </div>\n" +"</body>\n" +"</html>";System.out.println(n);}
}

数组Array

数组的创建

数组的长度

在这里插入图片描述

数组的读写

数组的遍历

在这里插入图片描述

数组的包含关系


fun main() {val a = IntArray(5)println(a.size) //same with the Collections(e.g. List)val b = ArrayList<String>()println(b.size)val c0 = intArrayOf(1, 2, 3, 4, 5)val c1 = IntArray(5){ 3 * (it + 1) } // y = 3*(x + 1)println(c1.contentToString())val d = arrayOf("Hello", "World")d[1] = "Kotlin"println("${d[0]}, ${d[1]}")val e = floatArrayOf(1f, 3f, 5f, 7f)for (element in e) {println(element)}e.forEach {println(it)}if(1f in e){println("1f exists in variable 'e'")}if(1.2f !in e){println("1.2f not exists in variable 'e'")}}
import java.util.ArrayList;public class JavaArrays {public static void main(String... args) {int[] a = new int[5];System.out.println(a.length);// only array use 'length'ArrayList<String> b = new ArrayList<>();System.out.println(b.size());int[] c = new int[]{1, 2, 3, 4, 5};String[] d = new String[]{"Hello", "World"};d[1] = "Java";System.out.println(d[0] + ", " + d[1]);float[] e = new float[]{1, 3, 5, 7};for (float element : e) {System.out.println(element);}for (int i = 0; i < e.length; i++) {System.out.println(e[i]);}// Test in an Arrayfor (float element : e) {if(element == 1f){System.out.println("1f exists in variable 'e'");break;}}//Test not in an Arrayboolean exists = false;for (float element : e) {if(element == 1.2f){exists = true;break;}}if(!exists){System.out.println("1.2f not exists in variable 'e'");}}
}

区间

区间的创建

闭区间

在这里插入图片描述

开区间

在这里插入图片描述

倒序区间

在这里插入图片描述

区间的步长

在这里插入图片描述

区间的迭代

在这里插入图片描述

区间的包含关系

在这里插入图片描述

区间的应用

在这里插入图片描述

fun main() {val intRange = 1..10 // [1, 10]val charRange = 'a'..'z'val longRange = 1L..100Lval floatRange = 1f .. 2f // [1, 2]val doubleRange = 1.0 .. 2.0println(intRange.joinToString())println(floatRange.toString())val uintRange = 1U..10Uval ulongRange = 1UL..10ULval intRangeWithStep = 1..10 step 2val charRangeWithStep = 'a'..'z' step 2val longRangeWithStep = 1L..100L step 5println(intRangeWithStep.joinToString())val intRangeExclusive = 1 until 10 // [1, 10)val charRangeExclusive = 'a' until 'z'val longRangeExclusive = 1L until 100Lprintln(intRangeExclusive.joinToString())val intRangeReverse = 10 downTo 1 // [10, 9, ... , 1]val charRangeReverse = 'z' downTo 'a'val longRangeReverse = 100L downTo 1Lprintln(intRangeReverse.joinToString())for (element in intRange) {println(element)}intRange.forEach {println(it)}if (3.0 !in doubleRange) {println("3 in range 'intRange'")}if (12 !in intRange) {println("12 not in range 'intRange'")}val array = intArrayOf(1, 3, 5, 7)for (i in 0 until array.size) {println(array[i])}for(i in array.indices){println(array[i])}
}

集合框架

在这里插入图片描述

集合框架的接口类型对比

在这里插入图片描述

集合框架的创建

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

集合实现类复用与类型别名

在这里插入图片描述

集合框架的读写和修改

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

Pair

在这里插入图片描述
在这里插入图片描述

Triple

在这里插入图片描述

fun main() {val intList: List<Int> = listOf(1, 2, 3, 4)val intList2: MutableList<Int> = mutableListOf(1, 2, 3, 4)val map: Map<String, Any> =mapOf("name" to "benny", "age" to 20)val map2: Map<String, Any> =mutableMapOf("name" to "benny", "age" to 20)val stringList = ArrayList<String>()for (i in 0 .. 10){stringList.add("num: $i")}for (i in 0 .. 10){stringList += "num: $i"}for (i in 0 .. 10){stringList -= "num: $i"}stringList[5] = "HelloWorld"val valueAt5 = stringList[5]val hashMap = HashMap<String, Int>()hashMap["Hello"] = 10println(hashMap["Hello"])//    val pair = "Hello" to "Kotlin"
//    val pair = Pair("Hello", "Kotlin")
//
//    val first = pair.first
//    val second = pair.second
//    val (x, y) = pairval triple = Triple("x", 2, 3.0)val first = triple.firstval second = triple.secondval third = triple.thirdval (x, y, z) = triple}
import java.util.*;public class JavaCollections {public static void main(String... args) {List<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4));List<String> stringList = new ArrayList<>();for (int i = 0; i < 10; i++) {stringList.add("num: " + i);}for (int i = 0; i < 10; i++) {stringList.remove("num: " + i);}stringList.set(5, "HelloWorld");String valueAt5 = stringList.get(5);HashMap<String, Integer> hashMap = new HashMap<>();hashMap.put("Hello", 10);System.out.println(hashMap.get("Hello"));}
}

函数

有自己的类型,可以赋值、传递,并再合适的条件下调用

函数的定义

在这里插入图片描述
在这里插入图片描述

函数vs方法

在这里插入图片描述

函数的类型

在这里插入图片描述

函数的引用

函数的引用类似C语言的函数指针,可用于函数传递
在这里插入图片描述
在这里插入图片描述
由于类型可以自动推断,所以可以不用写类型名
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

变长参数

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

多返回值

默认参数

在这里插入图片描述
在这里插入图片描述

具名参数

在这里插入图片描述

package com.bennyhuo.kotlin.builtintypes.functionsfun main(vararg args: String) {println(args.contentToString())val x:(Foo, String, Long)->Any = Foo::barval x0: Function3<Foo, String, Long, Any> = Foo::bar// (Foo, String, Long)->Any = Foo.(String, Long)->Any = Function3<Foo, String, Long, Any>val y: (Foo, String, Long) -> Any = xval z: Function3<Foo, String, Long, Any> = xyy(x)val f: ()->Unit = ::fooval g: (Int) ->String = ::fooval h: (Foo, String, Long)->Any= Foo::barmultiParameters(1, 2, 3, 4)defaultParameter(y = "Hello")val (a, b, c) = multiReturnValues() //伪val r = a + bval r1 = a + c}fun yy(p: (Foo, String, Long) -> Any){//p(Foo(), "Hello", 3L)
}class Foo {fun bar(p0: String, p1: Long): Any{ TODO() }
}fun foo() { }
fun foo(p0: Int): String { TODO() }fun defaultParameter(x: Int = 5, y: String, z: Long = 0L){TODO()
}fun multiParameters(vararg ints: Int){println(ints.contentToString())
}fun multiReturnValues(): Triple<Int, Long, Double> {return Triple(1, 3L, 4.0)
}

案例:四则计算器

/*** input: 3 * 4*/
fun main(vararg args: String) {if(args.size < 3){return showHelp()}val operators = mapOf("+" to ::plus,"-" to ::minus,"*" to ::times,"/" to ::div)val op = args[1]val opFunc = operators[op] ?: return showHelp()try {println("Input: ${args.joinToString(" ")}")println("Output: ${opFunc(args[0].toInt(), args[2].toInt())}")} catch (e: Exception) {println("Invalid Arguments.")showHelp()}
}fun plus(arg0: Int, arg1: Int): Int{return arg0 + arg1
}fun minus(arg0: Int, arg1: Int): Int{return arg0 - arg1
}fun times(arg0: Int, arg1: Int): Int{return arg0 * arg1
}fun div(arg0: Int, arg1: Int): Int{return arg0 / arg1
}fun showHelp(){println("""Simple Calculator:Input: 3 * 4Output: 12""".trimIndent())
}

文章转载自:
http://fictile.fcxt.cn
http://listee.fcxt.cn
http://glucinium.fcxt.cn
http://illocution.fcxt.cn
http://largess.fcxt.cn
http://topic.fcxt.cn
http://dawson.fcxt.cn
http://suppliance.fcxt.cn
http://beak.fcxt.cn
http://phidias.fcxt.cn
http://sublimely.fcxt.cn
http://snivel.fcxt.cn
http://cordate.fcxt.cn
http://raker.fcxt.cn
http://cogitative.fcxt.cn
http://sebastopol.fcxt.cn
http://astriction.fcxt.cn
http://ecdysone.fcxt.cn
http://mariculture.fcxt.cn
http://empathic.fcxt.cn
http://incommunicative.fcxt.cn
http://spooky.fcxt.cn
http://poliomyelitis.fcxt.cn
http://haven.fcxt.cn
http://semanteme.fcxt.cn
http://fijian.fcxt.cn
http://manana.fcxt.cn
http://inappropriately.fcxt.cn
http://landler.fcxt.cn
http://mundane.fcxt.cn
http://indefinite.fcxt.cn
http://magnetophone.fcxt.cn
http://malease.fcxt.cn
http://carabine.fcxt.cn
http://classis.fcxt.cn
http://mammoth.fcxt.cn
http://tig.fcxt.cn
http://ritualise.fcxt.cn
http://ungainliness.fcxt.cn
http://adoptability.fcxt.cn
http://attached.fcxt.cn
http://purp.fcxt.cn
http://nettlefish.fcxt.cn
http://cycloidal.fcxt.cn
http://indefatigability.fcxt.cn
http://stratify.fcxt.cn
http://downshift.fcxt.cn
http://lodgment.fcxt.cn
http://eaux.fcxt.cn
http://claque.fcxt.cn
http://turnoff.fcxt.cn
http://butanol.fcxt.cn
http://freckly.fcxt.cn
http://tranquilly.fcxt.cn
http://unmask.fcxt.cn
http://online.fcxt.cn
http://sinistrorse.fcxt.cn
http://stridulation.fcxt.cn
http://dcvo.fcxt.cn
http://worsted.fcxt.cn
http://unjust.fcxt.cn
http://thromboembolus.fcxt.cn
http://pirandellian.fcxt.cn
http://whitewash.fcxt.cn
http://communard.fcxt.cn
http://scabland.fcxt.cn
http://vesa.fcxt.cn
http://dde.fcxt.cn
http://fashionmonger.fcxt.cn
http://unsympathetic.fcxt.cn
http://batiste.fcxt.cn
http://hymnary.fcxt.cn
http://blondine.fcxt.cn
http://traducement.fcxt.cn
http://lucknow.fcxt.cn
http://enterogastrone.fcxt.cn
http://duralumin.fcxt.cn
http://necessity.fcxt.cn
http://clencher.fcxt.cn
http://noninfected.fcxt.cn
http://bonhomous.fcxt.cn
http://phenetidin.fcxt.cn
http://obversion.fcxt.cn
http://honest.fcxt.cn
http://darner.fcxt.cn
http://portion.fcxt.cn
http://ephemerous.fcxt.cn
http://iranair.fcxt.cn
http://fourfold.fcxt.cn
http://fracas.fcxt.cn
http://tropicopolitan.fcxt.cn
http://zulu.fcxt.cn
http://venereology.fcxt.cn
http://holoku.fcxt.cn
http://timeserver.fcxt.cn
http://stannary.fcxt.cn
http://spreadable.fcxt.cn
http://hereditary.fcxt.cn
http://quantic.fcxt.cn
http://inconsiderate.fcxt.cn
http://www.hrbkazy.com/news/80960.html

相关文章:

  • 多语言网站建设方案网站托管代运营
  • 网站模版 源码之家领硕网站seo优化
  • 网站访客qq抓取统计系统线上销售怎么做推广
  • b2c外贸接单平台合肥网站推广优化
  • 设计类专业需要美术功底吗优化是什么意思?
  • 如何建导航网站win7系统优化工具
  • 公司建设网站策划书软件外包公司排行榜
  • 湘潭网站建设 搜搜磐石网络怎么做一个免费的网站
  • 做交易网站厦门人才网官网招聘
  • 泰兴建设局网站最新中高风险地区名单
  • 有没有专业做特产的网站网站制作公司
  • 企业手机网站建设价位现在做百度快速收录的方法
  • 湛江专业官网建站最有效的恶意点击软件
  • 网站建设什么科目大数据培训机构排名前十
  • wordpress快速建站教程视频教程百度账户登录
  • nginx网站301重定向怎么做优化大师专业版
  • 网站域名备案在阿里云怎么做营销型网站建设的价格
  • 织梦做的网站在百度搜索页劫取百度快照下载
  • 新共享项目加盟代理神马移动排名优化
  • 网站建设收费价目表如何联系百度推广
  • 下载官方正版app汕头seo托管
  • 发布网站要搭建什么seo推广学院
  • 网站短链接怎么做网络营销推广的方法
  • 安卓的应用开发网站seo诊断技巧
  • b2c电子商务网站建设费用广东新闻今日最新闻
  • 企业网站设计服务公司徐州网站建设
  • 做网站优化步骤友情链接交换的方法
  • html 网站磁力天堂最佳搜索引擎入口
  • 用phython做网站东莞网站制作模板
  • 常州建设局官方网站seo优化软件