-------
android培训、
java培训、期待与您交流! ----------
- package com.gotoheima;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Random;
- /**
- * 编写程序,生成5个1至10之间的随机整数,存入一个List集合,编写方法对List集合进行排序(自定义排序算法,禁用Collections.sort方法和TreeSet),然后遍历集合输出。
- *
- *
- */
- public class Test16
- {
- public static void main(String[] args)
- {
- ArrayList<Integer> al = new ArrayList<Integer>();
- Random r= new Random();
- while (al.size()<5)
- {
- al.add(r.nextInt(10)+1);
- }
- //排序
- for (int x=0;x<al.size()-1;x++)
- {
- for (int y=0;y<al.size()-x-1;y++)
- {
- if (al.get(y)>al.get(y+1))
- Collections.swap(al, y, y+1);
- }
- }
- System.out.println(al);
- }
- }
- package com.gotoheima;
- import java.util.Arrays;
- /**
- * 把以下IP存入一个txt文件,编写程序把这些IP按数值大小,从小到达排序并打印出来。
- * 61.54.231.245
- * 61.54.231.9
- * 61.54.231.246
- * 61.54.231.48
- * 61.53.231.249
- *
- *
- */
- public class Test17
- {
- public static void main(String[] args)
- {
- String[] ip = {"61.54.231.245","61.54.231.9","61.54.231.246","61.54.231.48","61.53.231.249"};
- Arrays.sort(ip);
- for (int i=0;i<ip.length;i++)
- {
- System.out.println(ip[i]);
- }
- }
- }
- package com.gotoheima;
- /**
- *
- * 写一方法,打印等长的二维数组,要求从1开始的自然数由方阵的最外圈向内螺旋方式地顺序排列。 如: n = 4 则打印:
- * 1 2 3 4
- * 12 13 14 5
- * 11 16 15 6
- * 10 9 8 7
- *
- */
- public class Test18
- {
- public static void main(String[] args)
- {
- printMath(60);
- }
- public static void printMath(int n)
- {
- int[][] arr = new int[n][n];
- int x = 0;
- int y = 0;
- int max = arr.length-1;
- int min = 0;
- int num = 1;
- while (min<=max)
- {
- //right
- while (y<max)
- {
- arr[x][y++] = num++;
- }
- //down
- while (x<max)
- {
- arr[x++][y] = num++;
- }
- //left
- while (y>min)
- {
- arr[x][y--] = num++;
- }
- //up
- while (x>min)
- {
- arr[x--][y] = num++;
- }
- //如果是奇数,可能赋值不上
- if (min == max)
- {
- arr[x][y] = num;
- }
- max--;
- min++;
- x++;
- y++;
- }
- for (int i=0;i<arr.length;i++)
- {
- for (int j=0;j<arr.length;j++)
- {
- System.out.print(arr[i][j]+"\t");
- }
- System.out.println();
- }
- }
- }
- package com.gotoheima;
- /**
- * 28人买可乐喝,3个可乐瓶盖可以换一瓶可乐,那么要买多少瓶可乐,够28人喝?假如是50人,又需要买多少瓶可乐?(需写出分析思路)
- *
- * 三个瓶盖就是一个可乐和一个瓶盖
- *
- */
- public class Test19
- {
- public static void main(String[] args)
- {
- System.out.println(buyCoke(28));
- System.out.println(buyCoke(50));
- }
- public static int buyCoke(int num)
- {
- int coke = 0;
- int cap = 0;
- int buy = 0;
- while (coke < num)
- {
- buy++;
- coke++;
- cap++;
- if (cap == 3)
- {
- coke++;
- cap = 1;
- }
- }
- return buy;
- }
- }
- package com.gotoheima;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.Reader;
- /**
- * 自定义字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,能够在读取的每行前面都加上有行号和冒号。
- *
- */
- public class Test20
- {
- public static void main(String[] args)
- {
- MyLineNumberReader mlnr = null;
- try
- {
- mlnr = new MyLineNumberReader(new FileReader("F:\\java\\day09\\Demo.java"));
- String len = null;
- while ((len = mlnr.myReadLine()) != null)
- {
- System.out.println(mlnr.getCount()+":\t"+len);
- }
- }
- catch (IOException e)
- {
- System.out.println("读取出错!");
- }
- finally
- {
- try
- {
- if (mlnr != null)
- mlnr.myclose();
- }
- catch (IOException e)
- {
- System.out.println("关闭出错");
- }
- }
- }
- }
- class MyLineNumberReader
- {
- private Reader r;
- private int count;
- MyLineNumberReader(Reader r)
- {
- this.r = r;
- }
- public String myReadLine() throws IOException
- {
- StringBuffer sb = new StringBuffer();
- int num = 0;
- count++;
- while ((num = r.read()) != -1)
- {
- if (num == '\r')
- continue;
- if (num == '\n')
- return sb.toString();
- else
- sb.append((char)num);
- }
- if (sb.length() != 0)
- return sb.toString();
- return null;
- }
- public void myclose() throws IOException
- {
- r.close();
- }
- public int getCount() {
- return count;
- }
- public void setCount(int count) {
- this.count = count;
- }
- }
- package com.gotoheima;
- /**
- * 使用TCP协议写一个可以上传文件的服务器和客户端。
- *
- *
- */
- public class Test21
- {
- public static void main(String[] args)
- {
- }
- }
- import java.io.*;
- import java.util.*;
- /**
- 把一个文件夹下的包括子文件夹里的所有.java文件复制到另一个文件夹下面,并改名.txt
- 对一个文件夹进行递归,筛选出.java文件,并存到集合中
- 用IO字符流操作复制文件,并将.java文件改成.txt
- */
- class DirPrintListDemo
- {
- public static void main(String[] args) throws IOException
- {
- //源文件夹
- File dir = new File("F:\\java");
- //目标文件夹
- File copy = new File("F:\\copy_java");
- if (!copy.exists())
- copy.mkdir();
- ArrayList<File> dirlist = new ArrayList<File>();
- fileToArray(dir,dirlist);
- copy_ReName(dirlist,copy);
- }
- //函数功能,把一个文件夹中包括子文件夹的所有.java文件存到dirlist集合中
- public static void fileToArray(File dir,List<File> dirlist)
- {
- File[] files = dir.listFiles();
- for (File file : files )
- {
- if (file.isDirectory())
- fileToArray(file,dirlist);
- else
- {
- if (file.getName().endsWith(".java"))
- dirlist.add(file);
- }
- }
- }
- //函数功能,用IO字符流操作复制文件,并将.java文件改成.txt
- public static void copy_ReName(List<File> dirlist,File copy) throws IOException
- {
- //遍历集合
- for (File files : dirlist)
- {
- BufferedReader br = new BufferedReader(new FileReader(files));
- String newName = files.getName().replace(".java",".txt");
- BufferedWriter bw = new BufferedWriter(new FileWriter(new File(copy,newName)));
- String len = null;
- while ((len = br.readLine()) != null)
- {
- bw.write(len);
- bw.newLine();
- bw.flush();
- }
- br.close();
- bw.close();
- }
- }
- }
- package com.gotoheima;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- import java.util.TreeMap;
- /**
- * 编写一个类,在main方法中定义一个Map对象(采用泛型),加入若干个对象,然后遍历并打印出各元素的key和value。
- *
- *
- */
- public class Test22
- {
- public static void main(String[] args)
- {
- Map<String,Integer> m = new TreeMap<String,Integer>();
- m.put("Lilei", 10);
- m.put("hanmeimei", 9);
- //第一种
- Set<Map.Entry<String,Integer>> entrySet = m.entrySet();
- for (Iterator<Map.Entry<String,Integer>> it = entrySet.iterator();it.hasNext(); )
- {
- Map.Entry<String,Integer> me = it.next();
- String key = me.getKey();
- Integer value = me.getValue();
- System.out.println(key+"-------"+value);
- }
- //第二种
- Set<String> keySet = m.keySet();
- for (Iterator<String> it = keySet.iterator();it.hasNext(); )
- {
- String key = it.next();
- Integer value = m.get(key);
- System.out.println(key+"-------"+value);
- }
- }
- }
- package com.gotoheima;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- import java.util.TreeMap;
- /**
- * 把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出,例如:
- a: 21 次
- b: 15 次
- c:: 15 次
- 把: 7 次
- 当: 9 次
- 前: 3 次
- ,:30 次
- *
- *
- */
- public class Test23
- {
- public static void main(String[] args) throws IOException
- {
- BufferedReader br = new BufferedReader(new FileReader("面试题.txt"));
- BufferedWriter bw = new BufferedWriter(new FileWriter("tongji.txt"));
- TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
- String len = null;
- while ((len = br.readLine()) != null)
- {
- char[] chs = len.toCharArray();
- for (int i=0;i<chs.length;i++)
- {
- Integer value = tm.get(chs[i]);
- if (value == null)
- {
- tm.put(chs[i],1);
- }
- else
- {
- value += 1;
- tm.put(chs[i], value);
- }
- }
- }
- Set<Map.Entry<Character,Integer>> entrySet = tm.entrySet();
- for (Iterator<Map.Entry<Character,Integer>> it = entrySet.iterator();it.hasNext(); )
- {
- Map.Entry<Character, Integer> me = it.next();
- Character key = me.getKey();
- Integer value = me.getValue();
- bw.write(key+": "+value+"次");
- bw.newLine();
- bw.flush();
- }
- br.close();
- bw.close();
- }
- }
- package com.gotoheima;
- /**
- * 在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1。
- * 要搜索的字符数组和字符都以参数形式传递传递给该方法,如果传入的数组为null, 应抛出IllegalArgumentException异常。在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确,
- * 例如,字符不存在,字符存在,传入的数组为null等。
- *
- *
- */
- public class Test24
- {
- public static void main(String[] args)
- {
- char[] chs = {'z','a','c','t','c'};
- IsTest it = new IsTest();
- System.out.println(it.isTest(chs, 'c'));
- System.out.println(it.isTest(chs, 'v'));
- System.out.println(it.isTest(null, 'c'));
- }
- }
- class IsTest
- {
- public int isTest(char[] chs,char ch)
- {
- if (chs == null)
- throw new IllegalArgumentException("传入字符数组不能为空!");
- for (int i=0;i<chs.length;i++)
- {
- if (ch == chs[i])//基本数据类型不能用equals,用==
- return i;
- }
- return -1;
- }
- }
- package com.gotoheima;
- import java.io.FileReader;
- import java.lang.reflect.Method;
- import java.util.Properties;
- /**
- * 已知一个类,定义如下:
- package cn.itcast.heima;
- public class DemoClass {
- public void run()
- {
- System.out.println("welcome to heima!");
- }
- }
- (1) 写一个Properties格式的配置文件,配置类的完整名称。
- (2) 写一个程序,读取这个Properties配置文件,获得类的完整名称并加载这个类,用反射 的方式运行run方法。
- *
- *
- */
- public class Test25
- {
- public static void main(String[] args) throws Exception
- {
- Properties prop = new Properties();
- prop.load(new FileReader("cn\\itcast\\heima\\class.propertise"));
- String className = prop.getProperty("name");
- Class clazz = Class.forName(className);
- Method method = clazz.getMethod("run", null);
- method.invoke(clazz.newInstance());
- }
- }
- package com.gotoheima;
- import java.util.Scanner;
- /**
- * 金额转换,阿拉伯数字转换成中国传统形式。例如:101000001010 转换为壹仟零壹拾亿零壹仟零壹拾圆整
- *
- * 扫描一个数转换字符串存进一个数组中
- * 定义两个字符数组,分别是零壹贰叁肆伍陆柒捌玖,圆拾佰扦萬拾万佰萬仟萬亿拾亿佰亿仟亿萬亿
- *
- */
- public class Test26
- {
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- System.out.print("请输入金额:");
- String str = sc.nextLine();
- System.out.println(getChinese(str));
- }
- public static String getChinese(String str)
- {
- String[] str1 = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
- String[] str2 = {"整圆","拾","佰","扦","萬","拾万","佰萬","仟萬","亿","拾亿","佰亿","仟亿","萬亿"};
- StringBuilder sb = new StringBuilder();
- char[] chs = str.toCharArray();
- for (int i=chs.length-1;i>=0;i--)
- {
- }
- return sb.toString();
- }
- }
- package com.gotoheima;
- /**
- * 方法中的内部类能不能访问方法中的局部变量,为什么?
- *
- * 因为内部类的生存周期比方法中的局部变量长,局部变量再作用域完成后就会被栈内存释放销毁。要想访问局部变量,那么局部变量必须被final修饰。
- *
- */
- public class Test27 {
- }
- package com.gotoheima;
- /**
- * 有一个类为ClassA,有一个类为ClassB,在ClassB中有一个方法b,此方法抛出异常,在ClassA类中有一个方法a,
- * 请在这个方法中调用b,然后抛出异常。在客户端有一个类为TestC,有一个方法为c ,请在这个方法中捕捉异常的信息。
- * 完成这个例子,请说出java中针对异常的处理机制。
- *
- * java中的异常处理机制,谁调用谁处理,如果一直抛出最终会抛给虚拟机
- *
- */
- public class Test28
- {
- public static void main(String[] args)
- {
- C.c();
- }
- }
- class A
- {
- public static void a() throws Exception
- {
- B.b();
- }
- }
- class B
- {
- public static void b() throws Exception
- {
- throw new Exception("b");
- }
- }
- class C
- {
- public static void c()
- {
- try
- {
- A.a();
- }
- catch(Exception e)
- {
- System.out.println("处理");
- }
- }
- }
- package com.gotoheima;
- import java.lang.reflect.Field;
- /**
- * 写一个方法,此方法可将obj对象中名为propertyName的属性的值设置为value.
- *
- * public void setProperty(Object obj, String propertyName, Object value)
- * {
- * }
- *
- * 考点是反射
- */
- public class Test29
- {
- public static void main(String[] args)
- {
- Person p = new Person();
- System.out.println(p.name+"---"+p.age);
- setProperty(p,"name","Hanmeimei");
- setProperty(p,"age",19);
- System.out.println(p.name+"---"+p.age);
- }
- public static void setProperty(Object obj,String propertyName,Object value)
- {
- try
- {
- Field f = obj.getClass().getDeclaredField(propertyName);
- f.setAccessible(true);
- f.set(obj, value);
- f.setAccessible(false);
- }
- catch (Exception e)
- {
- System.out.println("修改错误");
- }
- }
- public static class Person
- {
- public String name = "Lilei";
- private int age = 18;
- }
- }
- package com.gotoheima;
- import java.util.LinkedList;
- /**
- * 有100个人围成一个圈,从1开始报数,报到14的这个人就要退出。然后其他人重新开始,从1报数,到14退出。问:最后剩下的是100人中的第几个人?
- *
- */
- public class Test30
- {
- public static void main(String[] args)
- {
- final int num = 14;
- int count = -1;
- LinkedList<Integer> ll = new LinkedList<Integer>();
- for (int i=0;i<100;i++)
- {
- ll.add(i+1);
- }
- while (ll.size() != 1)
- {
- for (int x=1;x<=num;x++)
- {
- count ++;
- if (count>=ll.size())
- count = 0;
- }
- ll.remove(count);
- count--;
- }
- System.out.print(ll.get(0));
- }
- }