A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© zuopiezi 中级黑马   /  2015-6-11 14:36  /  573 人查看  /  7 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

提示: 该帖被管理员或版主屏蔽

7 个回复

倒序浏览
2、  请列举您了解的一些排序算法,并用Java语言实现一个效率较高的。
答:冒泡排序、直接选择排序、快速排序、堆排序、直接插入排序、折半插入排序
  1. public class Test2 {
  2.         public static void bubbleSotr(int [] array)
  3.         {
  4.                 for (int i = 0; i < array.length; i++) {
  5.                         for (int j = i+1; j < array.length; j++) {
  6.                                 if (array[i]>array[j]) {
  7.                                         int temp = array[i];
  8.                                         array[i] =array[j];
  9.                                         array[j] = temp;
  10.                                        
  11.                                 }
  12.                         }
  13.                 }
  14.         }
  15. }
复制代码
回复 使用道具 举报
3、判断一个字符串是否是对称字符串,例如"abc"不是对称字符串,"aba"、"abba"、"aaa"、"mnanm"是对称字符串
  1. public class Test3 {
  2.         public static void main(String[] args)
  3.         {
  4.                 String s="abdcdba"; //定义一个字符串
  5.                 show(s);
  6.                
  7.         }


  8.         public static String reverseString(String s)
  9.         {
  10.                 char[] chs = s.toCharArray();

  11.                 reverse(chs);
  12.                 return new String(chs);

  13.         }
  14.         private static void reverse(char[] arr)
  15.         {
  16.                 for (int start =0,end=arr.length-1;start<end ;start++,end-- )
  17.                 {
  18.                         swap(arr,start,end);
  19.                 }
  20.         }
  21.         private static void swap(char[] arr,int x,int y)
  22.         {
  23.                 char temp = arr[x];
  24.                 arr[x] = arr[y];
  25.                 arr[y] = temp;
  26.         }
  27.         public static void show(String s)
  28.         {
  29.                
  30.                 if (s.equals(reverseString(s)))
  31.                         System.out.println("对称");
  32.                 else if(s.length()%2!=1)
  33.                         System.out.println("字符串有问题");
  34.                 else
  35.                         System.out.println("不对称");
  36.         }

  37. }
复制代码

回复 使用道具 举报
4、 从键盘接受一个数字,打印该数字表示的时间,最大单位到天,
例如:        键盘输入6,打印6秒;
                键盘输入60,打印1分;
                键盘输入66,打印1分6秒;
                键盘输入666,打印11分6秒;
                键盘输入3601,打印1小时1秒
  1. public class Test4 {

  2.         public static void main(String[] args)
  3.     {
  4.             int second,minute,hour,day;
  5.             Scanner in = new Scanner(System.in);//键盘录入
  6.             int time;
  7.             //循环读键盘输入的一个整数
  8.             System.out.print("请输入一个整数(0表示退出):");
  9.             while((time = in.nextInt()) != 0)
  10.             {
  11.                     if(time < 60)
  12.                     {
  13.                             second = time;
  14.                             System.out.println(second+"秒");
  15.                     }
  16.                     else if(time>=60 && time < 3600)
  17.                     {
  18.                             minute = time/60;
  19.                             second = time%60;
  20.                             System.out.println(minute+"分"+second+"秒");
  21.                     }
  22.                     else if(time>=3600 && time < 216000)
  23.                     {
  24.                             hour = time/3600;
  25.                             minute = (time%3600)/60;
  26.                             second = time%60;
  27.                             System.out.println(hour+"时"+minute+"分"+second+"秒");
  28.                     }
  29.                     else if(time >= 216000)
  30.                     {
  31.                             day = time/216000;
  32.                             hour = (time%216000)/3600;
  33.                             minute = (time%3600)/60;
  34.                             second = time%60;
  35.                             System.out.println(day+"天"+hour+"时"+minute+"分"+second+"秒");
  36.                     }
  37.                     else
  38.                             System.out.println("输入数据有误,请输入整数");
  39.             }
  40.     }
  41. }
