Java笔记

来源:互联网 发布:淘宝升钻有什么好处 编辑:程序博客网 时间:2024/06/14 19:25

1. Collections类
Collections类包含在java.util包中:此类完全由在 collection 上进行操作或返回 collection 的静态方法组成。(也就是说Colloections类中的方法全是静态方法)
常用方法:
1. 对List集合排序
在Collections类中有两个静态方法
public static < T extends Comparable< ? super T>> void sort(List list):根据元素的自然顺序对指定列表按升序进行排序。列表中的所有元素都必须实现 Comparable 接口。(所以是T extends Comparable,Comparable接口也带着泛型,取出来比较的元素可以用本身来接受,也可以用父类来接受,所以Comparable接口的泛型是< ? super T>)
public static < T> void sort(List< T> list, Comparator< ? super T> c):根据指定比较器产生的顺序对指定列表进行排序。此列表内的所有元素都必须可使用指定比较器相互比较。

List<String> list = new ArrayList<String>();list.add("abc");list.add("df");list.add("yum");list.add("e");list.add("uopi");System.out.println(list);Collections.sort(list);System.out.println(list);

输出结果:
[abc, df, yum, e, uopi]
[abc, df, e, uopi, yum]

2. 二分查找
public static < T> int binarySearch(List< ? extends Comparable< ? super T>> list, T key):使用二分搜索法搜索指定列表,以获得指定对象。在进行此调用之前,必须根据列表元素的自然顺序对列表进行升序排序(通过 sort(List) 方法)。
public static < T> int binarySearch(List< ? extends T> list,T key,Comparator< ? super T> c):使用二分搜索法搜索指定列表,以获得指定对象。在进行此调用之前,必须根据指定的比较器对列表进行升序排序(通过 sort(List, Comparator) 方法)。

//继续使用上面的List集合Collections.sort(list);int i = Collections.binarySearch(list, "e");System.out.println(i);

输出结果:
2

3. 获取元素的最大值和最小值
public static < T extends Object & Comparable< ? super T>> T max(Collection< ? extends T> coll):根据元素的自然顺序,返回给定 collection 的最大元素。collection 中的所有元素都必须实现 Comparable 接口。
public static < T> T max(Collection< ? extends T> coll, Comparator< ? super T> comp):根据指定比较器产生的顺序,返回给定 collection 的最大元素。

//使用上面的List集合Collections.sort(list);String str_max = Collections.max(list);System.out.println("str_max:"+str_max);String str_min = Collections.min(list);System.out.println("str_min:"+str_min);

输出结果:
str_max:yum
str_min:abc

4. 替换
public static < T> boolean replaceAll(List< T> list, T oldVal, T newVal):使用另一个值替换列表中出现的所有某一指定值。

//使用上面的List集合Collections.replaceAll(list, "abc", "cba");Iterator<String> it = list.iterator();while (it.hasNext()) {    System.out.println(it.next());}System.out.println("-----------------------");Collections.fill(list, "aaa");Iterator<String> it1 = list.iterator();while (it1.hasNext()) {    System.out.println(it1.next());}

输出结果:
cba
df
yum
e
uopi


aaa
aaa
aaa
aaa
aaa

5. 随机
public static void shuffle(List< ?> list)使用默认随机源对指定列表进行置换。

//使用上面的List集合Collections.shuffle(list);Iterator<String> it = list.iterator();while (it.hasNext()) {    System.out.println(it.next());}

运行发现每次运行的结果都不相同,随机排序

6. 倒叙
public static < T> Comparator< T> reverseOrder():返回一个比较器,它强行逆转实现了 Comparable 接口的对象 collection 的< font color = #FF000>自然顺序。
public static < T> Comparator< T> reverseOrder(Comparator< T> cmp):返回一个比较器,它强行逆转指定比较器的顺序

TreeSet<Student> ts = new TreeSet<Student>(Collections.reverseOrder(new ComparatorByName()));ts.add(new Student("zhangsan",21));ts.add(new Student("lisi",22));ts.add(new Student("wangwu",24));ts.add(new Student("zhaliu",27));ts.dd(new Student("sunqi",20));Iterator<Student> it = ts.iterator();while (it.hasNext()) {    System.out.println(it.next());}

