Java知识点复习系列(0)

来源:互联网 发布:淘宝网首页官网十字绣 编辑:程序博客网 时间:2024/06/16 06:24

前言

这是今天对一些Java知识点的复习笔记 , 以后每天的学习笔记和一些心得都会写在博客上和大家一起分享 , 希望能和大家一起进步!

复习知识点

  1. 常用字符串操作
  2. 反制机制操作数组(包括数组操作)
  3. 常用时间处理操作
  4. 一个非常简单的题目(递归的练习)

笔记代码

所有总结和一些注意点和一些心得写在代码的注释上了.

package Test;import java.lang.reflect.Array;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Scanner;/** * Java基础知识点复习(常用类库) * 知识点: * 1.字符串操作 * 2.数组操作 * 3.时间处理操作 * 总结: * 上面三个操作涉及到的知识点我都给大家整理好了 , 并根据个人心得做出了一些修改 , 每一个都给了相应的案例  . 这三个操作类库是基础的 也用的非常多, 所以一定要掌握 . * @author Administrator * */public class TestMy12_3 {    /**     * 字符串常用的操作方法和一些小技巧(不多不难 , 大家放心)     */    public static void operStr() {    Scanner s = new Scanner(System.in );    try {        //0.用户输入Ghoset  Ghosetis Very Ghoset Good! 请查找Ghoset出现最后的位置        System.out.println("Boss Please Input Here:" );        String line = s.nextLine();        //1.开始查找        int index = line.lastIndexOf("Ghoset" );        if(index > -1 ) {        System.out.println(index + " is Last index of!" );        }        //2.请删除字符串中的所有为is的字符        index = line.indexOf("is" );        while(index > -1 ) {        line.substring(index , index + "is".length() );        //"is"前的数据 + "is"后的数据          line = line.substring(0 , index ) + line.substring(index + "is".length() );        index = line.indexOf("is" );        }        System.out.println("删除的最后结果为:" + line );        //3.这个时候 , boss要求将Ghoset全部换成Boss , 怎么办?        line = line.replaceAll("Ghoset", "Boss" );      //replace替换第一个找到的        System.out.println("替换的最后结果为:" + line );        //4.反转字符串  , 这里有一个心得 , 我们将字符串创建成一个对象然后进行操作.        StringBuffer strBuffer = new StringBuffer(line );        line = strBuffer.reverse().toString();        System.out.println("反转字符中的结果为:" + line );        //5.字符串的查找 , 这个时候 , 请找出ssoB的值 , 使用indexOf        index = line.indexOf("ssoB" );          System.out.println("找到的结果为:" + index );        //6.小写转大写        line = line.toUpperCase();      //大转小:toLowerCase()        System.out.println("结果为:" + line );        //7.连接字符串        line = line.concat("  Ghoset is Very Good!" );        System.out.println("结果为:" + line );    }catch(Exception e ) {        e.printStackTrace();    }    }    /**     * 数组操作(利用反射机制创建一个可变长度的数组)     * 其它知识点:     * 1. 关于数组的判断 , 我们可以用equals()方法      * 2. 关于数组的删除 , 我们可以利用remove()方法     * 心得:     * 一般来说 , 我们不会用到数组  , 因为它的声明周期是不可变的 , 而且添加删除数据麻烦 .      * 我们一般都用集合框架 , Java提供的集合框架非常给力.     */    public static Object createNewArray(Object oldArray ) {    //1.获取传进来数据的类型    Class cls = oldArray.getClass();    if(cls.isArray() ) {    //2.获取数组原来的Type属性 , 因为我们创建数组要和原来的类型一样    Class type = cls.getComponentType();    //3.获取数组的长度 , 这里有一个知识点 , 使用Array提供的Static方法getLength    int length = Array.getLength(oldArray );            //4.创建新的数组    Object newArray = Array.newInstance(type , length + 5 );    //5.将原来的数组的数据复制到新的数组中 , 注意 , 这里是System提供的ArrayCopy方法        //System.arraycopy(源数组, 起始位置 , 目标数组, 起始位置 , 复制源数组的长度 );    System.arraycopy(oldArray , 0 , newArray , 0 , length );    return newArray;    }    return null;    }      /**     * 时间处理     * 知识点:     * 1. 时间格式化操作(Date->String , String->Date)     * 2. 时间运算操作和获取年,月,日操作     * 心得:     * 关于时间处理 , Java中只要掌握这两个类库完全够了.     */    public static void dateTime() {    //问题1. 时间格式化操作(SimpleDateFormat类库) , 将现在时间以2017年11月1日 10点11分20秒格式输出?        //关于这个类库会两个方法就可以了, format 和 parse(将String类型转为Date类型)    Date nowDate = new Date();      //获取当前时间缀    SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 hh点mm分ss秒" );     String nowDate1 = format.format(nowDate );  //使用format方法 , 将date类型转为String类型    System.out.println("当前的时间为:" + nowDate1 );      //*****************************************************************    //问题2. 时间处理类库(Calendar类库 ) , 可以进行时间运算和获取年 , 月 , 日等等操作    Calendar calendar = Calendar.getInstance();     //单例模式的本质就是防止多次创建这个对象. 它保证只创建了一次这个对象.    calendar.setTime(nowDate );             //给它设置当前时间缀 , 可以说成是初始化这个类库    //注意:它获取的话是通过一个Get方法 .    int year = calendar.get(Calendar.YEAR );    System.out.println("当前年份:" + year );    //时间运算操作        //往后5年是第几年?    calendar.add(Calendar.YEAR , 5 );    int afterYear = calendar.get(Calendar.YEAR );        //往前5年是第几年?    calendar.setTime(nowDate );     calendar.add(Calendar.YEAR , -5 );      //在它这里只有加法  , 但是我们要明白 , 在计算机中任何运算都可以看成是加法     int beforeYear = calendar.get(Calendar.YEAR );    System.out.println("往后的五年是:" + afterYear + ", 往前的五年是:" + beforeYear );    }    /**     * 测试方法     * @param args     */    public static void main(String[] args) {    //operStr();//  Integer iArray[] = new Integer[] {//      1 , 2 , 3 , 4 , 5//  };//  iArray = (Integer[]) createNewArray(iArray );    dateTime();    }}
  • 简单递归练习代码
  /**     * 问题一:求随意指定一个整数的阶乘     * 知识点:     * 0. 获取用户输入     * 1. 函数递归的运用     * 2. 逻辑思维的训练   公式:n!=1×2×3×...×n。阶乘亦可以递归方式定义:0!=1,n!=(n-1)!×n。     * 3. 循环语句的运用     */    public static long factorial(int number ) {    //直接对应着知识点2的公式 , 所以做题前一定要先思考 . 这样才有助于做题    if(number <= 1 ) {        return 1;    }    return (number - 1) * number;    }    public static void test() {    //1.声明变量    Scanner s = new Scanner(System.in );    long sum = 1 ;              //为什么是Long类型呢 ? 因为int类型可以不够存放某些数的阶乘    //2.进行运算    System.out.print("请您输入求阶乘的范围:" );    if(s.hasNextInt() ) {        int number = s.nextInt();        for(int i = 0 ; i < number ; i++ ) {        sum = factorial(i );        System.out.println("值为" + i + ":" + "阶乘值为:" + sum );        }    }    }
原创粉丝点击