第十八次学习Java
1、
package demo01;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
public class Demo01Collection {
public static void main(String[] args) {
Collection<String> coll= new HashSet<>();
System.out.println(coll);
boolean b1=coll.add("大古");
System.out.println(b1);
System.out.println(coll);
coll.add("大古");
coll.add("飞鸟");
coll.add("我梦");
coll.add("未来");
System.out.println(coll);
boolean b2=coll.remove("飞鸟");
System.out.println(b2);
System.out.println(coll);
boolean b3=coll.contains("我梦");
System.out.println(b3);
boolean b4=coll.contains("未来");
System.out.println(b4);
boolean b5=coll.isEmpty();
System.out.println(b5);
int b6=coll.size();
System.out.println(b6);
Object[] arr=coll.toArray();
System.out.println("==================");
coll.clear();
System.out.println(coll);
b5=coll.isEmpty();
System.out.println(b5);
}
}
2、
package demo01;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo02lterator {
public static void main(String[] args) {
Collection coll= new ArrayList<>();
coll.add("迪迦");
coll.add("戴拿");
coll.add("盖亚");
coll.add("阿古茹");
coll.add("梦比优斯");
coll.add("诺亚");
Iterator<String> it= coll.iterator();
if(it.hasNext()) {
String e=it.next();
System.out.println("e");
}
System.out.println("==================");
while(it.hasNext()) {
String e=it.next();
System.out.println("e");
}
System.out.println("===================");
for(Iterator<String> it2=coll.iterator();it2.hasNext();) {
String e=it2.next();
System.out.println(e);
}
System.out.println("=========第三次==========");
for(String s:coll) {
System.out.println(s);
}
System.out.println("==========第四次=========");
int[] arr1= {45,6,7,8,9,10};
for(int i=0;i<arr1.length;i++);
{
System.out.println();
}
for(int i:arr1) {
System.out.println(i);
}
}
}
3、
package demo02;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo01Exception {
public static void main(String[] args) {
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd”);
Date date=null;
try {
date=sdf.parse("2021-05-11");
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("第一句测试");
System.out.println(date);
int[] arr= {1,2,3};
System.out.println(arr[0]);
try {
System.out.println(arr[3]);
}catch(Exception e) {
System.out.println("第二句测试");
System.out.println(e);
}
int[] arr2= new int[1024*1024*1024*1024*1024*1024*1024*1024*1024*1024*1024];
System.out.println();
}
}
4、
package demo02;
public class Demo02Throw {
public static void main(String[] args) throws Exception {
int[] arr=null;
int e=getElement(arr,3);
System.out.println(e);
}
public static int getElement(int[] arr, int index) {
if (arr==null) {
throw new NullPointerException("传递的数组值是Null");
}
if(index<0||index>arr.length-1) {
throw new ArrayIndexOutOfBoundsException("传递的索引超出了数组的正常使用范围");
}
int ele=arr[index];
return ele;
}
}
5、
package demo02;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Demo03Throws {
public static void main(String[] args) throws Exception {
readFile(“c:\a”);
System.out.println(“后续代码”);
}
public static void readFile(String fileName) throws FileNotFoundException,IOException{
if(!fileName.equals("c:\\a.txt")) {
throw new FileNotFoundException("传递的文件不是c:\\a.txt");
}
if(!fileName.endsWith(".txt")) {
throw new IOException("文件后缀名有无");
}
System.out.println("文件正确");
}
}