Java集合框架22课后编程题

来源:互联网 发布:商业系统域名 编辑:程序博客网 时间:2024/06/03 11:11
编程练习题
22.1 创建俩个散列规则集,求它们的并集,差集和交集。
22.2 从文本读取单词,将所有不重复的单词按升序显示,文本文件作为命令参数传递。
22.3 读取一个java源文件,报告关键字的个数,命令行传递文件名。
22.4 创建俩个数组线性表,求它们的并集,差集和交集。
22.5 从文本文件读取单词,按照字母的升序显示所有的单词,可以重复,文本文件作为参数传递。
22.6 读取个数不定的整数,输入0结束,打印频率最高的,如果有俩个一样的最高频率则都输出。
22.7 将一个java文件转为html文件,关键字,注释和直接量分别用粗体的深蓝色,绿色和蓝色表示。
22.8 对面板上的点进行排序 
        * 定义一个为Point的类,它的俩个数据域为x和y,分别表示坐标。如果x坐标一样,实现comparable接口对在x坐标和y坐标上的点进行比较。
        *定义一个CompareY的类实现Comparator<Point>。如果y坐标一样,实现compare方法对在它们的x坐标和它们的y坐标上的俩个点进行比较。
        * 随机创建100个点,然后使用Arrays.sort方法分别以它们x坐标的升序和y坐标的升序显示这些点。

22.9 九宫格的解决方案,存在线型表中,为一个9×9的网格。

