Tag: 列表

什么是深刻复制二维MutableList简洁的方式?

元素已经实现了深度复制。 fun <T : DeepCopiable> f(a: MutableList<MutableList<T>>) { val copied = a.map { it.map { it.deepCopy() }.toMutableList() }.toMutableList() … } 我正在使用这种代码,但似乎冗长。

在Kotlin中递归地列出文件

用kotlin列出目录中的文件,我使用了list()和listFiles()函数: File("/tmp").list().forEach { println(it) } File("/tmp").listFiles().forEach { println(it) } 但是,如何递归列表文件?

从LinkedHashMap中提取映射值作为ArrayList

我创建了一个简单的类MapExtension来容纳将4个值传递给一个listview适配器,并使用LinkedHashmap来添加MapExtension的ArrayList。 public class MapExtension { private String studname; private String studnumber; private String schedule; public MapExtension(String studname, String studnumber, String schedule) { this.studname = studname; this.studnumber= studnumber; this.schedule= schedule; } public String getStudname () { return studname; } public String getStudnumber() { return studnumber; } public String getSchedule() { return schedule; } } 每当我尝试从LinkedHashMap中提取返回Collections的ArrayList<MapExtension> ,我会从不同的试验中得到这些错误(在注释中): […]

如何用十六进制值更改CheckedTextView的Checked Tint Color

我想在checked视图的状态时动态地改变CheckedTextView的色调。 我很确定我可以通过在CheckedTextView上调用setCheckMarkTintList来实现这CheckedTextView 。 要做到这一点,我需要一个ColorStateList ,但问题是我想保留CheckedTextView每个状态的所有颜色,除了checked状态。 所以,我可以获得ColorStateList的CheckedTextView ,但是我不知道只能更改checked状态的颜色。 我知道我可以创建一个新的ColorStateList ,但是如何确保它保留了原始的所有值? 我可以像这样创建一个状态列表: int[][] states = new int[][] { new int[]{android.R.attr.state_pressed}, new int[]{-android.R.attr.state_pressed}, new int[]{android.R.attr.state_focused}, new int[]{-android.R.attr.state_focused}, new int[]{android.R.attr.state_selected}, new int[]{-android.R.attr.state_selected}, new int[]{android.R.attr.state_checkable}, new int[]{-android.R.attr.state_checkable}, new int[]{android.R.attr.state_checked}, new int[]{-android.R.attr.state_checked}, new int[]{android.R.attr.state_enabled}, new int[]{-android.R.attr.state_enabled}, new int[]{android.R.attr.state_window_focused}, new int[]{-android.R.attr.state_window_focused}, new int[]{} // default state } 并从原始ColorStateList的颜色创建一个颜色列表: int[] colors = new […]

将数据从另一个活动添加到arrayList

我有一个为listView创建新项目的活动。 点击这个活动的保存按钮,我想添加日期到arrayList。 这个arrayList是在另一个活动,这个活动有一个listView和它的适配器从arrayList中获取项目 这是我的适配器: inner class mo3d1Adapter : BaseAdapter { override fun getItemId(p0: Int): Long { return p0.toLong() } override fun getCount(): Int { return listOfmo3d.size } var listOfmo3d = ArrayList<mo3dInfo>() var context: Context? = null constructor(context: Context, listOfmo3d: ArrayList<mo3dInfo>) : super() { this.listOfmo3d = listOfmo3d this.context = context } override fun getView(p0: Int, p1: […]

根据另一个列表的顺序排序列表

我需要排序一个Person对象List<Person> ( List<Person> ,其中每个Person对象具有像id (唯一), name , age等少数属性)。 排序顺序是基于另一个列表。 该列表包含一组Person id (已被排序的List<String> )。 使用Kotlin或Java以与id列表相同的顺序排列List<Person>的最好方法是什么? 例: List Person { (“ID1”,”PERSON1”,22,..), (“ID-2”,”PERSON2”,20,..) ), (“ID-3”,”PERSON3”,19,..),….. } 有序Id列表: List of ID {(“ID2”), (“ID1”),(”ID3”)….} 排序Person列表应该是: List PERSON { (“ID-2”,”PERSON 2”,20,..) ), (“ID1”,”PERSON 2”,22,..), (“ID-3”,”PERSON 2”,19,..),….. } 如果Person列表包含id列表中没有提到的任何id ,那么这些值应该在排序列表的末尾。 编辑:这是我目前在Java中的方式。 我希望有一个比这更好的方法: public static List<Person> getSortList(List <Person> unsortedList, List<String> orderList){ if(unsortedList!=null && […]

在kotlin中,如何传递一个MutableList,其中目标需要一个List

有一个定义为List值的哈希映射: private var mMap: HashMap<String, List<DataStatus>>? = null 有一个函数返回一个哈希映射,但与MutableList的值 fun getDataStatus(response: JSONObject?): HashMap<String, MutableList<DataStatus>> { return HashMap<String, MutableList<AccountStatusAlert>>() } 当将结果传递给期望List的hashMap时,它得到错误: mMap = getDataStatus(resp) //<== got error 有错误: Error:(81, 35) Type mismatch: inferred type is HashMap<String, MutableList<DataStatus>> but HashMap<String, List<DataStatus>>? was expected

如何将类型清除列表转换为Kotlin中的数组?

函数toArray应该将类型清除的列表转换为现在是Array<String> T inline fun <reified T> toArray(list: List<*>): T { return list.toTypedArray() as T } toArray<Array<String>>(listOf("a", "b", "c")) // should be arrayOf("a", "b", "c") 但是, toArray会抛出这个错误。 java.lang.ClassCastException:[Ljava.lang.Object; 不能转换为[Ljava.lang.String; 你有什么想法吗?

从Kotlin中的数字列表中获得一对数字

我有以下列表: [-1, -1, 2, 3, 4, 5, -1, 9, 10, 11, -1, -1, 23, 24, 25] 我想要的是下面的列表: [Pair(2,5), Pair(9,11), Pair(23,25)] 我可以使用像temp和temp这样的临时变量进行迭代。 但我正在寻找一个像map , filter等List上的转换方法来得到我的解决方案。

Kotlin – 基于对象属性对MutableList进行排序

我有这个令牌对象: class Token(type: TokenType, value: String, position: IntRange = 0..0) 我声明一个MutableList: val tokens: MutableList<Token> = mutableListOf() // Mutable List filled 现在我想根据位置IntRange的第一个值对我的列表进行排序。 我试过这样做: tokens .sortedBy { it.position.first } 但是,在使用it关键字后,我无法访问该对象,所以位置以红色突出显示。 有什么建议么?