为了账号安全,请及时绑定邮箱和手机立即绑定

【寒假学Java】 第四天收获: 随机数,可变参数,JDBC,访问控制

标签:
Java MySQL
正文之前

突遭大变,家道中落。竟沦落至此!!可悲可叹!!!
好吧, 就是昨晚停电了不是,然后早上起来来电了。美滋滋!!!施施然起床,洗脸刷牙,再回首!又停电??!!!WTF?至今都还没来!!只能继续依靠(充电)宝儿苟活,持续飞行(mode),聊以《一世之尊》此书度日!!辅以《Java编程思想》,好不快哉!!?

正文

第一点:随机数的巧妙!

    public static void main(String[] args) {
        // for(int i=9999;i>999;--i){
        //     zombieNum(i);
        // }
        Random rand=new Random(50);
        int [] a;
        a=new int[rand.nextInt(40)];
        for(int x:a){
            x=rand.nextInt(30);
            System.out.print(x+"   ");
        }
        System.out.println("\n\nThe Length of Array is: "+a.length);
    }
18   23   22   1   1   16   18   6   28   20   2   0   5   8   12   2   17   5   22   5   3   1   1   2   14   28   5   6   26   20   27   26   24   26   14   2   26   

The Length of Array is: 37
[Finished in 1.4s]

这个结果不论你运行多少次,都会是一样的,我明明用的是Random啊,Java你哄我啊呢???What Random?But, 关键是seed,以前在C++里面也有,现在不过是换了个花样!关键在此!!

        Random rand=new Random(50);

若是把上文中的50去掉,那么每一次运行的结果都不一样。如果换掉另一个数,那么再次运行也会产生一个始终一致的结果,假如我换为30,运行就会一直如下:

18   5   4   16   8   16   

The Length of Array is: 6
[Finished in 1.5s]

但是如果里面啥都不填写,保持默认值,那么运行一次就会有不同的结果:

19   

The Length of Array is: 1
[Finished in 1.5s]
22   13   10   4   13   2   18   28   

The Length of Array is: 8
[Finished in 1.5s]
17   1   22   1   2   25   23   12   27   16   23   24   19   29   12   6   23   

The Length of Array is: 17
[Finished in 1.5s]

这时候又发现一个奥妙,那么就是,随机出来的数受限于下列代码中的数字。

        a=new int[rand.nextInt(40)];

            x=rand.nextInt(30);

也即是不会 超过40/30 ,这就是界限!我们可以认为 的规定随机出来的数字的范围。

突发奇想,试验下蒙特卡洛的圆周率计算法了!!很简单的想法,一个边长为2的正方形内切一个半径为1 的圆,然后随机丢点,在圆内的点是总的点的pi/4这个大概的概率,所以只要算出这个比例,乘以4️⃣4就是pi了。代码如下:

    public static void main(String[] args) {
        // for(int i=9999;i>999;--i){
        //     zombieNum(i);
        // }
        Random rand=new Random();

        double [] a=new double[100000];
        double [] b=new double[100000];
        for(int i=0;i<100000;++i){
            a[i]=((double)rand.nextInt(1000))/1000;
            // System.out.print(a[i]+"   ");
        }
        Random rnd=new Random();
        for(int i=0;i<100000;++i){
            b[i]=((double)rnd.nextInt(1000))/1000;
            // System.out.print(b[i]+"   ");
        }
        // System.out.println(a[100]+" "+b[100]);
****        int count=0;
        for(int i=0;i<100000;++i){
            if((a[i]*a[i]+b[i]*b[i])<1){
                count++;
            }
        }

        System.out.println((double)count/100000*4);
        // System.out.println("\n\nThe Length of Array is: "+a.length);
    }

The Result:

3.14608
[Finished in 1.4s]

第二点:可变参数与自动包装机制

可变参数列表这是哪儿都逃不开的问题。当然,C++里面用容器我感觉挺好的,虽然感觉容器就是也该加强版的可变长数组以及自由特性的数组吧!

  1. 可变参数列表言简意赅! 看代码:
  2. 自动包装机制类似于多态,可以传入一个子类到形参为父类的地方

二者结合,很骚气!!比如用Object做可变长形参的!!骚操作啊!!!!废话不多说:看代码:


    public static void CV(Object... s){
        System.out.println("The length of the Object is :"+s.length);
        System.out.println("The kind of the Object is :"+s.getClass());
        for(Object x:s){
            System.out.println(x);
        }
    }

    public static void main(String[] args) {
        Random rand=new Random();
        int a=rand.nextInt(20);
        int b=rand.nextInt(20);
        int c=rand.nextInt(20);
        CV(a,b,c);
        String s1=new String("No .1");
        String s2=new String("No .2");
        String s3=new String("No .3");
        CV(s1,s2,s3);
    }

The Result:

