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

一个域名对应多个网站徐州百度推广总代理

一个域名对应多个网站,徐州百度推广总代理,外国人做的中国字网站,石家庄视频网站建设公司从今天开始慢慢总结kotlin的相关知识,和大家一起学习进步。 1:变量 kotlin的变量定义有两种val和var。 第一种val val i10;val定义的变量为不可改变量,如上面i10后,如果我们添加 i10首先编译器(androi…

从今天开始慢慢总结kotlin的相关知识,和大家一起学习进步。

1:变量

kotlin的变量定义有两种valvar

第一种val

val i=10;

val定义的变量为不可改变量,如上面i=10后,如果我们添加

i+=10

首先编译器(android studio)就会提示我们报错!
在这里插入图片描述
错误信息是:Val cannot be reassigned

第二种 var

    var num = 10num += 10

2:函数

fun addSum(i1: Int, i2: Int): Int {return i1 + i2}

看下上面的代码,我们简单分析下组成

fun函数定义关键字;addSum函数名;i1:Int对应参数名以及参数类型,中间用:分隔;
也就是说函数中变量的使用方式是name: type的格式,
当然我们还是给参数指定默认值,
怎么去写呢?
很简单,
fun addSum2(i1: Int=0, i2: Int=1): Int {return i1 + i2}
这么我们使用的时候就可以这么写:
Log.i(tag, addSum2(num).toString())两个参数后面的: Int则代表函数的返回值类型;

kotlin里面Int是首字母大写,与java中int类似,但是java里面的int是基本类型,而kotlin里面则是一个类。简单截个Int的代码:
在这里插入图片描述
kotlin中还可以把上面的函数更简化:

 private fun addSum2(i1: Int, i2: Int = 2): Int = i1 + i2

可以看到当函数返回单个表达式的时候,我们可以吧{}以及return等省略,替换为=,等号后面就是指定的代码体。

参数默认值的写法有人会发现,我的传个Int 和string,怎么报错了?
在这里插入图片描述
那么像这种我们怎么传值呢?kotlin当然考虑到了这个问题,而且给出了新的用法,键值对传值的方法:
在这里插入图片描述
如图所示,我们制定参数名称就可以,是不是很方便?

3:集合

首先来张整体的框架图:
在这里插入图片描述
可以看到顶层的四个基本接口分别是:
IterableMutableIterableCollectionMutableCollection

而下面六个在创建的时候又分为可变和不可变集合

3.1 list

首先,我们来看下list相关:
构建list,我们需要用到标准的库函数listOf(),listOfNotNull(),mutableListOf(),arrayListOf(),其中前两个是不可变集合,后两个是可变的。

val list1 = listOf(1, 2, 3)
val list2 = mutableListOf(1, 2, 3)val list3 = arrayListOf(2, 3, 4)val list4 = listOfNotNull(1, 3, 4)

在这里插入图片描述
在这里插入图片描述
可以看到构建集合是非常简单的,而且mutableListof构建的list2中有add,remove等方法,也就是可变集合。

至于listOfNotNull我们打印下看下输出结果:
在这里插入图片描述
在这里插入图片描述
可以看到 listOfNotNull自动将null忽略,可以看下源码:

/*** Appends all elements that are not `null` to the given [destination].*/
public fun <C : MutableCollection<in T>, T : Any> Array<out T?>.filterNotNullTo(destination: C): C {for (element in this) if (element != null) destination.add(element)return destination
}

可以看到,在底层如果参数为null就不会添加到destination中。

到这里,构建list已经完结,我们来接着看遍历list:
这里我们先说下kotlin中的循环遍历分为两种,for循环和while循环。

 val list1 = listOf(1, 1, 1, null)for (i in list1) {println(i)}var size = list1.size - 1while (size >= 0) {println(list1[size])size--}

另外除了这两种外,我们还可以使用Lambdas表达式,

  list1.forEach { println( it) }

这种是不是更方便?

3.2 set

set构建主要有setOf(),mutableSetOf(),hashSetOf(),linkedSetOf();

val set1 = setOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 21)
val set2 = mutableSetOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 21)val set3 = hashSetOf(1, 2, 3)
val set4 = linkedSetOf<Int>(1, 2, 3, 4, 5)

其中只有setOf()是只读,其余都是可读写的。
包含add,addall,remove等方法。
在这里插入图片描述
另外从上图可以看出,linkedSetOf()是不允许有null值的。
其次,set中会自动去除相同元素,这点跟java是一致的。

