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

© Kevin.Kang 高级黑马   /  2015-8-18 16:45  /  400 人查看  /  11 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

第一题:
  1. package com.itheima;

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

  4. /*
  5. * 第一题:创建ArrayList对象,添加5个元素,使用Iterator遍历输出
  6. */
  7. public class Test1 {

  8.         public static void main(String[] args) {
  9.                 // 创建集合对象
  10.                 ArrayList<String> al = new ArrayList<String>();

  11.                 // 添加元素
  12.                 al.add("康小广");
  13.                 al.add("李延旭");
  14.                 al.add("任兴亚");
  15.                 al.add("赵磊");
  16.                 al.add("王澳");

  17.                 // Iterator iterator():迭代器,集合的专用遍历方式
  18.                 Iterator<String> i = al.iterator();
  19.                 while (i.hasNext())
  20.                 {
  21.                         String s = i.next();
  22.                         System.out.println(s);
  23.                 }
  24.         }
  25. }
复制代码



11 个回复

倒序浏览
第二题:
  1. package com.itheima;

  2. import java.util.Scanner;

  3. /*
  4. * 第二题:求斐波那契数列第n项,n<30,斐波那契数列前10项为 1,1,2,3,5,8,13,21,34,55
  5. *
  6. * 分析:
  7. *                 1.分析前10个数据可以看出规律,每一项都是前两项之和,前两项都为1
  8. *                 2.根据这个规律利用递归是最简单的
  9. */

  10. public class Test2 {
  11.         public static void main(String[] args) {
  12.                 Scanner sc = new Scanner(System.in);
  13.                 System.out.println("你想求第几项:");
  14.                 int i = sc.nextInt();

  15.                 System.out.println(getNumber(i));
  16.         }

  17.         public static int getNumber(int n) {
  18.                 // 第一项和第二项的时候都是1。
  19.                 if (n == 1 || n == 2) {
  20.                         return 1;
  21.                 } else {
  22.                         // 除了前两项以外,其他斐波那契数都是前面两项之和。
  23.                         return getNumber(n - 1) + getNumber(n - 2);
  24.                 }
  25.         }
  26. }
复制代码



回复 使用道具 举报
第三题:
  1. package com.itheima;
  2. /*
  3. * 第三题:编程计算3乘8等于几,什么方法效率更高?
  4. */
  5. public class Test3 {

  6.         public static void main(String[] args) {
  7.                 //位移运算效率更高,8是2的3次方,就是让3向左位移三位
  8.                 System.out.println(3<<3);
  9.         }
  10. }
复制代码



回复 使用道具 举报
正好现在可以敲敲  哈哈
回复 使用道具 举报
第四题:
  1. package com.itheima;

  2. /*
  3. * 第四题:为什么需要配置path,什么时候需要classpath?
  4. *
  5. * path设置的路径是JDK安装目录下bin文件夹的路径,bin文件存放的是可执行文件,
  6. * 配置path,执行可执行文件的时候,就会自动去path配置的路径中查找, 如果不设置path,只有加上bin文件夹的完整路径才可以,
  7. * 设置后,可直接运行可执行文件,不需要再写路径就可以。 path是为了方便可以在任意一个路径下去执行所要执行的可执行文件
  8. *
  9. * 要运行指定文件夹里面的类的时候需要配置classpath
  10. */
复制代码



回复 使用道具 举报
第五题:
  1. package com.itheima;
  2. /*
  3. * 第五题:编写一个延迟加载的单例设计模式。
  4. */
  5. public class Test5 {

  6. }

  7. class Student {

  8.         private Student() {
  9.         }

  10.         private static Student s = null;

  11.         public Student getStudent() {
  12.                 if (s == null) {
  13.                         s = new Student();
  14.                 }
  15.                 return s;
  16.         }
  17. }
复制代码