输出结果:
Student:zhangsan:21
Student:zhaliu:27
Student:wangwu:24
Student:sunqi:20
Student:lisi:22

7. 给非同步的集合加锁
public static < T> Collection< T> synchronizedCollection(Collection< T> c):返回指定 collection 支持的同步(线程安全的)collection。

List list = new ArrayList();//非同步的。list = MyCollections.synchronizedList(list);//返回一个同步的list.

2. Arrays类
Arrays类中的方法不多
二分查找:boolean型的查不了
equals:比较数组是否相同,相同顺序且相同元素则返回true。
排序sort,toString,fill……大部分都是方法的重载。
但是如果我们把数组转为集合,那么方法就比较多了。
public static < T> List< T> asList(T… a):返回一个受指定数组支持的固定大小的列表。

String[] str = {"sd","sd","sdxcv"};List<String> str_list = Arrays.asList(str);System.out.println(str_list.contains("sd"));

输出结果:
true

注意

在数组转成集合后,如果想要对集合中的元素进行增删,运行时就会报错

str_list.add("a");

输出结果
java.lang.UnsupportedOperationException
因为数组的长度是固定的,所以对于集合的增删方法是不可以使用的。

int[] arr = {324,76,789,345,98,3};List<int[]> arr_list = Arrays.asList(arr);System.out.println(arr_list);

输出结果:
[[I@1db9742]

输出结果是一个哈希值。因为如果数组中的元素是对象,那么转成集合时,直接将数组中的元素作为集合中的元素进行存储,所以String数组转为集合时,集合中存储的是字符串对象。
如果数据中的元素是基本类型数值,因为基本数据类型是不能够存储到集合中的,那么会将该数组作为集合中的元素进行存储,把引用类型arr作为对象存储到集合中。

3. 增强型for循环 For-Each
在JDK1.5的版本后加入了一个增强型的For循环就是For-Each。
For-Each循环的加入简化了集合的遍历。
语法格式:
for(type element: array)
{
System.out.println(element);
}

List<String> list = new ArrayList<String>();list.add("abc");list.add("xcv");list.add("rfg");list.add("nf");for (String str : list) {    System.out.println(str);}System.out.println("--------------------");int[] arr = {234,546,8,98,345,9786};for (int i : arr) {    System.out.println(i);}

输出结果:
abc
xcv
rfg
nf


234
546
8
98
345
9786
我们可以使用For-Each来遍历集合和数组

传统for和高级for的区别?
传统for可以完成对语句执行很多次,因为可以定义控制循环的增量和条件。高级for是一种简化形式。
它必须有被遍历的目标。该目标要是数组,要么是Collection单列集合。
对数组的遍历如果仅仅是获取数组中的元素,可以使用高级for。
如果要对数组的角标进行操作建议使用传统for,因为For-Each循环的缺点就是丢掉了索引信息。

4. 静态导入static import
静态导入是JDK5.0版本加入的新特性。
使用静态导入可以使被导入类的静态变量和静态方法在当前类直接可见,使用这些静态成员无需再给出他们的类名。

import static java.util.Collections.*;//静态导入,其实导入的是类中的静态成员。public static void main(String[] args) {    List<String> list = new ArrayList<String>();    list.add("abc3");    list.add("abc7");    list.add("abc1");    System.out.println(list);    sort(list);//不用再写Collections.sort(list);    System.out.println(list);       }

使用静态导入方便书写,但是降低了阅读性,并且在类名重复的时候是不可用的。

5. 可变参数
Java1.5增加了新特性:可变参数:适用于参数个数不确定,类型确定的情况,java把可变参数当做数组处理。
注意:可变参数类型,必须定义在参数列表的结尾。

public static void main(String[] args) {    int sum = newAdd(23,3467,392,75423,2);    System.out.println(sum);}public static int newAdd(int a,int b,int...  arr){//第一个数给a,剩下的都是arr    int sum = 0;    for (int i = 0; i < arr.length; i++) {        sum+=arr[i];    }    return sum;}

输出结果:
75817(后三个数之和)

原创粉丝点击