public class Test_22_1 { //创建俩个散列规则集,求它们的并集,差集和交集。 public static void main(String[] args) {  HashSet<String> hs=new HashSet<String>();  hs.add("George");  hs.add("Jim");  hs.add("Blake");  hs.add("Kevin");  hs.add("Mecheal");  hs.add("John");   HashSet<String> hs2=new HashSet<String>();  hs2.add("George");  hs2.add("Kate");  hs2.add("Kevin");  hs2.add("Mecheal");  hs2.add("Ryan");   //hs.addAll(hs2);  并集  //hs.removeAll(hs2); //差集  //hs.retainAll(hs2); 交集  System.out.println(hs); }}


public class Test_22_2 { private static String source; static {  source = "You Know I still Love You Baby And it will never change I want nobody nobody But You I want nobody nobody But You"; } /** 从文本读取单词,将所有不重复的单词按升(降)序显示,文本文件作为命令参数传递。 */ public static void main(String[] args) {  String[] ints = source.split("[ \n\t\r.,;:!?(){]");  // 如果是升序输出,那么不需要自定义比较器了  TreeMap<String, Integer> map = new TreeMap<String, Integer>(    new ABCComparator());  for (int i = 0; i < ints.length; i++) {   String key = ints[i].toLowerCase();   if (key != null) {    if (map.get(key) == null) {     map.put(key, 1);    } else {     int value = map.get(key);     map.put(key, value + 1);    }   }  }  System.out.println(map); }}class ABCComparator implements Comparator<String> { // 需要逆序 ,不是升序 public int compare(String o1, String o2) {  return o2.compareTo(o1); }}

public class Test_22_3 { private static String source; private static HashMap<String, String> map = new HashMap<String, String>(); static {  source = "abstract,assert,boolean,break,byte,case,catch,char,class,const,continue,"    + "default,do,double,else,enum,extends,final,finally,float,for,goto,if,"    + "implements,import,instanceof,int,interface,long,native,new,package,"    + "private,protected,public,return,strictfp,short,static,super,switch,"    + "synchronized,this,throw,throws,transient,try,void,volatile,while";  String[] result = source.split(",");  for (int i = 0; i < result.length; i++) {   String key = result[i];   map.put(key, key);  } } public static int getKeyWords(String path) throws Exception {  StringBuffer sb = new StringBuffer();  int result = 0;  InputStream is = new FileInputStream(new File(path));  byte[] bs = new byte[1024];  int len;  while ((len = is.read(bs, 0, bs.length)) != -1) {   sb.append(new String(bs, 0, bs.length));  }  is.close();  String[] data = sb.toString().split("[^a-zA-Z]");  for (int i = 0; i < data.length; i++) {   if (data[i] != null && !data[i].equals("")) {    System.out.println(data[i]);    if (map.containsKey(data[i])) {     result++;    }   }  }  return result; } // 读取一个java源文件,报告关键字的个数,命令行传递文件名。 /**  * 思路:使用图,将文本文件转为字符串数组,然后遍历,将关键字放进一个HashMap中 因为基于hash表,所以查找速度很快  * 因为如果对问题设计了足够好的hash算法,保证碰撞率很低,hash_map的查找效率无可置疑。   * 在不碰撞的情况下,hash_map是所有数据结构中查找最快的,它是常数级的。  */ public static void main(String[] args) {  String path = "L:\\DBhelper.java";  int re;  try {   re = getKeyWords(path);   System.out.println(re);  } catch (Exception e) {   e.printStackTrace();  } }}



public class Test_22_4 { //创建俩个数组线性表,求它们的并集,差集和交集。 public static void main(String[] args) {  String[] s1=new String[]{"George","Jim","Blake","Kevin","Mecheal","John"};  String[] s2=new String[]{"George","Kate","Kevin","Mecheal","Ryan"};  List<String> list1=Arrays.asList(s1);  List<String> list2=Arrays.asList(s2);  Set<String> set1=new HashSet<String>(list1);  Set<String> set2=new HashSet<String>(list2); //set1.addAll(set2);    //并集 //set1.removeAll(set2); //差集  set1.retainAll(set2); //交集  list1=Arrays.asList(set1.toString());   System.out.println(set1); }}


public class Test_22_5 {     //从文本文件读取单词,按照字母的升序显示所有的单词,可以重复,文本文件作为参数传递。 /**  * 思路:读取文本转化为字符串数组,然后添加进入List集合中,然后排序  */ public static void main(String[] args) {  String path="L:\\DBhelper.java";  StringBuffer sb = new StringBuffer();  InputStream is;  List<String> list=new ArrayList<String>();  try {   is = new FileInputStream(new File(path));   byte[] bs = new byte[1024];   int len;   while ((len = is.read(bs, 0, bs.length)) != -1) {    sb.append(new String(bs, 0, bs.length));   }   is.close();   String[] data = sb.toString().split("[^a-zA-Z]");   for (int i = 0; i < data.length; i++) {    if (data[i] != null && !data[i].equals("")) {     list.add(data[i]);    }   }   System.out.println(list);   Collections.sort(list);    System.out.println(list);  } catch (Exception e) {   e.printStackTrace();  } }}



public class Test_22_6 { //需求:读取个数不定的整数,输入0结束,打印频率最高的,如果有多个一样的最高频率则都输出。 /*  * 思路:线性表适合插入和删除,不适合查找,这个程序需要找出频率最高的,还是使用HashMap比较好  */ public static void main(String[] args) {  Scanner sc=new Scanner(System.in);  HashMap<String,Integer> map=new HashMap<String, Integer>();  String number=sc.nextLine();  while(!number.equals("0")){    if(map.get(number)==null){     map.put(number, 1);    }else{     int value=map.get(number);     map.put(number, value+1);    }   number=sc.nextLine();  }  System.out.println(map);  int max=0;  String mark=new String("");  for(Map.Entry<String, Integer> m:map.entrySet()){   if(m.getValue()>max){    max=m.getValue();    mark="";    mark+=m.getKey();   }else if(m.getValue()==max){    mark+=","+m.getKey();   }  }  System.out.println(mark); }}



Test_22_7还在思考


public class Test_22_8 { public static void main(String[] args) {  Random random=new Random();  List<Point> list=new ArrayList<Point>();  for (int i = 0; i < 100; i++) {   int x=random.nextInt(1000)+1;   int y=random.nextInt(1000)+1;   list.add(new Point(x,y));  }  System.out.println("before sort:"+list.toString());  Collections.sort(list);   //我的错误就是在这里没有运行俩次,这是是针对y排序  Collections.sort(list,new CompareY());  //直接这样加是不会运行compareTo()方法的。这是针对x排序  System.out.println("after sort:"+list.toString()); }}class CompareY implements Comparator<Point> { public int compare(Point o1, Point o2) {  System.out.println("compareY");  if (o1.x > o2.x)   return 1;  if (o1.x < o2.x)   return -1;  return 0; }}class Point implements Comparable<Point> { int x; int y; public Point(int x,int y){  this.x=x;  this.y=y; }  @Override public String toString() {  return "[x="+x+",y="+y+"]"; } public int compareTo(Point other) {  System.out.println("compareTo");  if (other.y > this.y)   return -1;  if (other.y < this.y)   return 1;  return 0; }}

Test_22_9网上找的资料,下一篇再研究


2015年6月5日19:46:01    

我是菜鸟,我在路上。  

0 0
原创粉丝点击