day19

工具类

collections
//所有方法都是静态方法
1. 排序
List<String> list = new ArrayList<String>();
Collections.sort(list);
//public static <T extends Comparable<? super T>> void sort(List<T> list)
//用于list有自然顺序且需要按自然顺序进行排序
Collections.sort(list,new ComparablebyLength());
//public static <T> void sort(List<T> list,Comparator<? super T> comp)
//用自己编辑的比较器进行排序
2. 获取最值
Collections.max(list);
Collections.max(list,new ComparablebyLength());
3. 折半查找
Collections.binarySearch(list,"aa");//使用前需要先排序
4. 逆序
//向构造函数的参数内传入一个比较器
List<String> list = new ArrayList<String>(Collections.reverseOrder());//自然顺序逆序
ArrayList<String>(Collections.reverseOrder(new ComparablebyLength()));//按照长度逆序
5. 替换
Collections.replaceAll(list,"aaa","ddd");//将list中的aaa替换成ddd
Collections.fill(list,"cc");//将List的所有元素替换为cc,一般用于初始化
6. 加锁(重点)
synchronizedXxx(Xxx x)//Collection,List,Map,Set,返回同步集合
Arrays方法
Arrays.toString();
sort();
hashCode();
fill();
equals();
copyOf();
binarySearch();
asList(数组);//将数组转换成集合,这样就可以用集合的方法操作数组
List<String> list = Arrays.asList(arr);
//但是不能操作集合的增删操作,因为数组长度是固定的

Tip:

  • 如果数组中的元素是对象,转成集合时,直接将数组中的元素作为集合中的元素进行存储
  • 如果数组中的元素是基本类型数值,则会将数组作为集合中的元素进行存储,因为集合不能存基本类型数值
int[] arr = {22,435,35,2,5};
List<int[]> list = Arrays.asList(arr);
System.out.println(list);//结果为一个哈希值

集合转数组?

  • 使用Collection接口的toArray方法
  • toArray()需要传入一个指定类型的数组,数组长度要等于集合的长度
List<String> list = new ArrayList<String>();
list.add("abc1");
list.add("abc2");
list.add("abc3");
String[] arr = list.toArray(new String[list.size()]);
foreach语句

for(类型 变量:Collection集合或数组)

List<String> list = new ArrayList<String>();
list.add("abc1");
list.add("abc2");
list.add("abc3");
for(String s : list){
  System.out.println(s);
}//传统迭代器的简写格式,但是该方法不能对元素进行过多操作,一般只能用来取出元素
函数可变参数
public static int newAdd(int...arr){}
int a = newAdd(3,5,6,7,3);
int b = newAdd(23,43);
asList(T....t);
静态导入
import static java.util.Collections.*;
//导入静态成员
其他类
  • System类中的方法和属性都是静态的
  • Runtime类中没有构造方法,说明该类不可以创建对象,而类中又有非静态方法,说明该类提供一个静态方法返回该类的对象,称为单例设计模式
public static void main(String[] args) throws IOException{
  Runtime r = Runtime.getRuntime();
  r.exec("notepad.exe c:\\runtimedemo.java");//选用匹配的程序执行匹配的文件
  //Process p = r.exec("notepad.exe");
  //p.destroy();//杀掉子线程
}
  • Meth类
ceil();//向上取整
floor();//向下取整
round();//四舍五入
pow(a,b);//a**b
random();//生成【0,1】的伪随机数
double b = (int) (3.879);//b=3.0
int a = (int) (3.7546);//b=3
  • Date类
Date date = new Date();//将当前时间封装成对象
Date date = new Date(123343546);//将指定毫秒值转成日期
毫秒值-->日期对象
 1.new Date(time);
 2.setTime();
日期对象-->毫秒值
 getTime();
compareTo();//比较两个日期的顺序
toString();

日期对象–>字符串:format()

Date date = new Date();
DateFormat dateformat = DateFormat.getDateInstance();//默认风格,输出为2019-12-23
DateFormat dateformat = DateFormat.getDateInstance(DateFormat.FULL);//还有FULL,LONG等风格,也可以自定义风格,输出为2019年12月23日 星期一
DateFormat dateformat = DateFormat.getDateTimeInstance();//输出为2019-12-23 10:33:53
String s = dateformat.format(date);

字符串–>日期对象:parse()

public static void method() throws ParseException{
  String s = "2019年12月23日";
  String s2 = "2019---12-25";
  DateFormat dateformat = DateFormat.getDateInstance(DateFormat.LONG);//要使用对应风格对字符串进行解析
  DateFormat dateformat2 = new SimpleDateFormat("yyyy---MM-d");
  Date date = dateformat.parse(s);
  Date date2 = dateformat2.parse(s2);  
}

Calendar类

  • Date类中过时的类都被Calendar类替代了
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;//月份是从0开始的
c.set(2014,12,22);
c.add(Calendar.MONTH,2);//日期偏移
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值