hashmap 与冒泡排序
MyArrayList.java
package fanxing;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
//119 实现迭代器
//120 HashMap 分拣思路
public class MyArrayList {
public static void main(String[] args) {
String s1=new String("this is a cat and that is a mice");
String[] strArray=s1.split(" ");
Map<String, Word> words =new HashMap<String, Word>();
//为所有的key创建容器
for(String temp:strArray){
if (!words.containsKey(temp)) {
words.put(temp, new Word());
}
else {
Word co=words.get(temp);
co.setCount(co.getCount()+1);
}
}
Set<String> set=words.keySet();
for(String temp:set){
int num=words.get(temp).getCount();
System.out.println(temp+"出的次数:"+num);
}
}
}
word.java
package fanxing;
//120
public class Word {
private String name;
private int count;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
冒泡排序
BubbleSort1.java
package sort;
import java.util.Arrays;
//122 冒泡
public class BubbleSort1 {
public static void main(String[] args) {
int[] arr = new int[5];
arr = new int[] { 12, 9, 6, 3, 0 };
sort(arr);
}
public static void sort(int[] arr) {
for (int j = 0; j < arr.length - 1; j++) {
boolean sorted = true;
System.out.println("第" + (j + 1) + "趟:");
for (int i = 0; i < arr.length - 1 - j; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
sorted = false;
}
System.out.println(Arrays.toString(arr));
}
if (sorted) {
break;
}
}
}
}