[Java视频笔记]day17

集合框架的工具类。

Collections:

.BinarySearch()

.sort()

.max()方法

import java.util.*;

class day17 
{
	public static void main(String[] args) 
	{
		sortDemo();
		maxDemo();
		binarySearch();
	}
	public static void binarySearch()
	{
		List<String> list = new ArrayList<String>();
		list.add("aaa");
		list.add("cdf");
		list.add("ddf");
		list.add("bsssdf");
		Collections.sort(list);
		sop(list);
		//list必须是有序的
		int index = Collections.binarySearch(list, "aaa");
		sop(index);//输出0
		index =  Collections.binarySearch(list, "aaaa");
		sop(index);//输出-2,不存在找不到,返回 -(插入点)-1
		//也可以传入比较器
		//Collections.sort(list, new StrLenComparator());
		//index = Collections.binarySearch(list, "aaa", new StrLenComparator());
	}
	public static void maxDemo()
	{
		List<String> list = new ArrayList<String>();
		list.add("abdcd");
		list.add("cdf");
		list.add("ddf");
		list.add("bsssdf");
		String max = Collections.max(list);
		sop(max);//输出ddf
		//max = Collections.max(list, new StrLenComparator());
		//sop(max);//输出bsssdf
	}
	public static void sortDemo()
	{
		List<String> list = new ArrayList<String>();
		list.add("abdcd");
		list.add("fdf");
		list.add("edf");
		list.add("ssssdf");
		sop(list);
		Collections.sort(list, new StrLenComparator());//按长度排序
		sop(list);
	}
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

class StrLenComparator implements Comparator<String>
{
	public int compare(String s1,String s2)
	{
		int num = s1.length() - s2.length();
		if(num == 0)
			return s1.compareTo(s2);
		return num;
	}
}


fill方法可以将List集合中的所有元素替换成指定元素。

将list集合中部分元素替换成指定元素。

fill()方法

replaceAll()方法

reverse()方法

import java.util.*;

class day17 
{
	public static void main(String[] args) 
	{
		replaceAllDemo();
	}

	public static void replaceAllDemo()
	{
		List<String> list = new ArrayList<String>();
		list.add("aaa");
		list.add("cdf");
		list.add("ddf");
		list.add("bsssdf");
		sop(list);
		Collections.replaceAll(list, "aaa", "pp");
		sop(list);//输出[pp, cdf, ddf, bsssdf]
		Collections.reverse(list);//反转
		sop(list);//输出[bsssdf, ddf, cdf, pp]
	}

	public static void fillDemo()
	{
		List<String> list = new ArrayList<String>();
		list.add("aaa");
		list.add("cdf");
		list.add("ddf");
		list.add("bsssdf");
		sop(list);
		Collections.fill(list, "pp");//将集合中元素全部替换成"pp"
		sop(list);//输出[pp, pp, pp, pp]
	}
	public static void binarySearch()
	{
		List<String> list = new ArrayList<String>();
		list.add("aaa");
		list.add("cdf");
		list.add("ddf");
		list.add("bsssdf");
		Collections.sort(list);
		sop(list);
		//list必须是有序的
		int index = Collections.binarySearch(list, "aaa");
		sop(index);//输出0
		index =  Collections.binarySearch(list, "aaaa");
		sop(index);//输出-2,不存在找不到,返回 -(插入点)-1
		//也可以传入比较器
		//Collections.sort(list, new StrLenComparator());
		//index = Collections.binarySearch(list, "aaa", new StrLenComparator());
	}
	public static void maxDemo()
	{
		List<String> list = new ArrayList<String>();
		list.add("abdcd");
		list.add("cdf");
		list.add("ddf");
		list.add("bsssdf");
		String max = Collections.max(list);
		sop(max);//输出ddf
		//max = Collections.max(list, new StrLenComparator());
		//sop(max);//输出bsssdf
	}
	public static void sortDemo()
	{
		List<String> list = new ArrayList<String>();
		list.add("abdcd");
		list.add("fdf");
		list.add("edf");
		list.add("ssssdf");
		sop(list);
		Collections.sort(list, new StrLenComparator());//按长度排序
		sop(list);
	}
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

class StrLenComparator implements Comparator<String>
{
	public int compare(String s1,String s2)
	{
		int num = s1.length() - s2.length();
		if(num == 0)
			return s1.compareTo(s2);
		return num;
	}
}

reverseOrder()方法

import java.util.*;

class day17 
{
	public static void main(String[] args) 
	{
		orderDemo();
	}

	public static void orderDemo()
	{

		//TreeSet<String> ts = new TreeSet<String>();
		TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder());
		//将自然顺序强行逆转
		ts.add("abdde");
		ts.add("kkk");

		Iterator it = ts.iterator();
		while(it.hasNext())
		{
			sop(it.next());//输出kkk   abdde
		}

		/*
		TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder(new StrLenComparator()));
		//把自己写的比较器强行逆转,原来按长度递增,现在按长度递减
		ts.add("abdde");
		ts.add("kkk");
		ts.add("dfdfdfdf");

		Iterator it = ts.iterator();
		while(it.hasNext())
		{
			sop(it.next());//依次输出dfdfdf   abdde   kkk
		}
		*/
	}

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}

}

class StrLenComparator implements Comparator<String>
{
	public int compare(String s1,String s2)
	{
		int num = s1.length() - s2.length();
		if(num == 0)
			return s1.compareTo(s2);
		return num;
	}
}

Collections.shuffle(list);//将list集合中的元素随机排序,任意的打乱顺序

 

Arrays:用于操作数组的工具类。

里面都是静态方法。

 

asList方法:将数组变成List集合。

把数组变成List集合有什么好处?

可以使用集合的思想和方法来操作数组中的元素。是否包含某个元素直接用contains方法。

