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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ADS1993 中级黑马   /  2015-2-2 21:04  /  1302 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

做完这些题,感觉压力很大,好多概念都模糊不清,还得继续努力。
  1. package com.itheima;

  2. import java.util.ArrayList;
  3. import java.util.Iterator;

  4. /**
  5. * 第2题:创建ArrayList对象,添加5个元素,使用Iterator遍历输出
  6. * @author 刘云祥
  7. *
  8. */
  9. public class Test2 {

  10.         public static void main(String[] args) {
  11.                 // TODO Auto-generated method stub
  12.                
  13.                  ArrayList<String> list= new ArrayList<String>();//创建  ArrayList  集合
  14.                   
  15.                  list.add(new String("第一个元素")); //向集合中添加元素
  16.                  list.add(new String("第二个元素"));
  17.                  list.add(new String("第三个元素"));
  18.                  list.add(new String("第四个元素"));
  19.                  list.add(new String("第五个元素"));
  20.                 
  21.                
  22.                  Iterator<String> iterators = list.iterator();//获取  Iterator 对象
  23.                 
  24.                  while (iterators.hasNext()) //判断  ArrayList 集合中是否存在下一个元素
  25.                  {        
  26.                          Object obj = iterators.next(); //取出  ArrayList 集合中的元素
  27.                          System.out.println(obj);       //打印到控制台
  28.                          
  29.                  }
  30.                 }
  31.         }
复制代码
  1. package com.itheima;

  2. import java.util.Scanner;

  3. /**
  4. * 第3题:求斐波那契数列第n项,n<30,斐波那契数列前10项为 1,1,2,3,5,8,13,21,34,55
  5. * @author 刘云祥
  6. *
  7. */
  8. public class Test3 {   

  9.         public static int fib(int n){       //创建一个 递归 方法  
  10.                 if(n==1 || n==2)  return 1;     //递归出口
  11.                   if(n>2)
  12.                         return fib(n-2)+fib(n-1);  //递归调用
  13.                       return -1;
  14.         }
  15.        
  16. public static void fibPrint()    //创建一个 控制台 打印 方法
  17.           {
  18.                Scanner in=new Scanner(System.in);//扫描从控制台输入的字符
  19.             System.out.print("请输入项数:");
  20.              int n=in.nextInt();                 //输入数字赋值给n
  21.               if(n<30){
  22.                  System.out.println("斐波那契数列第"+n+"项为:"+fib(n));//调用 fib 方法 并打印 第n 项
  23.                     }
  24.               else{
  25.                           System.out.println("请输入小于30的整数"); }
  26.               
  27.                   in.close(); //关闭

  28.       }
  29.           
  30. public static void main(String[] args) {
  31.                 // TODO Auto-generated method
  32.                  fibPrint();//调用  fibPrint() 方法

  33.         }

  34. }
复制代码
  1. package com.itheima;
  2. /**
  3. * 第5题: 数组去重复,例如: 原始数组是{4,2,4,6,1,2,4,7,8},得到结果{4,2,6,1,7,8}
  4. * @author 刘云祥
  5. *
  6. */
  7. public class Test5 {

  8. public static void main(String[] args) {
  9.                 // TODO Auto-generated method stub
  10.                  int [] arr = {4,2,4,6,1,2,4,7,8};//创建并初始化数组
  11.                   for(int i = 0;i < Rmdistinct(arr).length; i++){//遍历数组
  12.                         System.out.print(Rmdistinct(arr)[i]+"  ");//调用 并打印 数组去重复 后 的值
  13.                                 }
  14.                 }

  15. public static int[] Rmdistinct(int[] arr){  //创建一个 方法  用以取出 数组中的重复 元素
  16.         int length=1;  //定义一个  int 型变量  
  17.         boolean Judgement=false;  // 定义一个 布尔型 变量
  18.       for(int i=1;i<arr.length;i++){  //对数组  进行 遍历
  19.                   for(int j=0;j<length;j++){  //对数组中的元素 进行 对比  
  20.                       if(arr[i]==arr[j]){  //比较数组中 重复的元素
  21.                          Judgement=true;  
  22.                             }  
  23.                            }  
  24.                  
  25.         if(!Judgement){  //如果数组某一元素对比不重复  则 进入
  26.                  arr[length]=arr[i];  //替换元素为当前值
  27.                      length++;  //长度自加一
  28.                             }            
  29.                   Judgement=false;  //复位变量值
  30.                         }  
  31.                         
  32.                 int[] newArr=new int[length]; //创建一个整型数组
  33.                 System.arraycopy(arr, 0, newArr, 0, length);  //复制指定长度数组
  34.                 return newArr;  //返回新的数组元素
  35.                     }
  36.        }
