聪聪工作室---Codingbat练习---Map-2 总结
2016-07-29 本文已影响458人
繁花流水congcong
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class word0 {
public static void main(String[] args) {
String[] strings = { "addd", "bddd", "b", "a", "a", "b", "b", "a", "a", "c", "c", "b", "b", "a", "a", "b", "b",
"a", "a", "c", "c" };
// System.out.println(word0(strings));
//
// System.out.println(wordMultiple(strings));
// System.out.println(wordLen(strings));
// System.out.println(firstChar(strings));
// allSwap(strings);
// System.out.println(Arrays.toString(strings));
// System.out.println(pairs(strings));
// System.out.println(wordAppend(strings));
System.out.println(Arrays.toString(firstSwap(strings)));
}
public static Map<String, Integer> word0(String[] strings) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (String s : strings) {
map.put(s, 0);
}
return map;
}
word0.png
public static Map<String, Integer> wordCount(String[] strings) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (String s : strings) { // s是指strings数组中的数;
System.out.println(map.get(s)); // map.get() 自动统计在map数组中元素出现的次数,从0开始
if (map.get(s) != null) {
map.put(s, map.get(s) + 1);// 从0开始,所以要加1,s对应的关联值加1 变成2 ,因为null+1
// 不知道运算方法
} else {
map.put(s, 1);// 如果是等于null 的,出现1次后,自动赋值为1
}
}
System.out.println("统计输出: ");
for (String s : map.keySet()) { // 把数组中的元素罗列出来,不允许重复
System.out.println(" " + s + " : " + map.get(s));// s是罗列出的元素
// ,map.get(s)
// 在上一步循环中已经进行+1处理,直接映射数组元素的关联值,也就是这个数组中所存在的相同元素的个数
}
return map;
}
wordCount.png
public static Map<String, Boolean> wordMultiple(String[] strings) {
HashMap<String, Integer> map1 = new HashMap<String, Integer>();
for (String s : strings) {
if (map1.get(s) == null) {
map1.put(s, 1);
} else if (map1.get(s) != null) {
map1.put(s, map1.get(s) + 1);
}
}
Map<String, Boolean> map2 = new HashMap<String, Boolean>();
for (String s1 : map1.keySet()) {
if (map1.get(s1) % 2 >= 0) {
map2.put(s1, true);
} else {
map2.put(s1, false);
}
}
return map2;
}
wordMultiple.png
public static Map<String, Integer> wordLen(String[] strings) {
HashMap<String, Integer> map3 = new HashMap<String, Integer>();
for (String s : strings) {
map3.put(s, 0);
}
for (String s1 : map3.keySet()) {
System.out.println(s1);
int size = s1.length();
map3.put(s1, size);
}
return map3;
}
wordLen.png
public static Map<String, String> firstChar(String[] strings) {
HashMap<String, String> map = new HashMap<String, String>();
for (String s : strings) {
String f = s.charAt(0) + "";
if (!map.containsKey(f)) {
map.put(f, s);
} else {
map.put(f, map.get(f) + s);
}
}
return map;
}
firstChar.png
public static String[] allSwap(String[] strings) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < strings.length; i++) {
String f = strings[i].charAt(0) + "";
if (!map.containsKey(f)) {
map.put(f, i);
} else {
String temp = strings[i];
strings[i] = strings[map.get(f)];
strings[map.get(f)] = temp;
map.remove(f);
}
}
return strings;
}
allSwap.png
public static Map<String, String> pairs(String[] strings) {
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < strings.length; i++) {
String index = strings[i].charAt(0) + "";
String end = strings[i].charAt(strings[i].length() - 1) + "";
map.put(index, end);
}
return map;
}
pairs.png
public static String wordAppend(String[] strings) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
String result = "";
for (String s : strings) {
if (!map.containsKey(s)) {
map.put(s, 1);
} else {
map.put(s, map.get(s) + 1);
}
System.out.println(s + ":" + map.get(s));
if (map.get(s) == 2) {
result = result + s;
map.remove(s);
}
}
return result;
}
wordAppend.png
public static String[] firstSwap(String[] strings) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
for (int i = 0; i < strings.length; i++) {
String f=strings[i].charAt(0)+"";
if(!map.containsKey(f)){
map.put(f, i);
}else if(map.get(f)==-1){
String temp=strings[i];
strings[i]=strings[map.get(f)];
strings[map.get(f)]=temp;
map.put(f, -1);
}
}
return strings;
}
}
firstSwap.png
引用
引用引用的引用