回复 使用道具 举报
第六题:
  1. package com.itheima;

  2. import java.util.Arrays;
  3. import java.util.LinkedHashSet;

  4. /*
  5. * 第六题:数组去重复,例如: 原始数组是{4,2,4,6,1,2,4,7,8},得到结果{4,2,6,1,7,8}
  6. *
  7. * 分析:
  8. *                 LinkedHashSet集合有序且唯一,用来接收原始数组,把集合再转为数组就可以了
  9. */
  10. public class Test6 {
  11.         public static void main(String[] args) {

  12.                 int[] arr = { 4, 2, 4, 6, 1, 2, 4, 7, 8 };

  13.                 // 创建集合接受新的数组
  14.                 LinkedHashSet<Integer> newArr = new LinkedHashSet<Integer>();

  15.                 // 集合中不包含就添加进去
  16.                 for (int i : arr) {
  17.                                 newArr.add(i);
  18.                         }

  19.                 // 创建一个长度为集合长度的数组
  20.                 Integer[] i = new Integer[newArr.size()];
  21.                 // 集合转为数组
  22.                 newArr.toArray(i);
  23.                 // 输出数组
  24.                 System.out.println(Arrays.toString(i));
  25.         }
  26. }
复制代码


回复 使用道具 举报
第七题:
  1. package com.itheima;

  2. /*
  3. * 第七题:用代码证明,在try中写了return,后面又写了finally,
  4. *                 是先执行return还是先执行finally?
  5. */
  6. public class Test7 {
  7.         public static void main(String[] args) {
  8.                 System.out.println(getReturn());
  9.                 // 打印结果是:
  10.                 // try
  11.                 // finally
  12.                 // 20
  13.                 // 说明先执行finally,再执行return。
  14.         }

  15.         public static int getReturn() {
  16.                 int a = 10;
  17.                 try {
  18.                         System.out.println("try");
  19.                         a = 20;
  20.                         return a;
  21.                 } catch (Exception e) {
  22.                         e.printStackTrace();
  23.                 } finally {
  24.                         a = 30;
  25.                         System.out.println("finally");
  26.                 }
  27.                 return a;
  28.         }
  29. }
复制代码


回复 使用道具 举报
第八题:
  1. package com.itheima;
  2. /*
  3. 第八题:分析运行结果,说明原理。(没有分析结果不得分)
  4.     class A {
  5.         void fun1() {
  6.                   System.out.println(fun2());
  7.             }
  8.             int fun2() {
  9.                     return 123;
  10.             }
  11.     }
  12.      public class B extends A {
  13.             int fun2() {
  14.                      return 456;
  15.              }
  16.           public static void main(String args[]) {
  17.                     B b = new B();
  18.                     b.fun1();
  19.                     A a = b;
  20.                     a.fun1();
  21.             }
  22.     }
  23.         结果:
  24.                 456
  25.         456
  26.         分析:
  27.                 B b = new B();
  28.                 b.fun1();
  29.                 因为B类继承了A类,继承了A中的所有方法
  30.                 fun1方法是输出fun2(),b调用本类中的fun2方法返回456,结果是456
  31.                
  32.                 A a = b;
  33.                 a.fun1();
  34.                 A类的引用指向了B类的对象,就是多态。多态成员方法编译看左边,运行看右边。
  35.                 a调用A类中的fun1,fun1()又调用fun2(),实际上调用的是子类B中重写的fun2方法,所以运行结果是456。
  36. */               
  37. public class Test8 {

  38. }
复制代码


