- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嘿,程序员们,
我最近从普通的 Python 编程跳到了 Android Studio 中的 Kotlin 编程。
在 python 中,我生成了一些字典,其中包含字符串作为键和元组作为值。这些元组本身包含一个 int、两个字符串和两个 int。想象一下这个简化的字典:
dict = {"key" : (1,"firstString","secondString",2,0),
"secondKey":(0,"firstString","secondString",4,1)}
现在可怕的部分来了:我想将我的字典导入到 Kotlin 中的普通变量/值中。
因此,我将所有四个字典放入 res 文件夹中的文本文件中。对我来说,困难在于声明适当数据结构的 val,然后实际将键值对一步步加载到 val 中。当深入研究其数据结构时,所有 Kotlin 教程似乎都结束了。
我的猜测是否正确,我应该有一个字典本身的 HashMap ,然后有一个元组的(n数组)列表?当试图声明复杂的结构时甚至陷入困境:
val importedDict: HashMap<String,Array<Integer,String,String,Integer>> = hashMapOf<String,Array<Int,String,String,Int>()
因为 Kotlin 中似乎没有多种类型的有效数据结构。
非常感谢您提供的任何提示。
最佳答案
我相信表示元组的最简单方法是声明 data class
,像这样:
data class TupleData(val id: Int,
val firstString: String,
val secondString: String,
val otherNumber: Int,
val anotherNumber: Int)
然后将您的数据结构声明为 HashMap<String, TupleData>
。请注意,您必须考虑类型( String
或 String?
)的可空性以及它们是否具有不可变引用( val
或 var
),因为您没有在问题中指定它们。
此外,为了让您的生活更轻松,我相信您可以使用Moshi使用自定义适配器来解析您的字符串,就好像它是直接解析到数据结构中的 JSON 对象一样。 (但请记住这个 other answer )。
您不能声明 Array
如Array<Integer, String, String, Integer>
因为它只需要一个名为 T
的通用参数在 kotlin documentation 。如果你想指定一个元组,你必须使用 Pair或Triple如果数据多于元组中的数据,则声明 data class
编辑:尝试使用您的数据制作一个工作示例后,我注意到它的格式不适合 JSON 解析,因此您可能必须手动解析它们。
如果您仍然想了解如何序列化为 JSON(也许使用 Python 中的 pickle),这就是我实现适配器的方式:
class TupleDataAdapter {
@ToJson
fun toJson(tupleData: TupleData) : String {
val (id, first, second, other, another) = tupleData;
return "($id, $first, $second, $other, $another)"
}
@FromJson
fun fromJson(tuple: String) : TupleData {
if (tuple[0] != '(' || tuple.last() != ')') {
throw JsonDataException("Wrong format for tuple data: missing '(' or ')'")
}
val trimmed = tuple.trimStart('(').trimEnd(')')
val fields = trimmed.split(',')
if (fields.size != 5) {
throw JsonDataException("Wrong format for tuple data: wrong number of fields in tuple")
}
return TupleData(fields[0].toInt(), fields[1], fields[2], fields[3].toInt(), fields[4].toInt())
}
}
fun main(args: Array<String>) {
val dict = File("./dict.txt").readText()
println(dict)
val moshi = Moshi.Builder()
.add(TupleDataAdapter())
.build()
val type = Types.newParameterizedType(Map::class.java, String::class.java, TupleData::class.java)
val mapAdapter : JsonAdapter<Map<String, TupleData>> = moshi.adapter(type)
val decoded : Map<String, TupleData>? = mapAdapter.fromJson(dict)
println(decoded)
}
当我更改文本时,此实现可以正常工作:
{"key" : "(1,firstString,secondString,2,0)","secondKey" : "(0,firstString,secondString,4,1)"}
注意引号中的区别。
关于python - Kotlin:从文件加载包含字符串和元组的字典( HashMap ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60043722/
如何使用 SPListCollection.Add(String, String, String, String, Int32, String, SPListTemplate.QuickLaunchO
我刚刚开始使用 C++ 并且对 C# 有一些经验,所以我有一些一般的编程经验。然而,似乎我马上就被击落了。我试过在谷歌上寻找,以免浪费任何人的时间,但没有结果。 int main(int argc,
这个问题已经有答案了: In Java 8 how do I transform a Map to another Map using a lambda? (8 个回答) Convert a Map>
我正在使用 node + typescript 和集成的 swagger 进行 API 调用。我 Swagger 提出以下要求 http://localhost:3033/employees/sear
我是 C++ 容器模板的新手。我收集了一些记录。每条记录都有一个唯一的名称,以及一个字段/值对列表。将按名称访问记录。字段/值对的顺序很重要。因此我设计如下: typedef string
我需要这两种方法,但j2me没有,我找到了一个replaceall();但这是 replaceall(string,string,string); 第二个方法是SringBuffer但在j2me中它没
If string is an alias of String in the .net framework为什么会发生这种情况,我应该如何解释它: type JustAString = string
我有两个列表(或字符串):一个大,另一个小。 我想检查较大的(A)是否包含小的(B)。 我的期望如下: 案例 1. B 是 A 的子集 A = [1,2,3] B = [1,2] contains(A
我有一个似乎无法解决的小问题。 这里...我有一个像这样创建的输入... var input = $(''); 如果我这样做......一切都很好 $(this).append(input); 如果我
我有以下代码片段 string[] lines = objects.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.No
这可能真的很简单,但我已经坚持了一段时间了。 我正在尝试输出一个字符串,然后输出一个带有两位小数的 double ,后跟另一个字符串,这是我的代码。 System.out.printf("成本:%.2
以下是 Cloud Firestore 列表查询中的示例之一 citiesRef.where("state", ">=", "CA").where("state", "= 字符串,我们在Stack O
我正在尝试检查一个字符串是否包含在另一个字符串中。后面的代码非常简单。我怎样才能在 jquery 中做到这一点? function deleteRow(locName, locID) { if
这个问题在这里已经有了答案: How to implement big int in C++ (14 个答案) 关闭 9 年前。 我有 2 个字符串,都只包含数字。这些数字大于 uint64_t 的
我有一个带有自定义转换器的 Dozer 映射: com.xyz.Customer com.xyz.CustomerDAO customerName
这个问题在这里已经有了答案: How do I compare strings in Java? (23 个回答) 关闭 6 年前。 我想了解字符串池的工作原理以及一个字符串等于另一个字符串的规则是
我已阅读 this问题和其他一些问题。但它们与我的问题有些无关 对于 UILabel 如果你不指定 ? 或 ! 你会得到这样的错误: @IBOutlet property has non-option
这两种方法中哪一种在理论上更快,为什么? (指向字符串的指针必须是常量。) destination[count] 和 *destination++ 之间的确切区别是什么? destination[co
This question already has answers here: Closed 11 years ago. Possible Duplicates: Is String.Format a
我有一个Stream一个文件的,现在我想将相同的单词组合成 Map这很重要,这个词在 Stream 中出现的频率. 我知道我必须使用 collect(Collectors.groupingBy(..)
我是一名优秀的程序员,十分优秀!