测试题(星号版)自己完成的答案(1-12)

来源:互联网 发布:php滚动加载 编辑:程序博客网 时间:2024/06/05 16:57

/*★★★★★
====第一题====================================
简述ArrayList、Vector、LinkedList之间的区别?
Arraylist和Vector都是数组结构,vector线程同步安全。ArrayList线程不同步,所以ArrayList效率高
当数组存满时,ArrayList是增加数组长度的50%,Vector是增加数组长度的100%
LinkedList是链表结构。增删快,ArrayList查询改快

Collection和Map之间的区别?
Collection 。 一组对立的元素,通常这些元素都服从某种规则。List必须保持元素特定的顺序,而Set 不能有重复元素。
Map  一组 成对的“键值对”对象。

HashSet和TreeSet之间的区别?*/
HashSet是哈希表结构的。线程不安全 
HashSet是如何保证元素唯一性
是通过元素的两个方法,hashCode和equals来完成。
如果元素的HashCode值相同,才会判断equals是否为true。
class 
{
 public static void main(String[] args)
 {
  HashSet users=new HashSet();
  users.add(new User("张三",28));
  users.add(new User("李四",25));
  users.add(new User("王五",20));
  System.out.println(users.size());

  users.remove(new User("张三",28));
  System.out.println(users.size());
 }
}
/*在删除元素前打印出的集合中的元素个数是3,删除后再次打印的集合中的
元素个数还是3,这个结果是错误的。这是因为User类虽然覆盖了equals()
方法,但是没有覆盖hashCode()的方法,当两个User实例对象的equals方法
比较的结果相等时,而这两个对象的哈希码却不同,所以导致他们存储进
HashSet集合中出现了问题,为了修正这个问题,应该保证两个User实例对象
的equals方法比较结果相等时,他们的哈希码也相等。

通常,用一个类的两个实例对象用equals()方法比较结果相等时,那他们的
哈希码也必须相等,反之不成立。哈希码相等时,他们的不一定equals。

当一个对象被存储进HashSet集合中以后,就不能修改这个对象中的那些参与
计算的哈希值的字段了,否则,对象修改后的哈希值与最初存储进hashSet集合
时的哈希值就不同了,在这种情况下,即使在contains方法使用该对象的当前
应用作为的参数去HashSet集合中检索对象,也将返回找不到的对象的结果,这
也会导致无法从hashSet集合中单独删除当前对象,从而造成内存泄漏。*/


TreeSet是二叉树结构的
保证元素的唯一性的依据:compareTo()方法return 0.
TreeSet排序的第一种方式:让元素自身具备比较性。元素需要实现Comparable接口,覆盖compareTo()方法。
第二种方式:自定义一个比较器(写一个类实现Comparator接口,覆盖compare方法。)做为TreeSet构造函数的参数


/*Collection和Collections的区别?
Collection是集合类的上级接口,继承与他的接口主要有Set 和List.
Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作。


Hashtable和HashMap之间的区别?
Hashtable继承自Dictionary类,而HashMap是Java1.2引进的Mapinterface的一个实现
HashMap允许将null作为一个entry的key或者value,而Hashtable不允许.
最大的不同是,Hashtable的方法是Synchronize的,而HashMap不是,在
多个线程访问Hashtable时,不需要自己为它的方法实现同步,而HashMap就必须为之提供外同步。


然后编程向HashSet中保存自定义的类(Student),并取出打印。
这个类要重写hashCode和equals方法
*/

/*★
====第二题====================================
自定义字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,
让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,
能够在读取的每行前面都加上有行号和冒号。
*/

 

