添加多个元素
List<String> list=List.of("jiuzhe","aaa");
过滤操作
list.stream().filter(name->name.startsWith("zhnag")).filter(name->name.length()==3)
.foreach.sout(name);
list转化为数组
ArrayList<Integer> list=new ArrayList<>();
Collections.addAll(list,1,2,3,4,0);//添加元素
Integer arr[]=list.stream().toArray(Integer[]::new);
System.out.println(Arrays.toString(arr));
throws
public static void main(String[] args) throws ParseException {
String time="2020年1月1日";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
Date date=sdf.parse(time);//转换
System.out.println(date);
}
异常处理//需要注意的是,try里面出错了后面的内容就不执行了,直接跳到catch
父类的异常写在下面,如果catch的内容与实际错误不符合,相当于catch白写了,依旧会报错
try catch 的快捷键 ctrl +alt+t
public static void main(String[] args) {
int arr[]={1};
try {
System.out.println(2/0);
String s=null;
System.out.println(s.equals("abc"));
System.out.println(arr[2]);
}catch (ArrayIndexOutOfBoundsException | ArithmeticException e){
System.out.println("索引或者算术");
}catch(NullPointerException e){
System.out.println("空指针异常");
}catch (Exception e){
System.out.println("Exception");
}
}
判定条件
if(arr==null) throw new NullPointerException();
if(arr.length==0) throw new ArrayIndexOutOfBoundsException();
文件
public static void main(String[] args) {
String str="C\\a.txt";
File file=new File(str);
System.out.println(file);
public File(String pathname)
public File(String parent,String child)
public File(File parent,String child);
}