[英]Android: Hashmap or Sequence in Java
什么更适合用于在我的应用程序中实现[创建键/值并处理输入]基于引用--->这里<---
这两个是由不同的用户提供给我的,所以我希望有人可以告诉我在操作速度和编码效率方面有什么用处
使用sequence
,但我仍然很难实现它。
//Those are fields:
private final List<List<String>> sequences;
private final List<String> currentSequence;
//This is a method you'll have to call from onCreate
private void initSequences(){
sequences = new ArrayList<ArrayList<String>>();
sequences.add(Arrays.asList("button1")); //A
sequences.add(Arrays.asList("button1", "button3")); //B
sequences.add(Arrays.asList("button1", "button2")); //C
...}
// This goes where you do the timer thing
currentSequence= new ArrayList<String>();
// For every new timer you must add the to the list the keypress
currentSequence.add(/*the key you registered*/);
// When a timer finishes its time and you finished to register the keypresses, lets say we will print the output letter with a syso
if(sequences.contains(currentSequence)){
System.out.println( sequences.indexOf(currentSequence)+97 );
} else {
System.out.println( "sequence not correct" );
}
currentSequence.clear();
或使用map
,但问题是这只是一个提供给我的伪代码。 我不知道如何实现它..
map = {[R.id.block1],'A',
[R.id.block1, R.id.block2],'B',
[R.id.block1, R.id.block2, R.id.block2],'C',
etc...
}
所以我已经完成了以下步骤
我无法得到它。 :(请帮忙。谢谢。
如果你在Key - > Value对的情况下寻找最好的东西,你应该检查ArrayMaps
设计的ArrayMaps
。 它们类似于HashMaps
,但内存效率更高。
您可以在此处查看此示例:
以防万一,这里也是HashMap
一个例子:
Map<String, String> map = new HashMap<String, String>();
map.put("145", "PentHouse");
map.put("125", "BoardRoom");
map.put("145", "PentHouse");
String room = map.get("8");
// room will be null if "8" isn't a key in the map.
在HashMap
使用List
示例(对于数组类似):
HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>();
HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>();
map.put("Something", new ArrayList<Integer>());
for (int i=0;i<lenghtOfList; i++) {
map.get("Something").add(item[i]);
}
使用HashMap
要比每次搜索Arrays
的ArrayList
快得多。 我会将HashMap
的键作为按钮编号的串联,然后我将该值作为字母数字字符。 所以你的HashMap将是<String, String>
,并且Map中的样本值将是["13": "B"]
。 检查按钮组合是否有效时,您将检查hashmap.get(buttonComboString) != null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.