/*★★★
====第三题====================================
拷贝一个带内容的文件夹。
*/
//拷贝单个文件
public static void Test2(String oldpath,String newpath) throws Exception
{
 FileInputStream fis=new FileInputStream(oldpath);
 FileOutputStream fos=new FileOutputStream(newpath);
 
 byte bytes[]=new byte[1024];
 int len=0;
 while((len=fis.read(bytes))!=-1)
 {
  fos.write(bytes, 0, len);
 }
 fos.close();
 fis.close();
}
//拷贝文件夹
public static void Test3(String oldpath,String newpath) throws Exception
{
 File oldfile=new File(oldpath);
 File newfile=new File(newpath);
 if(!newfile.isDirectory())
  newfile.mkdirs();
 File files[]=oldfile.listFiles();
 for(File file:files)
 {
  if(file.isFile())
  {
   Test2(file.getAbsolutePath(),newpath+"\\"+file.getName());
  }else{
   Test3(file.getAbsolutePath(),newpath+"\\"+file.getName());
  }
 }
}
调用
String oldpath="d:\\ASP后台网站";
String newpath="f:\\2\\1";
Test3(oldpath, newpath);


/*★★★★
====第四题====================================
编写一个程序,当用户输入一个目录时,
该程序能列出该目录下的所有子目录和文件。将目录和文件的绝对路径输出打印
*/
public static void Test(File dir) throws Exception
{
 
 File files[]=dir.listFiles();
 for(File file:files)
 {
  if(file.isFile())
   System.out.println(file.getAbsolutePath());
  else
   Test(file);
 }
}


/*★★★★★
====第五题====================================
有3个学生,每个学生有3门课的成绩,定义一种比较直观的文本文件格式,
输入学生姓名和成绩,从键盘输入以上数据(包括姓名,三门课成绩),
按总分数从高到低的顺序将学生信息存放在磁盘文件"stu.txt"中。
*/
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.TreeSet;

public class Di5 {
 public static void main(String[] args) {
  comStudents();
 }
 
 public static void comStudents()
 {
  BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));
  FileOutputStream fos=null;
  TreeSet<Students> set=new TreeSet<Students>(Collections.reverseOrder());
  //Collections.reverseOrder()是强行逆转实比较类的顺序
  String str=null;
  try {
   while((str=bfr.readLine())!=null)
   {
    if("over".equals(str))
     break;
    String arr[]=str.split("( )+");
    
    Students stu=new Students(arr[0], Integer.parseInt(arr[1]), Integer.parseInt(arr[2]), Integer.parseInt(arr[3]));
    set.add(stu);
   }

   fos=new FileOutputStream("c:\\stu.txt");
   for(Students stu:set)
   {
    String info=stu.getName()+" "+stu.getMath()+" "+stu.getChin()+" "+stu.getEngl()+" "+stu.getCount();
    fos.write((info+"\r\n").getBytes());
   }
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   try {
    fos.close();
    bfr.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}

class Students implements Comparable<Students>
{
 private String name;
 private int count;
 private int math;
 private int chin;
 private int engl;
 
 public String getName() {
  return name;
 }
 public int getCount() {
  return count;
 }
 public int getMath() {
  return math;
 }
 public int getChin() {
  return chin;
 }
 public int getEngl() {
  return engl;
 }
 Students(String name,int math,int chin,int engl)
 {
  this.name=name;
  this.math=math;
  this.chin=chin;
  this.engl=engl;
  this.count=math+chin+engl;
 }
 @Override
 public int compareTo(Students o) {
  
  if(this.count>o.count)
   return 1;
  if(this.count<o.count)
   return -1;
  return this.name.compareTo(o.name);
 }
 
}

 

/*★★★★★
====第六题====================================
取出D:盘下全部的.java文件的文件路径保存在java.txt文件中
*/
public static void Test5(File f,FileOutputStream fos) throws Exception
{
 File[] files=f.listFiles();
 for(File file:files)
 {
  if(file.isFile())
  {
   if(file.getName().endsWith(".java"))
   {
    fos.write((file.getAbsolutePath()+"\r\n").getBytes());
   }
  }else{
   Test5(file,fos);
  }
  
 }
}

 


/*★★★
====第七题====================================
计算字符串中子串出现的位置,
例:子串"kk"在字符串abkkcdkkabkkefkk中出现的次数
*/

public static void Test6()
{
 String str="abkkcdkkabkkefkkk";
 String regex="(k)\\1";
 int a=0;
 Pattern p=Pattern.compile(regex);
 Matcher m=p.matcher(str);
 while(m.find())
 {
  a++;
  System.out.println("出现的起始位置(包括头不包括尾):"+m.start()+"........"+m.end());
 }
 System.out.println(a);
}


/*★★★★
====第八题====================================
取出一个文本文件中所有的email地址,并存放到集合中。
*/
public static List<String> Test7(String filename) throws Exception
{
 List<String> maillist=new ArrayList<String>();
 FileInputStream fis=new FileInputStream(filename);
 BufferedReader bfr=new BufferedReader(new InputStreamReader(fis));
 String len=null;
 while((len=bfr.readLine())!=null)
 {
  String regex="[\\w]+@[\\w]+(\\.+[\\w])+";
  Pattern p=Pattern.compile(regex);
  Matcher m=p.matcher(len);
  while(m.find())
  {
   maillist.add(m.group());
   System.out.println(m.group());
  }
 }
 return maillist;
}

/*★★
====第九题====================================
程序启动之后生成一个1-100之间的随机数
通过键盘录入的方式,进行猜数字的游戏。
输入一个1~100之间的数字,程序与预先生成的数字比较, 如果不同,要给出大或者小的提示, 重新输入.
如果相同也给予提示, 程序结束.
*/
public static void Test8() throws Exception
{
 BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));
 while(true)
 {
  int a=(int)(Math.random()*10+1);
  int b=Integer.parseInt(bfr.readLine())-a;
  if(b>0)
  {
   System.out.println("你输入的数子比系统产生的大");
  }else if(b<0)
  {
   System.out.println("你输入的数子比系统产生的小");
  }else{
   System.out.println("你输入的数子和系统产生的一样大,退出");
   break;
  }
 }
}

 