The length of the Object is :3
The kind of the Object is :class [Ljava.lang.Object;
12
7
5
The length of the Object is :3
The kind of the Object is :class [Ljava.lang.Object;
No .1
No .2
No .3
[Finished in 1.6s]

Object 不知道我前面讲过没?所有类的头头,所有对象的父类或者往上肯定是他没错的!

不知道把零散的一些元素打包成数组算是打包机制不?我觉得四啊!!《Java编程思想》里面说把int 的零散元素打包成Integer算是,或者是把char打包到Character算是的。那就是啊吧!这些东西现在搞这么清白干嘛???啊?上天啊!!

第三点:枚举类型 enum

这个东西只说一点:与switch可以搭配使用,这就很骚了!!以前因为在有限范围内做switch老是会要变成1,2,3,4这些整形数,虽然简单了,但是确实看起来比较费劲啊。十年之后鬼还记得你这是啥?所以用枚举简直就是贴合人类的设计啊!至于那一丁点的损耗?那也叫事???


    public static enum Mei{
        Zhang,Yan
    }
    public static void main(String[] args) {
        Mei BG=Mei.Zhang; 
        switch (BG) {
            case Zhang:
                System.out.println("颜雨薇,我好想你啊!");
                break;
            case Yan:
                System.out.println("张照博,我好喜欢你的!");
                break;
       }
    }

简单点说的话就是可以作为一个集合,你可以看做是1-10这种组合,当然如果要使用。不需要new,只要定义下。比如说Mei作为一个枚举集合,如果要使用其中的Zhang,那么就只要

Mei x=Mei.Zhang

就ok的。跟基本数据类型很相似啊!!!

第四点:好神奇的Static啊!!

今天看到了《Java编程思想》的第6章,访问控制,发现了一个很神奇,虽然我早知道了但是还没有吃透的东西--Static,具体的请看下面的代码:

import java.util.*;

class Xa{
    public  static  int TY(){
        System.out.println("Can I have a try?");
        return 0;
    }
}
    public static enum Mei{
        Zhang,Yan
    }
    public static void main(String[] args) {
        Random rand=new Random();
        Mei BG=Mei.Zhang;
        switch (BG) {
            case Zhang:
                System.out.println("颜雨薇,我好想你啊!");
                break;
            case Yan:
                System.out.println("张照博,我好喜欢你的!");
                break;
        }
        int s=Xa.TY();
        System.out.println(s);
     }
}

The Result:

/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=52223:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/zhangzhaobo/IdeaProjects/Java_Program_Spirit/out/production/Java_Program_Spirit Java_Program_Spirit
颜雨薇,我好想你啊!
Can I have a try?
0

Process finished with exit code 0

可以看到,静态的方法根本不需要定义一个对象,它的存在只与自身有关,也就是说定义了它,那么要用到它的第一时间它就会实例化!所以这其实就跟C++里面直接在最外层定义一个函数是一个道理,要用到就直接调用即可,当然Java是面向对象编程的,所以前头还得加个类名才可以!!

第五点:访问权限控制

default就是没有控制符的意思,private用于类“助手”的成员。protected用于一些想要在包内继承的地方,而对于包外就没那么友好了。至于public最为开放也最受人们喜爱。default,既可以偷懒,而且防范的也挺不错的。老实说对于这些访问控制的理解我还不够,现在也不需要那么深刻。以后再说吧!

第六点:JDBC,Java结合数据库Mysql操作,可以做好东西啦!!

具体的配置参考下面几篇文章你就会了,记得按照我的顺序来:

1. Mac 如何配置JDBC的教程,Windows类似intelliJ IDEA for mac with JDBC

2. 下载JDBC的替代,国内版

3. 配置好之后的使用手段

看我的代码:

import java.sql.*;

public class JDBC_Test {

    public static void main(String[] args){

        String driver = "com.mysql.jdbc.Driver";

        String url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=GBK";

        String user = "root";

        String password = "root";

        try {
            Class.forName(driver);

            Connection conn = DriverManager.getConnection(url, user, password);

            if(!conn.isClosed())
                System.out.println("Succeeded connecting to the Database!");

            Statement statement = conn.createStatement();

            String sql = "select * from tcount_tbl";
            String insert = "insert into tcount_tbl(runoob_author,runoob_count) values(\"张照博\",50)";
//            String delete = "delete from tcount_tbl where runoob_author = \"RUNOOB.COM\" ";
            boolean rs1 = statement.execute(insert);
//            boolean rs1 = statement.execute(delete);
            ResultSet rs = statement.executeQuery(sql);
            System.out.println("-----------------");
            System.out.println("执行结果:");
            System.out.println("-----------------");
            System.out.println(" Name" + "\t\t" + " ID");
            System.out.println("-----------------");

            String name = null;

            while(rs.next()) {

                name = rs.getString("runoob_author");
                name = new String(name.getBytes("UTF-8"),"UTF-8");
                System.out.println(name+ "\t\t" + rs.getString("runoob_count")  );
            }
            rs.close();
            conn.close();
        } catch(ClassNotFoundException e) {
            System.out.println("Sorry,can`t find the Driver!");
            e.printStackTrace();
        } catch(SQLException e) {
            e.printStackTrace();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

我的代码与上面第三个教程略有不同,主要集中于对编码方式的设置。

        String url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=GBK";
                name = new String(name.getBytes("UTF-8"),"UTF-8");

具体来说是上面两句,大家伙自己体会其中的差别哦,我先溜了!!效果如下:

/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=57104:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Users/zhangzhaobo/IdeaProjects/JDBC/out/production/JDBC:/Applications/IntelliJ IDEA.app/Contents/lib/mysql-connector-java-5.1.44-bin.jar" JDBC_Test
Sun Jan 28 23:58:57 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Succeeded connecting to the Database!
-----------------
执行结果:
-----------------
 Name        ID
-----------------
菜鸟教程        10
Google      22
今日成神        11
张照博     50

Process finished with exit code 0
正文之后

今天到此为止,真的沉迷小说不可自拔了。今天看了三四个小时了!!烦躁!另外,今天主要是冒雪出去买了零食,还有就是揭开井盖然后把水泵调整了位置。解决了家中饮水问题!!我觉得自己还是很6的,有图为证:

揭开井盖的我,心中MMP,野外的老鼠跑起来飞快的,不像我室友那种残疾家养的

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消