复制代码
回复 使用道具 举报
5、 使用带缓冲功能的字节流复制文件。
  1. public class Test5 {
  2.   public static void main(String[] args)
  3.   {
  4.           BufferedInputStream bufis =null;
  5.           BufferedOutputStream bufos= null;
  6.           try {
  7.                 bufis = new BufferedInputStream(new FileInputStream("c:\\1.txt"));
  8.                
  9.                 bufos = new BufferedOutputStream(new FileOutputStream("D:\\2.txt"));
  10.                
  11.                 int line = 0;
  12.                 while((line=bufis.read())!=-1)
  13.                 {
  14.                         bufos.write(line);
  15.                 }
  16.                   
  17.         } catch (Exception e) {
  18.                 // TODO: handle exception
  19.                 throw new RuntimeException("文件拷贝失败");
  20.         }
  21.           finally
  22.           {
  23.                   try {
  24.                           if(bufis!=null)
  25.                         bufis.close();//关闭读取流,并处理异常
  26.                 } catch (Exception e2) {
  27.                         // TODO: handle exception
  28.                         throw new RuntimeException("文件读取关闭失败");
  29.                 }
  30.                   
  31.                   try {
  32.                           if(bufis!=null)
  33.                         bufos.close();//关闭写入流,并处理异常
  34.                 } catch (Exception e2) {
  35.                         // TODO: handle exception
  36.                         throw new RuntimeException("文件写入关闭失败");
  37.                 }
  38.           }
  39.           
  40.   }
  41. }
复制代码
回复 使用道具 举报
10、使用TCP协议完成一个客户端一个服务器。 客户端从键盘输入读取一个字符串,发送到服务器。
        服务器接收客户端发送的字符串,反转之后发回客户端。客户端接收并打印。
客户端代码
  1. public class Test10_Client {
  2.        
  3.                 public static void main(String[] args)throws Exception
  4.                 {
  5.                          Socket s = new Socket( "192.168.0.106" , 8000); // 创建 Socket 对象;

  6.                       BufferedReader buf = new BufferedReader(

  7.                             new InputStreamReader(System. in )); // 从键盘获取输入流到缓冲区;

  8.                       BufferedWriter bufout= new BufferedWriter( new OutputStreamWriter(s.getOutputStream())

  9.                             ); // 创建一个输出流

  10.                       BufferedReader bufIn = new BufferedReader( new InputStreamReader(

  11.                             s.getInputStream())); // 获取输入流 …… ;  

  12.                       String line = null ;

  13.                       while ((line = buf.readLine()) != null ) {

  14.                          if ( "over" .equals(line))

  15.                             break ;

  16.                             bufout.write(line);

  17.                             bufout.newLine();

  18.                             bufout.flush();

  19.                             String str=bufIn.readLine();

  20.                            System.out.print( "server:" +str);

  21.                       }
  22.                       s.close();
  23.                        
  24.                 }
  25.                
  26. }
复制代码

服务器端:
  1. public class Test10_Server {
  2.         public static void main(String []agrs) throws Exception

  3.            {

  4.               ServerSocket ss= new ServerSocket(8000);//创建ServerSocket对象

  5.                Socket s= ss.accept();

  6.                String ip = s.getInetAddress().getHostAddress();//获取本地Ip

  7.                System.out.println(ip+ "----------------------- connected!" );

  8.                BufferedReader buf = new BufferedReader( new InputStreamReader(s.getInputStream()));//创建输入流

  9.                BufferedWriter bufout= new BufferedWriter( new OutputStreamWriter(s.getOutputStream())

  10.                     ); // 创建一个输出流



  11.                String line= null ;

  12.                while ((line=buf.readLine())!= null ){

  13.                   System.out.println(line);

  14.                   StringBuffer re= new StringBuffer(line); // 字符串反转处理

  15.                   String str=re.reverse().toString();

  16.                   System.out.println(re+ "......" +str); // 服务器本地输出

  17.                   bufout.write(str); // 将字符反转并输出到客户端

  18.                   bufout.newLine();

  19.                   bufout.flush();

  20.                }

  21.             s.close();

  22.            }

  23. }
复制代码
回复 使用道具 举报
meng12 中级黑马 2015-6-11 15:31:24
7#
赞一个,楼主太给力啦!
回复 使用道具 举报
其他的都太简单了。就没往上复制了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马