【智能终端题】kotlin 容器

版权声明:萍水相逢 https://blog.csdn.net/ping_lvy/article/details/89352609

1、有如下赋值语句:

 

 

private val goodsA:String = "惠普"

 

private val goodsB:String = "联想"

 

private val goodsC:String = "戴尔"

 

private val goodsD:String = "国际通用"

 

private val goodsF:String = "方正"

 

private val goodsG:String = "联想 Y520"

 

private val goodsH:String = "联想 Y720"

 

private val goodsI:String = "联想 Y7000"

 

 

 

 

2 请声明一个MutableList队列,并用上面的数据,初始化你声明的MutableList队列,分别使用removeAt移除第三个元素,使用函数根据传入参数值的不同,对MutableList的元素的升序或者降序排序并输出,使用for-in、iterator while、forEach、indices,输出如下结果

 

 

电脑畅销榜已添加,当前共有8款电脑

 

for-in电脑畅销榜包含以下8款电脑:

 

名称:联想

 

名称:联想 Y520

 

名称:惠普

 

名称:戴尔

 

名称:国际通用

 

名称:方正

 

名称:联想 Y720

 

名称:联想 Y7000

 

 

removeAt电脑畅销榜已更新,当前共有8款电脑

 

for-in电脑畅销榜包含以下7款电脑:

 

名称:联想

 

名称:联想 Y520

 

名称:戴尔

 

名称:国际通用

 

名称:方正

 

名称:联想 Y720

 

名称:联想 Y7000

 

 

for-in sort 电脑畅销榜已按照it升序重新排列:

 

名称:戴尔

名称:方正

名称:国际通用

名称:惠普

名称:联想

名称:联想 Y7000

名称:联想 Y720

 

 

for-in sort 电脑畅销榜已按照it降序重新排列:

 

名称:联想 Y720

名称:联想 Y7000

名称:联想

名称:惠普

名称:国际通用

名称:方正

名称:戴尔

 

iterator电脑畅销榜包含以下7款电脑:

 

名称:联想 Y720

 

名称:联想 Y7000

 

名称:联想

 

名称:方正

 

名称:戴尔

 

名称:惠普

 

名称:国际通用

 

 

forEach电脑畅销榜包含以下7款电脑:

 

名称:联想 Y720

 

名称:联想 Y7000

 

名称:联想

 

名称:方正

 

名称:戴尔

 

名称:惠普

 

名称:国际通用

 

 

indices电脑畅销榜包含以下7款电脑:

 

名称:长城

 

名称:联想 Y720

 

名称:联想 Y7000

 

名称:联想

 

名称:方正

 

名称:戴尔

 

名称:惠普

 

名称:国际通用

分析: List集合中自带的有升序降序方法,调用即可

private val goodsA:String = "惠普"
private val goodsB:String = "联想"
private val goodsC:String = "戴尔"
private val goodsD:String = "国际通用"
private val goodsF:String = "方正"
private val goodsG:String = "联想 Y520"
private val goodsH:String = "联想 Y720"
private val goodsI:String = "联想 Y7000"
private var goodMutableList:MutableList<String> = mutableListOf()
fun main() {
    goodMutableList.add(goodsB)
    goodMutableList.add(goodsG)
    goodMutableList.add(goodsA)
    goodMutableList.add(goodsC)
    goodMutableList.add(goodsD)
    goodMutableList.add(goodsF)
    goodMutableList.add(goodsH)
    goodMutableList.add(goodsI)


    var str:String = ""
    for (item in goodMutableList){
        str = str + "名称:" + item +"\n"
    }
    println("电脑畅销榜已添加,当前共有${goodMutableList.size}款电脑")
    println("for in电脑畅销榜包含以下${goodMutableList.size}款电脑:")
    println("$str")



    if (goodMutableList.isNotEmpty() && goodMutableList.size>=3){
        goodMutableList.removeAt(2)
    }
    println("removeAt电脑畅销榜已更新,当前共有${goodMutableList.size}款电脑")
    str = ""
    for (item in goodMutableList){
        str = str + "名称:" + item +"\n"
    }
    println("for in电脑畅销榜包含以下${goodMutableList.size}款电脑:")
    println("$str")

    goodMutableList.sort()
    str = ""
    for (item in goodMutableList){
        str = str + "名称:" + item + "\n"
    }
    println("for-in sort 电脑畅销榜已按照it升序重新排列:")
    println("$str")

    goodMutableList.sortDescending()
    str = ""
    for (item in goodMutableList){
        str = str + "名称:" + item + "\n"
    }
    println("for-in sort 电脑畅销榜已按照it降序重新排列:")
    println("$str")

    str = ""
    var iterator = goodMutableList.iterator()
    while (iterator.hasNext()){
        var item = iterator.next()
        str = str + "名称:" + item + "\n"
    }
    println("iterator电脑畅销榜包含以下7款电脑:")
    println("$str")

    println("forEach电脑畅销榜包含以下7款电脑:")
    goodMutableList.forEach { println("名称:${it}") }
}

实验结果:

猜你喜欢

转载自blog.csdn.net/ping_lvy/article/details/89352609