Java.Collections,工具类

// Collections,工具类
	public static void main(String[] args) {
		List<Integer> list = new ArrayList<Integer>();

		// Collections,为List增加内容
		Collections.addAll(list, 10, 1, 2, 34, 5, 6, 653, 211, 23);
		Iterator<Integer> iterator = list.iterator();

		while (iterator.hasNext()) {
			Integer integer = iterator.next();
			System.out.print(integer + ".");
		}

		System.out.println("==============================");
		Collections.reverse(list);// 反转内容

		Iterator<Integer> iterator2 = list.iterator();
		while (iterator2.hasNext()) {
			Integer integer = iterator2.next();
			System.out.print(integer + ".");
		}

		System.out.println("==============================");
		// 检索内容,输出位置
		int i = Collections.binarySearch(list, 5);
		System.out.println("位置在: " + i);
		
		System.out.println("==============================");
		//替换内容
		if (Collections.replaceAll(list, 23, 30)) {
			System.out.println("替换成功");
		}
		
		System.out.println("==============================");
		//排序
		//要求排序的对象实现了Comparable接口
		List<String> arrayList = new ArrayList<String>();
		
		Collections.addAll(arrayList, "123","qwe","asd","34");
		Collections.addAll(arrayList, "we");
		Collections.addAll(arrayList, "看看","不小心");
		
		Collections.sort(arrayList);
		Iterator<String> iterator3 = arrayList.iterator();
		while(iterator3.hasNext()){
			String next = iterator3.next();
			System.out.print(next+".");
		}

		System.out.println("==============================");
		//交换指定位置的元素
		List<Integer> arrayList2 = new ArrayList<Integer>();
		Collections.addAll(arrayList2, 1,2,3);
		Collections.swap(arrayList2, 0, 2);//交换位置1和位置3的
		
		Iterator<Integer> iterator4 = arrayList2.iterator();
		while (iterator4.hasNext()) {
			Integer next = iterator4.next();
			System.out.print(next+" ");
		}

你可能感兴趣的:(Java.Collections,工具类)