回复 使用道具 举报
第九题:
  1. package com.itheima;

  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. import java.util.Scanner;

  7. /*
  8. * 第九题:编写程序,该程序启动后用户可以按“yyyy-MM-dd”的格式输入一个日期,
  9. *                  程序计算这一天是星期几,并且计算出是一年中的第几天。
  10. */
  11. public class Test9 {
  12.         public static void main(String[] args) throws ParseException {
  13.                 Scanner sc = new Scanner(System.in);

  14.                 System.out.println("请按“yyyy-MM-dd”的格式输入一个日期:");
  15.                 String s = sc.nextLine();
  16.                 getDay(s);
  17.         }

  18.         public static void getDay(String s) throws ParseException {
  19.                 // 给定格式
  20.                 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

  21.                 // 把输入的String类转化为Date类
  22.                 Date d = format.parse(s);

  23.                 // 使用默认时区和语言环境获得一个日历
  24.                 Calendar time = Calendar.getInstance();

  25.                 // 使用给定的 Date 设置此 Calendar 的时间
  26.                 time.setTime(d);

  27.                 // 得到一年中的第几天
  28.                 int dayOfYear = time.get(Calendar.DAY_OF_YEAR);
  29.                 System.out.println("这是一年中的第" + dayOfYear + "天");

  30.                 // 得到一周中的第几天

  31.                 int dayOfWeek = time.get(Calendar.DAY_OF_WEEK);

  32.                 // 获得星期数
  33.                 String dayOfWeek2 = getWeekDay(dayOfWeek);
  34.                 System.out.println(dayOfWeek2);
  35.         }

  36.         public static String getWeekDay(int i) {
  37.                 String[] week = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

  38.                 // DAY_OF_WEEK字段是从0开始的,减去1才是实际星期数。
  39.                 String weekDay = week[i - 1];
  40.                 return weekDay;
  41.         }
  42. }
复制代码



回复 使用道具 举报
第十题:
  1. package com.itheima;
  2. /*
  3. * 第十题:一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。
  4. * 当老农不和猫狗鱼在一起时,狗会咬猫,猫会吃鱼,当老农和猫狗鱼在一起时,则不会发生这种问题。
  5. * 编程解决猫狗鱼过河问题。
  6. */
  7. import java.util.ArrayList;

  8. public class Test10 {
  9.         // 创建一个集合,代表起点.
  10.         public static ArrayList<String> here = new ArrayList<String>();
  11.         // 创建一个集合,代表终点.
  12.         public static ArrayList<String> there = new ArrayList<String>();

  13.         public static void main(String[] args) {
  14.                 // 添加元素.
  15.                 here.add("Dog");
  16.                 here.add("cat");
  17.                 here.add("fish");

  18.                 take();
  19.         }

  20.         // 定义一个方法,用来判断这三个动物之间关系
  21.         public static boolean safe(ArrayList<String> al) {
  22.                 // 当猫狗,猫鱼在一起的时候是不安全的
  23.                 if (al.contains("dog") && al.contains("cat") || al.contains("cat")
  24.                                 && al.contains("fish")) {
  25.                         return false;
  26.                 }
  27.                 return true;
  28.         }

  29.         // 定义一个方法,将起点的动物送到终点
  30.         public static void take() {
  31.                 // 得到一个动物
  32.                 String s = here.get(0);
  33.                 // 在起点删除此动物
  34.                 here.remove(s);
  35.                 // 判断起点剩下动物关系是否安全
  36.                 if (safe(here)) {
  37.                         // 添加此动物到终点中
  38.                         there.add(s);
  39.                         System.out.println("带着" + s + "\t去往终点。起点有" + here + ",终点有" + there);
  40.                         // 判断终点动物关系是否安全
  41.                         thereSafe();
  42.                 } else {
  43.                         // 动物关系不安全就不带此动物,重新来一次。第一次只能带猫去对面
  44.                         here.add(s);
  45.                         take();
  46.                 }
  47.         }

  48.         // 判断终点的元素
  49.         public static void thereSafe() {
  50.                 // 如果起点没有动物存在,就结束语句。
  51.                 if (here.isEmpty()) {
  52.                         System.out.println(there + "都安全过河了");
  53.                         return;
  54.                 }

  55.                 // 终点安全就接着去起点带动物
  56.                 if (safe(there)) {
  57.                         take();
  58.                 } else {
  59.                         // 不安全就将终点原先动物带回起点
  60.                         String temp = there.get(0);
  61.                         there.remove(temp);
  62.                         here.add(temp);
  63.                         System.out.println("带着" + temp + "\t回到起点。起点有" + here + ",终点有"
  64.                                         + there);
  65.                         take();
  66.                 }
  67.         }
  68. }
复制代码


回复 使用道具 举报
收藏了,慢慢kan
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马