[黑马程序员](第50天)一些平时刷的题(3)

------- android培训java培训、期待与您交流! ----------
[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Collections;  
  5. import java.util.Random;  
  6.   
  7. /** 
  8.  * 编写程序,生成5个1至10之间的随机整数,存入一个List集合,编写方法对List集合进行排序(自定义排序算法,禁用Collections.sort方法和TreeSet),然后遍历集合输出。 
  9.  *  
  10.  * 
  11.  */  
  12. public class Test16   
  13. {  
  14.     public static void main(String[] args)  
  15.     {  
  16.         ArrayList<Integer> al = new ArrayList<Integer>();  
  17.         Random r= new Random();  
  18.           
  19.         while (al.size()<5)  
  20.         {  
  21.             al.add(r.nextInt(10)+1);  
  22.         }  
  23.           
  24.         //排序  
  25.         for (int x=0;x<al.size()-1;x++)  
  26.         {  
  27.             for (int y=0;y<al.size()-x-1;y++)  
  28.             {  
  29.                 if (al.get(y)>al.get(y+1))  
  30.                     Collections.swap(al, y, y+1);  
  31.             }  
  32.         }  
  33.           
  34.         System.out.println(al);  
  35.     }  
  36. }  


 

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.util.Arrays;  
  4.   
  5. /** 
  6.  * 把以下IP存入一个txt文件,编写程序把这些IP按数值大小,从小到达排序并打印出来。 
  7.  * 61.54.231.245 
  8.  * 61.54.231.9 
  9.  * 61.54.231.246 
  10.  * 61.54.231.48 
  11.  * 61.53.231.249 
  12.  *  
  13.  * 
  14.  */  
  15. public class Test17  
  16. {  
  17.     public static void main(String[] args)  
  18.     {  
  19.         String[] ip = {"61.54.231.245","61.54.231.9","61.54.231.246","61.54.231.48","61.53.231.249"};  
  20.         Arrays.sort(ip);  
  21.           
  22.         for (int i=0;i<ip.length;i++)  
  23.         {  
  24.             System.out.println(ip[i]);  
  25.         }  
  26.     }  
  27. }  

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. /** 
  4.  *  
  5.  * 写一方法,打印等长的二维数组,要求从1开始的自然数由方阵的最外圈向内螺旋方式地顺序排列。 如: n = 4 则打印: 
  6.  * 1    2   3   4 
  7.  * 12   13  14  5 
  8.  * 11   16  15  6 
  9.  * 10   9   8   7 
  10.  * 
  11.  */  
  12. public class Test18   
  13. {  
  14.     public static void main(String[] args)  
  15.     {  
  16.         printMath(60);  
  17.     }  
  18.       
  19.     public static void printMath(int n)  
  20.     {  
  21.         int[][] arr = new int[n][n];  
  22.         int x = 0;  
  23.         int y = 0;  
  24.         int max = arr.length-1;  
  25.         int min = 0;  
  26.         int num = 1;  
  27.           
  28.         while (min<=max)  
  29.         {  
  30.             //right  
  31.             while (y<max)  
  32.             {  
  33.                 arr[x][y++] = num++;  
  34.             }  
  35.             //down  
  36.             while (x<max)  
  37.             {  
  38.                 arr[x++][y] = num++;  
  39.             }  
  40.             //left  
  41.             while (y>min)  
  42.             {  
  43.                 arr[x][y--] = num++;  
  44.             }  
  45.             //up  
  46.             while (x>min)  
  47.             {  
  48.                 arr[x--][y] = num++;  
  49.             }  
  50.             //如果是奇数,可能赋值不上  
  51.             if (min == max)  
  52.             {  
  53.                 arr[x][y] = num;  
  54.             }  
  55.               
  56.             max--;  
  57.             min++;  
  58.               
  59.             x++;  
  60.             y++;  
  61.         }  
  62.           
  63.         for (int i=0;i<arr.length;i++)  
  64.         {  
  65.             for (int j=0;j<arr.length;j++)  
  66.             {  
  67.                 System.out.print(arr[i][j]+"\t");  
  68.             }  
  69.             System.out.println();  
  70.         }  
  71.     }  
  72. }  


 

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. /** 
  4.  *  28人买可乐喝,3个可乐瓶盖可以换一瓶可乐,那么要买多少瓶可乐,够28人喝?假如是50人,又需要买多少瓶可乐?(需写出分析思路) 
  5.  *   
  6.  *      三个瓶盖就是一个可乐和一个瓶盖 
  7.  * 
  8.  */  
  9. public class Test19   
  10. {  
  11.     public static void main(String[] args)  
  12.     {  
  13.         System.out.println(buyCoke(28));  
  14.         System.out.println(buyCoke(50));  
  15.     }  
  16.       
  17.     public static int buyCoke(int num)  
  18.     {  
  19.         int coke = 0;  
  20.         int cap = 0;  
  21.         int buy = 0;  
  22.           
  23.         while (coke < num)  
  24.         {  
  25.             buy++;  
  26.             coke++;  
  27.             cap++;  
  28.               
  29.             if (cap == 3)  
  30.             {  
  31.                 coke++;  
  32.                 cap = 1;  
  33.             }  
  34.         }  
  35.         return buy;  
  36.     }  
  37. }  


 

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.io.FileReader;  
  4. import java.io.IOException;  
  5. import java.io.Reader;  
  6.   
  7. /** 
  8.  * 自定义字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,能够在读取的每行前面都加上有行号和冒号。 
  9.  * 
  10.  */  
  11. public class Test20   
  12. {  
  13.     public static void main(String[] args)  
  14.     {  
  15.         MyLineNumberReader mlnr = null;  
  16.           
  17.         try  
  18.         {  
  19.             mlnr = new MyLineNumberReader(new FileReader("F:\\java\\day09\\Demo.java"));  
  20.               
  21.             String len = null;  
  22.               
  23.             while ((len = mlnr.myReadLine()) != null)  
  24.             {  
  25.                 System.out.println(mlnr.getCount()+":\t"+len);  
  26.             }  
  27.         }  
  28.         catch (IOException e)  
  29.         {  
  30.             System.out.println("读取出错!");  
  31.         }  
  32.         finally  
  33.         {  
  34.             try  
  35.             {  
  36.                 if (mlnr != null)  
  37.                     mlnr.myclose();  
  38.             }  
  39.             catch (IOException e)  
  40.             {  
  41.                 System.out.println("关闭出错");  
  42.             }  
  43.         }  
  44.     }  
  45. }  
  46.   
  47. class MyLineNumberReader  
  48. {  
  49.     private Reader r;  
  50.     private int count;  
  51.   
  52.     MyLineNumberReader(Reader r)  
  53.     {  
  54.         this.r = r;  
  55.     }  
  56.       
  57.     public String myReadLine() throws IOException  
  58.     {  
  59.         StringBuffer sb = new StringBuffer();  
  60.           
  61.         int num = 0;  
  62.         count++;  
  63.           
  64.         while ((num = r.read()) != -1)  
  65.         {  
  66.             if (num == '\r')  
  67.                 continue;  
  68.             if (num == '\n')  
  69.                 return sb.toString();  
  70.             else  
  71.                 sb.append((char)num);  
  72.         }  
  73.         if (sb.length() != 0)  
  74.             return sb.toString();  
  75.         return null;  
  76.     }  
  77.       
  78.     public void myclose() throws IOException  
  79.     {  
  80.         r.close();  
  81.     }  
  82.       
  83.     public int getCount() {  
  84.         return count;  
  85.     }  
  86.   
  87.     public void setCount(int count) {  
  88.         this.count = count;  
  89.     }  
  90. }  


 

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. /** 
  4.  * 使用TCP协议写一个可以上传文件的服务器和客户端。 
  5.  *  
  6.  * 
  7.  */  
  8. public class Test21   
  9. {  
  10.     public static void main(String[] args)  
  11.     {  
  12.           
  13.     }  
  14. }  


 

[java]  view plain copy print ?
  1. import java.io.*;  
  2. import java.util.*;  
  3.   
  4. /** 
  5.     把一个文件夹下的包括子文件夹里的所有.java文件复制到另一个文件夹下面,并改名.txt 
  6.  
  7.     对一个文件夹进行递归,筛选出.java文件,并存到集合中 
  8.     用IO字符流操作复制文件,并将.java文件改成.txt 
  9. */  
  10.   
  11. class DirPrintListDemo   
  12. {  
  13.     public static void main(String[] args) throws IOException  
  14.     {  
  15.         //源文件夹  
  16.         File dir = new File("F:\\java");  
  17.   
  18.         //目标文件夹  
  19.         File copy = new File("F:\\copy_java");  
  20.         if (!copy.exists())  
  21.             copy.mkdir();  
  22.   
  23.         ArrayList<File> dirlist = new ArrayList<File>();  
  24.         fileToArray(dir,dirlist);  
  25.         copy_ReName(dirlist,copy);  
  26.           
  27.     }  
  28.   
  29.     //函数功能,把一个文件夹中包括子文件夹的所有.java文件存到dirlist集合中  
  30.     public static void fileToArray(File dir,List<File> dirlist)  
  31.     {  
  32.         File[] files = dir.listFiles();  
  33.   
  34.         for (File file : files )  
  35.         {  
  36.             if (file.isDirectory())  
  37.                 fileToArray(file,dirlist);  
  38.             else  
  39.             {  
  40.                 if (file.getName().endsWith(".java"))  
  41.                     dirlist.add(file);  
  42.             }  
  43.         }  
  44.     }  
  45.   
  46.     //函数功能,用IO字符流操作复制文件,并将.java文件改成.txt  
  47.     public static void copy_ReName(List<File> dirlist,File copy) throws IOException  
  48.     {  
  49.         //遍历集合  
  50.         for (File files : dirlist)  
  51.         {  
  52.             BufferedReader br = new BufferedReader(new FileReader(files));  
  53.             String newName = files.getName().replace(".java",".txt");  
  54.             BufferedWriter bw = new BufferedWriter(new FileWriter(new File(copy,newName)));  
  55.   
  56.             String len = null;  
  57.             while ((len = br.readLine()) != null)  
  58.             {  
  59.                 bw.write(len);  
  60.                 bw.newLine();  
  61.                 bw.flush();  
  62.             }  
  63.   
  64.             br.close();  
  65.             bw.close();  
  66.         }  
  67.     }  
  68. }  


 

 

 

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.util.Iterator;  
  4. import java.util.Map;  
  5. import java.util.Set;  
  6. import java.util.TreeMap;  
  7.   
  8. /** 
  9.  * 编写一个类,在main方法中定义一个Map对象(采用泛型),加入若干个对象,然后遍历并打印出各元素的key和value。 
  10.  *  
  11.  *   
  12.  */  
  13. public class Test22   
  14. {  
  15.     public static void main(String[] args)  
  16.     {  
  17.         Map<String,Integer> m = new TreeMap<String,Integer>();  
  18.         m.put("Lilei"10);  
  19.         m.put("hanmeimei"9);  
  20.           
  21.         //第一种  
  22.         Set<Map.Entry<String,Integer>> entrySet = m.entrySet();  
  23.           
  24.         for (Iterator<Map.Entry<String,Integer>> it = entrySet.iterator();it.hasNext(); )  
  25.         {  
  26.             Map.Entry<String,Integer> me = it.next();  
  27.               
  28.             String key = me.getKey();  
  29.             Integer value = me.getValue();  
  30.             System.out.println(key+"-------"+value);  
  31.         }  
  32.           
  33.         //第二种  
  34.         Set<String> keySet = m.keySet();  
  35.           
  36.         for (Iterator<String> it = keySet.iterator();it.hasNext(); )  
  37.         {  
  38.             String key = it.next();  
  39.               
  40.             Integer value = m.get(key);  
  41.             System.out.println(key+"-------"+value);  
  42.         }  
  43.     }  
  44. }  


 

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.FileReader;  
  6. import java.io.FileWriter;  
  7. import java.io.IOException;  
  8. import java.util.Iterator;  
  9. import java.util.Map;  
  10. import java.util.Set;  
  11. import java.util.TreeMap;  
  12.   
  13. /** 
  14.  * 把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出,例如: 
  15.   a: 21 次  
  16.   b: 15 次 
  17.   c:: 15 次 
  18.   把: 7 次 
  19.   当: 9 次 
  20.   前: 3 次 
  21.   ,:30 次 
  22.  *  
  23.  * 
  24.  */  
  25. public class Test23   
  26. {  
  27.     public static void main(String[] args) throws IOException  
  28.     {  
  29.             BufferedReader br = new BufferedReader(new FileReader("面试题.txt"));  
  30.             BufferedWriter bw = new BufferedWriter(new FileWriter("tongji.txt"));  
  31.             TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();  
  32.               
  33.             String len = null;  
  34.             while ((len = br.readLine()) != null)  
  35.             {  
  36.                 char[] chs = len.toCharArray();  
  37.                   
  38.                 for (int i=0;i<chs.length;i++)  
  39.                 {  
  40.                     Integer value = tm.get(chs[i]);  
  41.                     if (value == null)  
  42.                     {  
  43.                         tm.put(chs[i],1);  
  44.                     }  
  45.                     else  
  46.                     {  
  47.                         value += 1;  
  48.                         tm.put(chs[i], value);  
  49.                     }  
  50.                 }  
  51.             }  
  52.       
  53.             Set<Map.Entry<Character,Integer>> entrySet = tm.entrySet();  
  54.             for (Iterator<Map.Entry<Character,Integer>> it = entrySet.iterator();it.hasNext(); )  
  55.             {  
  56.                 Map.Entry<Character, Integer> me = it.next();  
  57.                 Character key = me.getKey();  
  58.                 Integer value = me.getValue();  
  59.                   
  60.                 bw.write(key+": "+value+"次");  
  61.                 bw.newLine();  
  62.                 bw.flush();  
  63.             }  
  64.             br.close();  
  65.             bw.close();  
  66.     }  
  67. }  


 

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. /** 
  4.  * 在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1。 
  5.  * 要搜索的字符数组和字符都以参数形式传递传递给该方法,如果传入的数组为null, 应抛出IllegalArgumentException异常。在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确, 
  6.  *  例如,字符不存在,字符存在,传入的数组为null等。 
  7.  *  
  8.  * 
  9.  */  
  10. public class Test24   
  11. {  
  12.     public static void main(String[] args)  
  13.     {  
  14.         char[] chs = {'z','a','c','t','c'};  
  15.           
  16.         IsTest it = new IsTest();  
  17.           
  18.         System.out.println(it.isTest(chs, 'c'));  
  19.         System.out.println(it.isTest(chs, 'v'));  
  20.         System.out.println(it.isTest(null'c'));  
  21.           
  22.     }  
  23. }  
  24.   
  25. class IsTest  
  26. {  
  27.     public int isTest(char[] chs,char ch)  
  28.     {  
  29.         if (chs == null)  
  30.             throw new IllegalArgumentException("传入字符数组不能为空!");  
  31.           
  32.         for (int i=0;i<chs.length;i++)  
  33.         {  
  34.             if (ch == chs[i])//基本数据类型不能用equals,用==  
  35.                 return i;  
  36.         }  
  37.         return -1;  
  38.     }  
  39. }  


 

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.io.FileReader;  
  4. import java.lang.reflect.Method;  
  5. import java.util.Properties;  
  6.   
  7. /** 
  8.  * 已知一个类,定义如下: 
  9.   package cn.itcast.heima; 
  10.   public class DemoClass { 
  11.     public void run() 
  12.     { 
  13.       System.out.println("welcome to heima!"); 
  14.     }  
  15.   } 
  16.   (1) 写一个Properties格式的配置文件,配置类的完整名称。 
  17.   (2) 写一个程序,读取这个Properties配置文件,获得类的完整名称并加载这个类,用反射 的方式运行run方法。 
  18.  *  
  19.  * 
  20.  */  
  21. public class Test25   
  22. {  
  23.     public static void main(String[] args) throws Exception  
  24.     {  
  25.         Properties prop = new Properties();  
  26.         prop.load(new FileReader("cn\\itcast\\heima\\class.propertise"));  
  27.           
  28.         String className = prop.getProperty("name");  
  29.         Class clazz = Class.forName(className);  
  30.         Method method = clazz.getMethod("run"null);  
  31.         method.invoke(clazz.newInstance());  
  32.     }  
  33. }  


 

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.util.Scanner;  
  4.   
  5. /** 
  6.  * 金额转换,阿拉伯数字转换成中国传统形式。例如:101000001010 转换为壹仟零壹拾亿零壹仟零壹拾圆整 
  7.  *  
  8.  * 扫描一个数转换字符串存进一个数组中 
  9.  * 定义两个字符数组,分别是零壹贰叁肆伍陆柒捌玖,圆拾佰扦萬拾万佰萬仟萬亿拾亿佰亿仟亿萬亿 
  10.  * 
  11.  */  
  12. public class Test26   
  13. {  
  14.     public static void main(String[] args)  
  15.     {  
  16.         Scanner sc = new Scanner(System.in);  
  17.         System.out.print("请输入金额:");  
  18.         String str = sc.nextLine();  
  19.           
  20.         System.out.println(getChinese(str));  
  21.           
  22.           
  23.     }  
  24.       
  25.     public static String getChinese(String str)  
  26.     {  
  27.         String[] str1 = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};  
  28.         String[] str2 = {"整圆","拾","佰","扦","萬","拾万","佰萬","仟萬","亿","拾亿","佰亿","仟亿","萬亿"};  
  29.         StringBuilder sb = new StringBuilder();  
  30.           
  31.         char[] chs = str.toCharArray();  
  32.       
  33.         for (int i=chs.length-1;i>=0;i--)  
  34.         {  
  35.               
  36.         }  
  37.           
  38.         return sb.toString();  
  39.     }  
  40. }  


 

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. /** 
  4.  * 方法中的内部类能不能访问方法中的局部变量,为什么? 
  5.  *  
  6.  *  因为内部类的生存周期比方法中的局部变量长,局部变量再作用域完成后就会被栈内存释放销毁。要想访问局部变量,那么局部变量必须被final修饰。 
  7.  * 
  8.  */  
  9. public class Test27 {  
  10.   
  11. }  


 

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. /** 
  4.  * 有一个类为ClassA,有一个类为ClassB,在ClassB中有一个方法b,此方法抛出异常,在ClassA类中有一个方法a, 
  5.  * 请在这个方法中调用b,然后抛出异常。在客户端有一个类为TestC,有一个方法为c ,请在这个方法中捕捉异常的信息。 
  6.  * 完成这个例子,请说出java中针对异常的处理机制。 
  7.  *  
  8.  * java中的异常处理机制,谁调用谁处理,如果一直抛出最终会抛给虚拟机 
  9.  * 
  10.  */  
  11. public class Test28   
  12. {  
  13.     public static void main(String[] args)  
  14.     {  
  15.         C.c();  
  16.     }  
  17. }  
  18.   
  19. class A  
  20. {  
  21.     public static void a() throws Exception  
  22.     {  
  23.         B.b();  
  24.     }  
  25. }     
  26.   
  27. class B  
  28. {  
  29.     public static void b() throws Exception  
  30.     {  
  31.         throw new Exception("b");  
  32.     }  
  33. }   
  34.   
  35. class C  
  36. {  
  37.     public static void c()  
  38.     {  
  39.         try  
  40.         {  
  41.             A.a();  
  42.         }  
  43.         catch(Exception e)  
  44.         {  
  45.             System.out.println("处理");  
  46.         }  
  47.     }  
  48. }  


 

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.lang.reflect.Field;  
  4.   
  5. /** 
  6.  * 写一个方法,此方法可将obj对象中名为propertyName的属性的值设置为value.  
  7.  *  
  8.  * public void setProperty(Object obj, String propertyName, Object value)  
  9.  *   {     
  10.  *   }  
  11.  *    
  12.  *  考点是反射 
  13.  */  
  14. public class Test29  
  15. {  
  16.     public static void main(String[] args)  
  17.     {  
  18.         Person p = new Person();    
  19.         System.out.println(p.name+"---"+p.age);   
  20.           
  21.         setProperty(p,"name","Hanmeimei");    
  22.         setProperty(p,"age",19);    
  23.         System.out.println(p.name+"---"+p.age);    
  24.     }  
  25.       
  26.     public static void setProperty(Object obj,String propertyName,Object value)   
  27.     {  
  28.         try  
  29.         {  
  30.             Field f = obj.getClass().getDeclaredField(propertyName);  
  31.               
  32.             f.setAccessible(true);  
  33.               
  34.             f.set(obj, value);  
  35.               
  36.             f.setAccessible(false);  
  37.         }  
  38.         catch (Exception e)  
  39.         {  
  40.             System.out.println("修改错误");  
  41.         }  
  42.     }  
  43.       
  44.     public static class Person  
  45.     {  
  46.         public String name = "Lilei";  
  47.         private int age = 18;  
  48.     }  
  49. }  


 

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.util.LinkedList;  
  4.   
  5. /** 
  6.  * 有100个人围成一个圈,从1开始报数,报到14的这个人就要退出。然后其他人重新开始,从1报数,到14退出。问:最后剩下的是100人中的第几个人? 
  7.  * 
  8.  */  
  9. public class Test30   
  10. {  
  11.     public static void main(String[] args)  
  12.     {  
  13.         final int num = 14;  
  14.         int count = -1;  
  15.           
  16.         LinkedList<Integer> ll = new LinkedList<Integer>();  
  17.           
  18.         for (int i=0;i<100;i++)  
  19.         {  
  20.             ll.add(i+1);  
  21.         }  
  22.           
  23.         while (ll.size() != 1)  
  24.         {  
  25.             for (int x=1;x<=num;x++)  
  26.             {  
  27.                 count ++;  
  28.                 if (count>=ll.size())  
  29.                     count = 0;  
  30.             }  
  31.             ll.remove(count);  
  32.             count--;  
  33.         }  
  34.         System.out.print(ll.get(0));  
  35.     }  
  36. }  


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值