复制代码
  1. package com.itheima;
  2. /**
  3. * 写出以下代码执行结果,分析为什么?(没有分析结果不得分)
  4. *    public class Test {
  5.                public static void main(String[] args) {
  6.                         String s = "abc";
  7.                         s.substring(1);
  8.                         s.replace("bc", "xyz");
  9.                        System.out.println(s);
  10.                        String value = new String ("abc");
  11.                        System.out.println(s == value);
  12.                 }
  13.        }
  14. * 输出结果:
  15. * abc
  16. * false
  17. * 分析:
  18. *
  19. * 字符串是常量,一般是存放在字符串常量池中,一旦创建就无法改变,
  20. * 而且    s.substring(1);和   s.replace("bc", "xyz");  
  21. * 只是对字符串 进行操作,却没有改变 s 中字符串
  22. *
  23. * Java把内存分成两种,一种叫做栈内存,一种叫做堆内存
  24. * 一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配
  25. * 堆内存用于存放由new创建的对象和数组
  26. * ==操作比较的是两个变量的值是否相等,
  27. * 对于引用型变量表示的是两个变量在堆中存储的地址是否相同,即栈中的内容是否相同。
  28. * 由于  s  与   value   所指向的存放 位置 不同   所以  地址 也不相同
  29. * @author 刘云祥
  30. *
  31. */
  32. public class Test6 {

  33.             public static void main(String[] args) {
  34.         String s = "abc";//栈中开辟一块空间存放引用s,s指向池中String常量"abc",
  35.         s.substring(1);///从位置1处截取s字符串并返回,但是没有被变量接受。
  36.         s.replace("bc", "xyz");//返回一个新的字符串,并用字符串“xyz”代替原字符串的“bc”,也没有被变量接受。
  37.         System.out.println(s); //打印 输出
  38.         String value = new String ("abc");// 栈中开辟一块空间存放引用value;
  39.                                           //堆中开辟一块空间存放一个新建的String对象"abc";  
  40.         System.out.println(s == value);//打印s是否等于value
  41.    }
  42.   }
复制代码
  1. package com.itheima;
  2. import java.util.Scanner;
  3. /**
  4. * 第9题: 写一个正则表达式,可以匹配尾号5连的手机号。规则: 第1位是1,第二位可以是数字3458其中之一,
  5. * 后面4位任意数字,最后5位为任意相同的数字。例如:18601088888、13912366666
  6. * @author 刘云祥
  7. *
  8. */
  9. public class Test9 {
  10.          public static void main(String[] args) {
  11.               String regex = "1[3|4|5|8]{1}[0-9]{4}(\\d{1})\\1{4}";
  12.               System.out.println("输入需要匹配的号码");  
  13.                Scanner str = new Scanner(System.in);  
  14.                String s1 = str.next();  
  15.                 
  16.                 char cchar[] = s1.toCharArray();  
  17.                 String s2 = new String(cchar);
  18.                
  19.                  if(s2.matches(regex)){
  20.                      System.out.println("匹配成功!");
  21.                  }
  22.                   else{
  23.                           System.out.println("匹配失败!");
  24.                            }
  25.            str.close();
  26.             
  27.             }
  28.         }
复制代码
  1. package com.itheima;

  2. import java.net.*;
  3. import java.io.*;

  4. /**
  5. * 第10题:使用TCP协议完成一个客户端一个服务器。客户端从键盘输入读取一个字符串,发送到服务器。
  6. * 服务器接收客户端发送的字符串,反转之后发回客户端。客户端接收并打印。
  7. * @author 刘云祥
  8. * 先运行服务端
  9. */

  10. public class TestA //服务端
  11. {
  12.         public static void main(String[] args) throws IOException
  13.         {
  14.                
  15.             ServerSocket ss = new ServerSocket(30000);//创建一个ServerSocket,用于监听客户端Socket的连接请求
  16.                 Socket s = ss.accept();//取得连接的Socket对象
  17.                 BufferedReader server_buf=new BufferedReader(new InputStreamReader(s.getInputStream()));//输入流
  18.         String str=server_buf.readLine();
  19.         System.out.println("服务端接受到字符串为:");
  20.         System.out.println(str);        
  21.         PrintStream pri=new PrintStream(s.getOutputStream());//对输出流进行 包装
  22.         System.out.println("将字符串反转后发送给客户端...");
  23.         pri.println(new StringBuffer(str).reverse());
  24.                         //关闭输出流
  25.                         pri.close();
  26.                         s.close();
  27.                         ss.close();
  28.         }
  29. }
复制代码
  1. package com.itheima;

  2. import java.net.*;
  3. import java.util.Scanner;
  4. import java.io.*;
  5. /**
  6. * 第10题:使用TCP协议完成一个客户端一个服务器。客户端从键盘输入读取一个字符串,发送到服务器。
  7. * 服务器接收客户端发送的字符串,反转之后发回客户端。客户端接收并打印。
  8. * @author 刘云祥
  9. * 先运行服务端
  10. */

  11. public class TestA1 //客户端
  12. {
  13.         public static void main(String[] args) throws IOException
  14.         {
  15.           Socket socket = new Socket(InetAddress.getLocalHost(), 30000);//创建一个ServerSocket,
  16.           BufferedReader br = new BufferedReader( new InputStreamReader(socket.getInputStream()));//输入流

  17.          PrintStream pri=new PrintStream(socket.getOutputStream());//输出
  18.      Scanner sc=new Scanner(System.in);
  19.      System.out.println("请输入一个字符串:");
  20.      String str=sc.nextLine();
  21.      System.out.println("正在向服务器发送字符串...");
  22.      pri.println(str);
  23.      System.out.println("接受来自服务器反转的字符串...");
  24.      System.out.println(br.readLine());  
  25.         //关闭
  26.         sc.close();
  27.                 br.close();
  28.                 socket.close();
  29.         }
  30. }
复制代码



10 个回复

倒序浏览
这是什么基础题?
回复 使用道具 举报

就是自荐信 通过之后   的 基础测试
回复 使用道具 举报
路过的,先看看
回复 使用道具 举报
这些题还是挺考基本功的
回复 使用道具 举报
感谢楼主分享
回复 使用道具 举报
努力学习中
回复 使用道具 举报
ADS1993 发表于 2015-2-2 21:55
就是自荐信 通过之后   的 基础测试

哦,知道了,谢谢
回复 使用道具 举报
ADS1993 发表于 2015-2-2 21:55
就是自荐信 通过之后   的 基础测试

哦,知道了,谢谢
回复 使用道具 举报
容我慢慢的看一看
回复 使用道具 举报
回去好好复习下吧
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马