写在前面
前段时间闲着没事作死,在写一个数据结构的时候,在想能不能通过字典的方法来实现,即以数组方法实现。
这篇就是个小的折腾笔记。
代码实现
废话不多说,直接上代码
List<String> CHARGUS_Data = new ArrayList<String>();
Map<String, List<String>> CHARGUS_Map = new HashMap<String, List<String>>(); //Mutlti-key Map
这段代码有趣在哪里?
其实我最一开始我想到的是 Python 下的 Dictionary(字典,当然如果你想显得自己异常聪明大概可以用 关联数组 这个词),Java 中也有 java.util.Dictionary 这个库;然而实际上的话这个库已经被废弃了,转而使用 java.util.Map,是一个继承了 Dictionary 的库(具体做了哪些改变我也没看)
但是它和 Dictionary 是一个德行:只能做一个键,一个值
那如果我想要做多个值呢?如果我想要比如说给个 Key 叫 码农BTS,然后映射多个值:身高,年龄,颜值……
究竟要如何把多个值映射到一个 Key 上呢?
上 List 啊!
然后!然后这里 敲黑板了!
注意重点,上次第一次把这段代码给一位新入门的新手看的时候,他问了个很好的问题:
这个 <String> 是干啥的?
是啊,继续看刚才的代码:
List<String> CHARGUS_Data = new ArrayList<String>();
Map<String, List<String>> CHARGUS_Map = new HashMap<String, List<String>>(); //Mutlti-key Map
为什么我要加上 String 呢?因为泛型安全!
Map,List 等都是泛型变量(Raw type),所以我们这里要声明最后的类型,来保证泛型安全,否则是会报 Warning 的
作为一个有良好修养的程序猿肯定要有这种良好习惯。
(PS: 顺便晒下博主的代码高亮配色)
结语
最后其实,真的,已经违背我的原意了
原来是想通过类似数组的方法实现结构化数据存储,现在可能还得不断往内嵌套 List,Map,整个结构极其复杂,一对多映射操作非常麻烦
所以最后:老老实实地建个抽象类,建个对象吧
@coder-bts, steemit上我觉得只需要静静读你的技术贴就值了~~~
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
哈哈,非常感谢!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
的确这样,用object简单清楚点
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
清楚很多
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit