1:需求:递归删除带内容的目录 假设删除当前项目下的目录:demo,demo中可以有文件夹自己给出
public class FileDeleteDemo {
public static void main(String[] args) {
// 封装目录
File srcFolder = new File(""D:\\demo\\aaa\\bbb\\ccc"");
// 递归实现
DeleteFolder(srcFolder);
}
private static void DeleteFolder(File srcFolder) {
// 获取该目录下的文件或者文件夹的File数组
File[] fileArray = srcFolder.listFiles();
if (fileArray != null) {
// 遍历该File数组,得到每个File对象
for (File file : fileArray) {
// 判断该File对象是否是文件夹
if (file.isDirectory()) {
DeleteFolder(file);
} else {
System.out.println(file.getName() + "---" + file.delete());
}
}
System.out
.println(srcFolder.getName() + "---" + srcFolder.delete());
}
}
}
2:需求:请大家把E:\JavaSE目录下所有的java结尾的文件的绝对路径给输出在控制台。
public class File_JavaSEDemo {
public static void main(String[] args) {
File file =new File("E:\\JavaSE");
File[] list = file.listFiles();
for(File s:list) {
if(s.isFile()) {
if(s.getName().endsWith("java")) {
System.out.println(s.getName());
}
}
}
}
}
3:下面程序段的执行结果是什么?( B )
public class Foo{
public static void main(String[] args){
try{
return;}
finally{System.out.println(“Finally”);
}
}
}
A.编译能通过,但运行时会出现一个例外。 B.程序正常运行,并输出 “Finally”。
C.程序正常运行,但不输出任何结果。 D.因为没有catch语句块,所以不能通过编
4:对于已经被定义过可能抛出异常的语句,在编程时( A)。
A.必须使用try/catch语句处理异常,或用throw将其抛出。
B.如果程序错误,必须使用 try/catch语句处理异常。
C.可以置之不理。
D.只能使用try/catch语句处理。
5:哪个关键字可以抛出异常?( B )
A.transient B.throw C.finally D.catch
6:请问所有的异常类皆继承哪一个类?( A)
A.java.lang.Throwable B.java.lang.Exception
C.java.lang.Error D.java.io.Exception
7.System类在哪个包中?( B )
A.java.awt B.java.lang C.java.util D.java.io
A:简答题
2、请说明Map接口和Collection接口的区别
Map为双链集合,collection为单链集合
3、请写出Map集合的遍历方式
第一种:通过Map.keySet遍历key和value
第二种:通过Map.entrySet使用iterator遍历key和value
第三种:通过Map.entrySet遍历key和value
第四种:通过Map.values()遍历所有的value
4、请说明HashMap和Hashtable的区别
HashMap非同步的 效率高 数据不安全
Hashtable同步的 效率第 数据安全
5、请解释Collection与Collections的区别
Collection为单链集合
Collections为对单链集合操作的工具类
B:看程序写结果(写出自己的分析理由),程序填空,改错,看程序写结果。
class Car {
private String brand;//品牌
private int year; //制造年份
public Car () {}
public Car (String brand, int year) {
this.brand = brand;
this.year = year;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getBrand(){
return brand;
}
public void setYear(int year) {
this.year = year;
}
public int getYear(){
return year;
}
}
1、给出以下代码,请问该程序的运行结果是什么?如有问题,请说明原因。
class Test {
public static void main(String[] args) {
HashMap
Kevin
原因:添加kevin时候,替代了之前的Jim,因为他们的键相同
2、给出以下代码,已定义好Car类,请问该程序的运行结果是什么?如有问题,请说明原因。
class Test {
public static void main(String[] args) {
HashMap
Jim
Kevin
3、给出以下代码,请问该程序的运行结果是什么?如有问题,请说明原因。
class Test {
public static void main(String[] args) {
TreeMap
11--Tom
23--Kevin
45--David
原因:Treemap集合会对键进行自然排序,由于jim和kevin的健相同所以kevin替代了jim
4、给出以下代码,已定义好Car类,请问该程序的运行结果是什么?如有问题,请说明原因。
class Test {
public static void main(String[] args) {
TreeMap
奥迪A4L–2014–Kevin
奥迪Q7–2014–Jim
宝马X5–2014—David
实现了comparator接口,所以排序按照先年月,后品牌
C:编程题
1、请编写程序,统计一个字符串中每个字符出现的次数
public class StringNumber {
public static void main(String[] args) {
HashMap<Character,Integer > hm = new HashMap<Character,Integer>();
Scanner sc = new Scanner(System.in);
System.out.println("请随意输入一串字符:");
String next = sc.next();
char[] ch = next.toCharArray();
for(char s:ch) {
Integer count = hm.put(s, 1);
if(count==null)
count=1;
count++;
hm.put(s, count);
}
Set<Entry<Character,Integer>> entrySet = hm.entrySet();
for(Entry<Character,Integer> s1:entrySet) {
System.out.println(s1.getKey()+"---"+s1.getValue());
}
}
}
2、请编写程序,存储自定义对象到HashMap集合中,并采用两种方式遍历
public class HashMapDemo {
public static void main(String[] args) {
HashMap<Student, Integer> hm = new HashMap<Student, Integer>();
hm.put(new Student(20, "紫霞"), 1);
hm.put(new Student(21, "朱茵"), 2);
hm.put(new Student(21, "朱茵1"), 3);
hm.put(new Student(21, "至尊宝"), 4);
Set<Student> keySet = hm.keySet();
for (Student s1 : keySet) {
System.out.println(s1 + "-----" + hm.get(s1));
}
Set<Entry<Student, Integer>> es = hm.entrySet();
for (Entry<Student, Integer> s : es) {
System.out.println(s.getKey() + "===" + s.getValue());
}
}
}
3、请编写程序,存储自定义对象到TreeMap集合中,并采用两种方式遍历
public class TreeMapDemo{
public static void main(String[] args) {
TreeMap<String, String> tm = new TreeMap<String, String>();
// 给键值对集合存入元素
tm.put("至尊宝", "紫霞");
tm.put("刘备", "孙尚香");
tm.put("项羽", "虞姬");
tm.put("赵云", "貂蝉");
// 遍历,通过建找值
Set<String> keys = tm.keySet();
for (String key : keys) {
System.out.println(key + " " + tm.get(key));
}
System.out.println("-------");
Set<Entry<String,String>> entrySet = tm.entrySet();
for (Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
}
4、请编写程序,完成集合嵌套,并遍历
jc 基础班
张三 20
李四 22
jy 就业班
王五 21
赵六 23
HashMap嵌套HashMap
HashMap嵌套ArrayList
public class Demo {
public static void main(String[] args) {
HashMap<String,HashMap<String, Integer>> bighm = new HashMap<String,HashMap<String, Integer>>();
HashMap<String, Integer> hm = new HashMap<String,Integer>();
HashMap<String, Integer> hm2 = new HashMap<String,Integer>();
hm.put("张三", 20);
hm.put("李四", 22);
bighm.put("基础班",hm);
hm2.put("王五", 23);
hm2.put("赵六", 24);
bighm.put("就业班", hm2);
Set<Entry<String, HashMap<String, Integer>>> bigen = bighm.entrySet();
for(Entry<String, HashMap<String, Integer>> s:bigen) {
System.out.println(s.getKey());
Set<Entry<String,Integer>> entrySet = s.getValue().entrySet();
for(Entry<String,Integer> s1:entrySet) {
System.out.println("\t"+s1.getKey()+"=="+s1.getValue());
}
}
}
}
5、请编写程序,完成模拟斗地主案例
public class PuKei{
public static void main(String[] args) {
//1.先创建两个String数组存储牌的点数和花色
//点数
String []numbers={"3","4","5","6","7","8","9","10","J","O","k","A","2"};
String []colors={ "♥", "♠", "♣", "♦" };
//2.因为要洗牌所以用ArrayList集合存储牌的序号
ArrayList<Integer> lists = new ArrayList<Integer>();
//3.创建hashmap来存储牌的序号和牌的内容
HashMap<Integer, String> hs = new HashMap<Integer, String>();
//4.遍历上面的两个数组,将序号和牌加入hs中,并将序号加入lists中
int index=0;
for (String number : numbers) {
for (String color : colors) {
hs.put(index, color+number);
lists.add(index);
index++;
}
}
//5.将大王和小王加入hs中,序号加入lists中
lists.add(index);
hs.put(index, "大王");
index++;//序号加1
lists.add(index);
hs.put(index, "小王");
//6.洗牌,洗牌的时候洗索引
Collections.shuffle(lists);
//7.发牌。创建TreeSet集合来存储各自的牌的序号,使得牌有序
TreeSet<Integer> zhangsan = new TreeSet<Integer>();
TreeSet<Integer> lisi = new TreeSet<Integer>();
TreeSet<Integer> wangwu = new TreeSet<Integer>();
TreeSet<Integer> dipai = new TreeSet<Integer>();
//用for循环发牌
for (int i = 0; i < lists.size(); i++) {
if(i>=lists.size()-3){
dipai.add(lists.get(i));
}else if(i%3==0){
zhangsan.add(lists.get(i));
}else if(i%3==1){
lisi.add(lists.get(i));
}else if(i%3==2){
wangwu.add(lists.get(i));
}
}
lookpoker("张三", zhangsan, hs);
lookpoker("李四", lisi, hs);
lookpoker("王五", wangwu, hs);
lookpoker("底牌", dipai, hs);
}
//8.写一个看牌的方法
public static void lookpoker(String name,TreeSet<Integer> ts,HashMap<Integer, String> hs){
System.out.print(name+"的牌是: ");
for (Integer it : ts) {
System.out.print(hs.get(it)+" ");
}
System.out.println();
}
}