注意:

将数组变为集合以后,不可以使用集合的增删方法,因为数组的长度是固定的,可以使用contains  get  indexOf subList等。如果增删了,会发生UnsupportedOperationException不支持操作异常。

 

如果数组中的元素都是对象。那么变成集合时,数组中的对象直接转变为集合中的元素。如果数组中的元素都是基本数据类型,会将该数组作为集合中的元素存在


import java.util.*;

class day17 
{
	public static void main(String[] args) 
	{

		String[] arr = {"abc", "cc", "kkk"};
		List<String> list = Arrays.asList(arr);
		sop(list);//输出[abc, cc, kkk]
		sop(list.contains("cc"));//很方便判断
		//list.add("hhh");错误

		Integer[] nums = {2, 4, 5};
		List<Integer> li = Arrays.asList(nums);
		sop(li);//输出[2, 4, 5]

		//int[] nums = {2, 4, 5};
		//List<int[]> li = Arrays.asList(nums);
		//sop(li);//输出[[I@2a139a55]  是一个数组的哈希值

	}

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}

}

集合变数组:

Collection接口中的toArray方法

<T> T[] toArray(T[] a)

返回包含此 collection 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同。

 

1.指定类型的数组到底要定义多长呢?

当指定类型的数组长度小于了集合的size,那么该方法内部会创建一个新的数组,长度为集合的size。

当指定类型的数组长度大于了集合的size,就不会新创建数组,而是使用传递进来的数组。所以创建一个刚刚好的数组最优。

2.为什么要将集合变成数组?

为了限定对元素的操作。不让集合再增或者删了。

import java.util.*;

class day17 
{
	public static void main(String[] args) 
	{
		ArrayList<String> al = new ArrayList<String>();

		al.add("abcc0");
		al.add("abcc1");
		al.add("abcc2");

		String[] arr = al.toArray(new String[0]);
		sop(Arrays.toString(arr));//输出[abcc0, abcc1, abcc2]
		//当new String[0]变为new String[5]的话
		//则输出[abcc0, abcc1, abcc2, null, null]
		/*
		1.指定类型的数组到底要定义多长呢?
		当指定类型的数组长度小于了集合的size,那么该方法内部会创建一个新的
		数组,长度为集合的size。
		当指定类型的数组长度大于了集合的size,就不会新创建数组,而是使用传递
		进来的数组。所以创建一个刚刚好的数组最优。
		所以上面要写成 new String[sl.size()]
		*/
		
	}

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}

}

高级for循环

 

格式:

for(数据类型 变量名 : 被遍历的集合(Collection(包含 list 或 set)或者数组)

{

 

}

Map不支持迭代,所以不能用高级for循环。只有支持迭代的才能使用高级for。

对集合进行遍历,只能获取元素,但是不能对集合进行操作。

迭代器除了遍历,还可以进行remove集合中元素的动作。

如果使用ListIterator 还可以在遍历过程中对集合进行增删改查的动作。

 

传统for和高级for有什么区别呢?

高级for有一个局限性。必须有被遍历的目标。比如打印100次”hello world”就不可以。

建议遍历数组的时候,还是使用传统for。因为传统for有脚标。

import java.util.*;

class day17 
{
	public static void main(String[] args) 
	{
		ArrayList<String> al = new ArrayList<String>();
		//如果没加泛型,下面就不能写String s : al

		al.add("abcc0");
		al.add("abcc1");
		al.add("abcc2");
		
		for(String s : al)
		{
			sop(s);
		}

		int[] arr = {3, 5, 1};
		for(int i : arr)
		{
			sop(i);
		}

		HashMap<Integer, String> hm = new HashMap<Integer, String>();
		hm.put(1, "a");
		hm.put(2, "b");
		hm.put(3, "c");

		Set<Integer> keySet = hm.keySet();
		for(Integer i : keySet)
		{
			sop(i+"..."+hm.get(i));
		}

		//Set<Map.Entry<Integer, String>> entrySet = hm.entrySet();
		//for(Map.Entry<Integer, String> me : entrySet)
		//简写成
		for(Map.Entry<Integer, String> me : hm.entrySet())
		{
			sop(me.getKey()+"..."+me.getValue());
		}
	}

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

JDK 1.5版本出现的新特性。

方法的可变参数:

在使用时注意可变参数一定要定义在参数列表的最后面。

比如public static void show(String str, String s, int... arr){}

import java.util.*;

class day17 
{
	public static void main(String[] args) 
	{
		//void show(int[] arr)虽然少定义了多个方法,但是每次都要定义
		//一个数组作为实际参数.

		/*可变参数
		其实就是上一种数组参数的简写形式。
		不用每一次都手动的建立数组对象。
		只要将要操作的元素作为参数传递即可。
		隐士将这些参数封装成了数组.
		*/
		show(2);
		show(2, 3, 5, 9, 12);
		show(2, 4, 4, 2, 56, 9);
	}

	public static void show(int... arr)
	{
		sop(arr.length);
	}
	/*
	public static void show(int[] arr)
	{
	}
	*/
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

JDK 1.5后的新特性

StaticImport 静态导入。

 

当类名重名时,需要指定具体的包名。

当方法重名时,需要指定具体所属的对象或者类。

import java.util.*;
import static java.util.Arrays.*;//导入的是Arrays这个类中的所有静态成员
import static java.lang.System.*;//导入了System类中的所有静态成员
class day17 
{
	public static void main(String[] args) 
	{
		int[] arr = {3, 1, 5};
		sort(arr);
		int index = binarySearch(arr, 1);
		sop(index);//输出0
		sop(Arrays.toString(arr));//这句不能把Arrays去掉
		out.println("haha");//因为导入了static java.lang.System.*
		//前面的System.就不用写了
	}

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值