// Create new 6 Pot
val list = mutableListOf<Int>()
val pot = listOf<Pot>(Pot(50), Pot(50), Pot(50), Pot(50), Pot(50), Pot(50))
val linearLayout: LinearLayout = findViewById(R.id.text)
linearLayout.removeAllViews()
// random 6 number from 1 - 50 without duplicate
// Create textview to print out roll result
pot.forEach {
val d = it
val rollResult = d.roll()
list.add(rollResult)
val textView = TextView(this)
textView.append(this.toString())
val param = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
param.marginStart = 10
textView.layoutParams = param
textView.text = rollResult.toString()
linearLayout.addView(textView)
}我有这样的代码,在像临时文件一样将rollResult添加到list之后,我应该做些什么来比较下一个结果和以前在list中的结果,如果它已经出现了,那么从开始到结束执行另一个滚动。谢谢
发布于 2021-04-22 09:40:42
你可以用distinct(),
参考文献:https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/distinct.html
示例
var list = listOf(1,2,4,5,1,6,8,2,4,3,3,5)
list = list.distinct();
println(list)生成轧辊结果的第一个循环
pot.forEach {
val d = it
val rollResult = d.roll()
list.add(rollResult)
}用于添加动态视图的第二个循环
list = list.distinct();
list.forEach {
// Add Views etc
}根据您的需要进行修改。
发布于 2021-04-22 09:14:31
检查元素是否存在于整个列表中,是否在下面的代码中进行尝试:
if(list.contains(element)){ // do rollout again}https://stackoverflow.com/questions/67209710
复制相似问题