/*★
====第十题====================================
假如我们在开发一个系统时需要对员工进行建模,员工包含 3 个属性:
姓名、工号以及工资。经理也是员工,除了含有员工的属性外,另为还有一个
奖金属性。请使用继承的思想设计出员工类和经理类。要求类中提供必要的方
法进行属性访问。
*/

class employe{
 private String name;
 private String number;
 private double wages;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getNumber() {
  return number;
 }
 public void setNumber(String number) {
  this.number = number;
 }
 public double getWages() {
  return wages;
 }
 public void setWages(double wages) {
  this.wages = wages;
 }
}
class yuangong extends employe
{
 public void work()
 {
  System.out.println("员工工作了");
 }
 public void ispay()
 {
  System.out.println("时间到了,给我发工资");
 }
}
class jingli extends employe
{
 private double jiangjin;

 public double getJiangjin() {
  return jiangjin;
 }

 public void setJiangjin(double jiangjin) {
  this.jiangjin = jiangjin;
 }
 public void pay()
 {
  System.out.println("给员工发工资了");
 }
}

 

/*★★★
====第十一题==================================
字符串由多个数字和空格组成“20 8 0 -1 32 14”,
将字符串中的数字按数值大小升序排列,获取排序后的字符串。
*/
public static void Test9()
{
 String str="20 8 0 -1 32 14";
 String regex="( )+";
 String arr[]=str.split(regex);
 TreeSet<Integer> set=new TreeSet<Integer>();
 for(String s:arr)
 {
  set.add(Integer.parseInt(s));
 }
 for(Integer i:set)
 {
  System.out.print(i+" ");
 }
}
/*★★★★
====第十二题==================================
写一个程序,允许用户依次输入多个姓名和住址,
并能将用户的输入保存到文件中。
用户输入 ”quit” 表示输入完毕,程序退出。
*/
public static void Test10() throws Exception
{
 FileOutputStream fos=new FileOutputStream("c:\\user.txt");
 BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));
 String len=null;
 while((len=bfr.readLine())!=null)
 {
  if("quit".equals(len))
  {
   break;
  }else{
   String []userinfo=len.split(" ");
   fos.write((userinfo[0]+" ").getBytes());
   fos.write((userinfo[1]+"\r\n").getBytes());
  }
 }
 bfr.close();
 fos.close();
}