数据结构
// 栈:先进后出; // 队列:先进先出; // 数组:查询快,增删慢; // 链表:查询慢,增删快; // 红黑树:查询快(二分查找);
collection
public class practice { public static void main(String[] args) { Collection<String> list = new ArrayList<String>(); list.add("linlong"); list.add("dingcaiyan"); list.add("linlong"); list.add("linjingcheng"); System.out.println(list); // 集合通用方法 // if (list.contains("linlong")) { // list.remove("linlong"); // } // System.out.println(list); // list.clear(); // System.out.println(list.size()); // Object[] array = list.toArray(); // 迭代器 // Iterator<String> iterator = list.iterator(); // while (iterator.hasNext()) { // String next = iterator.next(); // System.out.println(next); // } // 增强for循环 for (String str : list) { System.out.println(str); } } }
扑克牌案例:
public class practice { public static void main(String[] args) { String[] teshu = {"大王", "小王"}; String[] huase = {"♠", "♥", "♣", "♦"}; String[] shuzizimu = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"}; ArrayList<String> all = new ArrayList<>(); all.add(teshu[0]); all.add(teshu[1]); for (String n : shuzizimu) { for (String m : huase) { all.add(n + m); } } ArrayList<Integer> alllist = new ArrayList<>(); Map<Integer, String> allmap = new HashMap<>(); for (int i = 0; i < all.size(); i++) { alllist.add(i); allmap.put(i, all.get(i)); } Collections.shuffle(alllist); ArrayList<Integer> zhourunfa = new ArrayList<>(); ArrayList<Integer> liudeihua = new ArrayList<>(); ArrayList<Integer> zhouxingchi = new ArrayList<>(); ArrayList<Integer> dipai = new ArrayList<>(); for (int i = 0; i < alllist.size(); i++) { if (i >= 51) { dipai.add(alllist.get(i)); } else if (i % 3 == 0) { zhourunfa.add(alllist.get(i)); } else if (i % 3 == 1) { liudeihua.add(alllist.get(i)); } else if (i % 3 == 2) { zhouxingchi.add(alllist.get(i)); } } Collections.sort(zhourunfa); Collections.sort(liudeihua); Collections.sort(zhouxingchi); Collections.sort(dipai); ArrayList<String> zhourunfasort = new ArrayList<>(); ArrayList<String> liudeihuasort = new ArrayList<>(); ArrayList<String> zhouxingchisort = new ArrayList<>(); ArrayList<String> dipaisort = new ArrayList<>(); for (Integer item : zhourunfa) { zhourunfasort.add(allmap.get(item)); } for (Integer item : liudeihua) { liudeihuasort.add(allmap.get(item)); } for (Integer item : zhouxingchi) { zhouxingchisort.add(allmap.get(item)); } for (Integer item : dipai) { dipaisort.add(allmap.get(item)); } System.out.println(zhourunfasort); System.out.println(liudeihuasort); System.out.println(zhouxingchisort); System.out.println(dipaisort); } }
list+set
// ArrayList // ArrayList<Integer> arrayList = new ArrayList<>(); // arrayList.add(1); // arrayList.add(2); // arrayList.add(3); // arrayList.add(1, 4); // arrayList.remove(2); // arrayList.set(1, 2); // System.out.println(arrayList); // System.out.println(arrayList.get(1)); // LinkedList // LinkedList<Integer> linkedList = new LinkedList<>(); // linkedList.add(1); // linkedList.add(2); // linkedList.add(3); // linkedList.addFirst(1);//等价于linkedList.push(1); // System.out.println(linkedList); // linkedList.addLast(3);//等价于linkedList.add(3); // System.out.println(linkedList); // System.out.println(linkedList.getFirst()); // System.out.println(linkedList.getLast()); // linkedList.removeFirst();//等价于linkedList.pop(); // linkedList.removeLast(); // System.out.println(linkedList); // System.out.println(linkedList.isEmpty()); // linkedList.clear(); // System.out.println(linkedList); // hashset存储的数据:先比较hashcode,再比较equals,都相同则不存储 // HashSet<Integer> hashSet = new HashSet<>(); // hashSet.add(2); // hashSet.add(3); // hashSet.add(1); // hashSet.add(1); // System.out.println(hashSet); // hashset存储自定义类型元素,必须重写hashcode和equals方法(快捷键Alt+Insert自动生成) // LinkedHashSet元素顺序与add时的顺序一致,hsahset元素顺序与add时的元素顺序不一定一致 // 可变参数 // public void method(Object... obj) { // } // method(1,2,3) // 参数列表只能有一个可变参数,且可变参数必须在参数列表的末尾
Collections工具类
ArrayList<Integer> arrayList = new ArrayList<>(); arrayList.add(4); arrayList.add(2); arrayList.add(6); Collections.addAll(arrayList, 5, 8, 3); System.out.println(arrayList); // Collections.sort如果是自定义类型,该自定义类型必须实现Comparable接口;return this - 参数(升序) Collections.sort(arrayList); System.out.println(arrayList); Collections.sort(arrayList, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { int res = o2 - o1;//降序 if (res == 0) { //其他属性进行排序 } return res; } }); System.out.println(arrayList); Collections.shuffle(arrayList);//随机排序 System.out.println(arrayList);
map
HashMap<Integer, String> hashMap = new HashMap<>(); hashMap.put(2, "林志玲"); hashMap.put(3, "佟丽娅"); hashMap.put(1, "孟美岐"); hashMap.put(1, "林允儿"); //System.out.println(hashMap); //hashMap.remove(1); //System.out.println(hashMap); //System.out.println(hashMap.get(2)); //System.out.println(hashMap.containsKey(2)); //遍历map //for (Integer key : hashMap.keySet()) { // System.out.println(hashMap.get(key)); //} //for (Map.Entry<Integer, String> entry : hashMap.entrySet()) { // System.out.print(entry.getKey() + ":"); // System.out.println(entry.getValue()); //} //hashMap如果key为自定义类型,该自定义类型必须重写hashcode和equals方法(Alt+Insert自动生成) //LinkedHashMap元素顺序与add时的顺序一致,hashMap元素顺序与add时的顺序不一定一致 //Hashtable:key或value不能为null
map案例
Scanner scanner = new Scanner(System.in); System.out.println("请输入字符串"); String next = scanner.next(); HashMap<Character, Integer> hashMap = new HashMap<>(); for (Character character : next.toCharArray()) { if (hashMap.containsKey(character)) { Integer integer = hashMap.get(character); integer++; hashMap.put(character, integer); } else { hashMap.put(character, 1); } } for (Character character : hashMap.keySet()) { System.out.println(character + ":" + hashMap.get(character)); }
of方法
//1、of方法只适用于List接口、Set接口、Map接口,不适用于接口的实现类; //2、of方法的返回值是一个不能改变的集合,集合不能在使用add、put方法添加元素,会抛出异常; //3、Set接口和Map接口在调用of方法的时候,不能有重复的元素,否则会抛出异常; List<Integer> list = List.of(1, 2, 3); System.out.println(list); Set<Integer> set = Set.of(1, 2, 3); System.out.println(set); Map<Integer, String> map = Map.of(1, "林志玲", 2, "佟丽娅", 3, "林允儿"); System.out.println(map);