集合Set/MutableSet
集合是一种最简单的容器,具备以下特点:
1> 容器内部元素不按顺序排列,因此无法进行下标金星访问
2>容器内部元素存在唯一性,通过哈希值校验是否存在相同的元素,若存在将其覆盖
for 循环
class MainActivity : AppCompatActivity() {
private var goodsMap: Map<String, String> = mapOf("苹果" to "iphone 8", "华为" to "Mate10", "小米" to "小米6", "魅族" to "魅族 PRO6")
private var goodsMutMap:MutableMap<String,String> = mutableMapOf(Pair("苹果","iphone 8"), Pair("华为","Mate10"),
Pair("小米","小米6"),Pair("魅族","魅族 PRO6"))
@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn_set_for.setOnClickListener { v ->
var desc = ""
for (item in satellites.indices) {
desc = "${desc}名称:${item}\n"
}
text_show.text = "显示的星球数量${satellites.size}\n$desc"
}
}
}
效果
22.png
网友评论