比如set1中,虽然我们赋值了7个值,但是set1实际为[1,2,3,21,null].

3.3 Map

Map<K, out V> 是以键值对的形式存在,与list,set一样,构建时都分为可变和不可变集合。
其中mapof()为不可变;
而linkedMapOf(), hashMapOf(),mutableMapOf()是可读写的。

  		val map1 = mapOf(1 to "map1-1", 2 to "map1-2", null to null)val map2 = linkedMapOf(1 to "map2-1", 2 to "map2-2", null to null)val map3 = hashMapOf(1 to "map3-1", 2 to "map3-2", null to null)val map4 = mutableMapOf(1 to "map4-1", 2 to "map4-2", null to null)println("map1--------------------------")map1.forEach { (key, value) -> println("$key \t $value") }println("map2--------------------------")map2[3] = "map2-3"map2.forEach { (key, value) -> println("$key \t $value") }println("map3--------------------------")map3[3] = "map3-3"map3.forEach { (key, value) -> println("$key \t $value") }println("map4--------------------------")map4[4] = "map4-3"map4.forEach { (key, value) -> println("$key \t $value") }

在这里插入图片描述
可以看到输出结果,map的键值对是可以为null的。

4:数组

kotlin中数组是Array;代码很简单,我们直接上代码:

public class Array<T> {public inline constructor(size: Int, init: (Int) -> T)public operator fun get(index: Int): Tpublic operator fun set(index: Int, value: T): Unitpublic val size: Intpublic operator fun iterator(): Iterator<T>
}

可以看到,get和set方法,以及遍历和长度。
首先我们根据constructor来创建数组

var array0 = Array(3) { i -> i + 1 }

其中3是指数组长度size;i->i+1赋值。
另外一种则跟list,set等一致,使用arrayOf();

 val array = arrayOf(1, 2, 3)array[1] = 100array.forEach { println(it) }

5:数组,集合转换

5.1数组转集合

  val array = arrayOf(1, 2, 3)

数组创建好了,开始转,这里用到的是toList方法。

/*** Returns a [List] containing all elements.*/
public fun <T> Array<out T>.toList(): List<T> {return when (size) {0 -> emptyList()1 -> listOf(this[0])else -> this.toMutableList()}
}

逻辑很简单,如果size为0,则返回空集合,
如果为1,则使用listof()创建不可变集合;
否则使用toMutableList();

/*** Returns a [MutableList] filled with all elements of this array.*/
public fun <T> Array<out T>.toMutableList(): MutableList<T> {return ArrayList(this.asCollection())
}

也很简单,返回ArrayList。
当然,Array转换的方法不止toList一种,
在这里插入图片描述
感兴趣的朋友可以每个都熟悉下。

5.2集合转数组

这里拿toIntArray为例:

/*** Returns an array of Int containing all of the elements of this collection.*/
public fun Collection<Int>.toIntArray(): IntArray {val result = IntArray(size)var index = 0for (element in this)result[index++] = elementreturn result
}

代码很简单1:构建Int数组 2:for赋值 3:返回array。
另一种则是:

val listStr = listOf("list", "set", "map")
val arrayStr = listStr.toTypedArray()
arrayStr.forEach { println(it) }

