黑马程序员技术交流社区
标题:
有没有 简单一点的?????
[打印本页]
作者:
张可可
时间:
2016-6-1 23:00
标题:
有没有 简单一点的?????
(1)有如下代码:
List<String> list = new ArrayList<>();
list.add("a");
list.add("a");
list.add("a");
list.add("b");
list.add("b");
list.add("c");
list.add("d");
list.add("d");
list.add("d");
list.add("d");
list.add("d");
System.out.println(frequency(list, "a")); // 3
System.out.println(frequency(list, "b")); // 2
System.out.println(frequency(list, "c")); // 1
System.out.println(frequency(list, "d")); // 5
System.out.println(frequency(list, "xxx")); // 0
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("a");
list.add("a");
list.add("a");
list.add("b");
list.add("b");
list.add("c");
list.add("d");
list.add("d");
list.add("d");
list.add("d");
list.add("d");
System.out.println(frequency(list, "a")); // 3
System.out.println(frequency(list, "b")); // 2
System.out.println(frequency(list, "c")); // 1
System.out.println(frequency(list, "d")); // 5
System.out.println(frequency(list, "xxx")); // 0
System.out.println("a "+ frequency(list, "a") +",b " + frequency(list, "b") +
",c "+frequency(list, "c")+ ",d "+ frequency(list, "d"));
}
public static int frequency(List<String> list, String string) {
String [] arr = list.toArray(new String[list.size()]);
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (string.equals(arr[i])) {
count++;
}
}
return count;
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2