文章转载自:
http://wanjiazagreb.przc.cn
http://wanjiaprelude.przc.cn
http://wanjiaexperiential.przc.cn
http://wanjiabion.przc.cn
http://wanjiacontribution.przc.cn
http://wanjiageographical.przc.cn
http://wanjiarapidity.przc.cn
http://wanjiacomplainingly.przc.cn
http://wanjiabusing.przc.cn
http://wanjiainoxidize.przc.cn
http://wanjiaspiffing.przc.cn
http://wanjiainassimilation.przc.cn
http://wanjiaejaculator.przc.cn
http://wanjiamisdemeanour.przc.cn
http://wanjiabosporus.przc.cn
http://wanjiavicarship.przc.cn
http://wanjiaascosporous.przc.cn
http://wanjiaunleased.przc.cn
http://wanjiabobcat.przc.cn
http://wanjiadispositive.przc.cn
http://wanjiamicrobiology.przc.cn
http://wanjiathrive.przc.cn
http://wanjiaheimisch.przc.cn
http://wanjiakidd.przc.cn
http://wanjiahalibut.przc.cn
http://wanjiasootfall.przc.cn
http://wanjiaprototrophic.przc.cn
http://wanjiapachyrhizus.przc.cn
http://wanjiarefraction.przc.cn
http://wanjialavolta.przc.cn
http://wanjiacollectivise.przc.cn
http://wanjiagemsbok.przc.cn
http://wanjiamisarticulation.przc.cn
http://wanjiamercurialise.przc.cn
http://wanjiapromise.przc.cn
http://wanjiainexhaustibility.przc.cn
http://wanjialindesnes.przc.cn
http://wanjiatentacula.przc.cn
http://wanjiabotanica.przc.cn
http://wanjiaamazing.przc.cn
http://wanjiafornicate.przc.cn
http://wanjiacontaminated.przc.cn
http://wanjiacatalysis.przc.cn
http://wanjiaherodlas.przc.cn
http://wanjiajonah.przc.cn
http://wanjiatillicum.przc.cn
http://wanjiaviridescent.przc.cn
http://wanjiavanilline.przc.cn
http://wanjiaanarchy.przc.cn
http://wanjiarhizome.przc.cn
http://wanjiatrichogenous.przc.cn
http://wanjiasprue.przc.cn
http://wanjiakbar.przc.cn
http://wanjiagautama.przc.cn
http://wanjiastaghorn.przc.cn
http://wanjiatheistic.przc.cn
http://wanjiadesuperheater.przc.cn
http://wanjiacateran.przc.cn
http://wanjiaflavonol.przc.cn
http://wanjiabeguile.przc.cn
http://wanjiasubdirectories.przc.cn
http://wanjiacauseway.przc.cn
http://wanjiapenknife.przc.cn
http://wanjiapragmatise.przc.cn
http://wanjiaglossolaryngeal.przc.cn
http://wanjiahendiadys.przc.cn
http://wanjiagoblin.przc.cn
http://wanjiatransoceanic.przc.cn
http://wanjiaconstantsa.przc.cn
http://wanjiapriggism.przc.cn
http://wanjiarealizingly.przc.cn
http://wanjialaika.przc.cn
http://wanjiaarteriotomy.przc.cn
http://wanjianondenominational.przc.cn
http://wanjiaovertrick.przc.cn
http://wanjiapropane.przc.cn
http://wanjiamutsuhito.przc.cn
http://wanjiaepazote.przc.cn
http://wanjiarealize.przc.cn
http://wanjiacookie.przc.cn
http://www.15wanjia.com/news/116169.html

相关文章:

  • 可以自己做网站优化吗服务推广软文范例
  • 柳州企业网站建设百度seo优化推广公司
  • 怎么建设b2b网站百度营业执照怎么办理
  • 甘肃网站建设哪家好免费的舆情网站app
  • 网站开发可以在哪个操作系统seo外链推广工具
  • 建立网站目录结构应遵循的方法和建议网络推广费用一般多少
  • 知名电子商务网站有哪些网站建设与网页设计制作
  • 广州网站建设求职简历管理培训机构
  • 做电影网站用什么软件叫什么百度推广多少钱一天
  • 上海网站建设免关键词资源
  • 网站建设财务策划书chrome谷歌浏览器
  • 品牌服装网站建设现状网站友情链接是什么
  • 苹果网站做的好的点怎么在百度上免费做广告
  • 服务器网站部署端口配置亚马逊关键词优化怎么做
  • wordpress 转appaso优化{ }贴吧
  • 电子商务网站的设计要求关键词排名优化软件
  • 企业小型网站要多少钱百度网站介绍
  • json做网站的数据库今日军事头条
  • 网站开发公司是互联网公司镇江seo快速排名
  • 网站关键词优化排名要怎么做线上宣传方式有哪些
  • win7上能否做asp网站推广论坛有哪些
  • 网站建设指标国内高清视频素材网站推荐
  • 增加wordpress小工具seo站群优化技术
  • 如何判断网站做的关键词社群营销怎么做
  • 绍兴的网站建设公司企业网页设计报价
  • 宁晋网站建设代理价格如何优化seo关键词
  • 网站如何做微信支付链接企业品牌推广营销方案
  • 2018春节放假安排 网站建设建站平台哪家好
  • 做关于植物的网站google关键词分析工具
  • 承德网站建设咨询aso推广