Java 几个基础类

   Scanner类     Random类        ArrayList 集合类    String 类     String的方法   

   equal() 类      static类             静态代码块             Arrays 类        Math类

Scanner类

     创建一个Scanner对象     Scanne sc =new Scanner(system.in);   这里的system.in指的是 从键盘输入

     获取从键盘输入的值       String str =sc.next( )                   获取一个整数    int  num =sc.nextInt( )

     使用匿名对象创建           int  num  =  new Scanner(system.in).nextInt( )

Random类

   创建Random 对象   Random  r  = new  Random( )           获取一个随机 整数   int  num  = r.nextInt( )    范围是int 数据类型的范围

     获取指定范围内的 整数    int num = r.nextInt( 5)                       获取一个[0,5) 范围内的整数采用左闭右开的原则 

        获取一个 [1,5] 范围内的整数       int num = r.nextInt(5) +1      获取[1,5] 范围内的整数

ArrayList<String> 集合类

   集合比数组更灵活好用, 可以改变长度,内容    但是只能存储对象

  创建一个ArrayList <String>  list  =  new Array<>();         从jdk1.7+ 右边<> 内不用写 左边对应的String 

  <>内指泛型    泛型只能使用引用型数据类型  如果想要使用基本数据类型则需要 包装类型

 byte   --> Byte                    short -->  Short                   long -->Long         float -->Float     

 double -->Double               boolean-->Boolean             int --> Integer            char -->Character        

    ArrayList < Integer  >  list  =  new Array<>();   定义基本数据类型时

  以上为对基本数据类型的包装,就是构成一个引用型对象, 从jdk 1.5+ 开始  支持 自动装箱 拆箱 

 1.对ArrayList<String> list   来说当直接打印 list 时, 得到的是一个[ ]  因为存放的不再是地址,而是内容,根据的底层的toSttring方法

 2.集合的一些方法

      1.添加数据    public boolean add( E,e)   可见是有返回值的      集合添加返回值必须为  true 

          必须和 定义的 泛型数据一致     list.add( 'wyc' )     list.add( 5 ) 可以直接写整数       底层自动装箱 拆箱原因

      2.删除数据    public  E remove(int index)      参数是索引      返回值是被删除的数据 

      3.查找数据    public  E get(int index)     参数是索引  返回值是 查找的数据

      4.长度           public  int size()              返回值是集合中的元素个数 

 3.集合也可以存放 new出来的对象   存放的还是对象的地址值

 4.集合也可以当作参数和返回值     实质上还是内存地址值

public static void main(String[] args) {
        ArrayList<String> list =new ArrayList<>();
        ArrayList m=r(list);
        System.out.println(m);
    }
    public static ArrayList r(ArrayList list){
        System.out.println(list);
        ArrayList<Integer> listB =new ArrayList<>();
        listB.add(15);
        return listB;
    }

  String 类

     string 对象创建的四种方法: 

        public String( );   创建一个空的字符串,不含有任何内容

        public String( char[ ] array );  通过字符数组创建    在底层 字符串就是通过字符 数组连接而成 

        public String( byte[ ]  array );    通过字节 数组创建    计算机中所有的数据都是二进制,一个英文字符又由1个字节  8位构成

public static void main(String[] args) {
        String str = new String();

        char[] chararray = {'A', 'B', 'C'};
        String str2 = new String(chararray);

        byte[] bytearray = {97, 98, 99};
        String str3 = new String(bytearray);
        System.out.println(str+str2+str3);
    }

      String str = 'wyc'    直接创建 字符串对象             只要写了 '" "  就是一个字符串对象

    对于引用类型数据来说 ==  是比较 内存地址  基本数据类型比较的是 值

     而对于 String   来说  直接 " "  出来的字符串对象  是存在堆当中的字符串 常量池中   

       new String(" a") 先去常量池找如果有, 就不在常量池创建了 ,但是new出来的都是 在堆内存中存放一个地址

    

       String str = "wyc" ;     str = "wydasd';         强调:   常量(字符串)  是不会改变的  只是str 存的 内存地址变化了

    String的方法

            String 运算   比较  'A' < 56:     由于 这是二元运算  所以 范围小的 char 类型 自动提升 为 int类型 

           1.int num = "dasjkafada".length();       获取字符串的长度    返回值 int 

           2.String str1 = "dada"; String str2 = "jgh";           String str3 = str1.concat(str2);            拼接字符串 生一个全新的字符串

           3. char ch = str1.charAt(3);                获取指定位置的 单个字符   超出索引报错

           4.  int num2 = str1.indexOf("hd");       获取字符或字符串首次出现的索引  返回值int

           5.String str4 =str1.substring(5);          获取str1 索引5以后所有的 字符串

           6.String str4 =str1.substring(5,8);       获取str1 索引 5 到 7的  字符串   [5,8 )

           7.char[ ] chars ="dsdasda".toCharArray();      将字符串转换为 字符数组

           8. byte[] bytes = "dsdasda".getBytes();           将字符串转换为 字节数组  也就是英文的Ascii码

           9.String [] strs = "aaa,bbb,ccc".split(",")           将 字符串按照正则规则切割 并存放在一个数组中     注意"."      代表所有     

           10. String str = "dsdadad".replace( "a", "*")       将将字符串中的a替换为*

           11.new String( byte[ ] )    将一个字节数组 转换为字符串        12.new String( char[ ] )     将一个字符数组 转换为字符串

    equal() 类

           equal   比较的是数据的值 ,严格区分大小写   ,有多个重载方法 

         String str = "wyd  " ;    "wyc".equal(str)    返回值boolean类型         不推荐  str .equal("wyc ")  因为str 有可能为null 运行报错

           "wyc".equalsIgnoreCase("wyC");   不区分大小写

          objects.equals 可以解决空指针问题  原理:   列如 a.equals(b)  先判断  a!=null && a.equals(b)  否则False

         用法  objects.equals(a,b)

static类

  对于static特殊的是  被static修饰的 成员变量 或 方法   会被存放在 方法区的静态区中 为一个类的所有对象所共享

  对于使用了static 关键字的成员变量   这样的变量 将不再属于 对象自己.而是属于类的.所以 凡是本类的对象,共享一个static变量

  列如   Student stu1 = new Student();      Student stu2 = new Student();    静态方法或变量  推荐使用类名调用

public class DemoStudent {
    static  String room ;
    private String name;
}
  public static void main(String[] args) {              
        DemoStudent stu1 = new DemoStudent("wyc");
        stu1.room="101";
        DemoStudent stu2 = new DemoStudent("wyc2");
        System.out.println("stu1的教室是: "+stu1.room);
        System.out.println("stu2的教室是: "+stu2.getRoom());  都是101
    }

  静态代码块

    特点:到第一次用到本类时,静态代码执行唯一的一次, 可以有多个,  且总是优于非静态方法比构造方法先执行
      用途: 用来一次性的对静态成员变量赋值

public class DemoStudent {                     
    static {
        System.out.println("静态代码块执行了");   
    }      
}

  数组

     int[] array1 = {1, 3, 2};            String[] arry2 = {"a", "c", "g", "b"};          

            String str1 = Arrays.toString(array1);         将一个数组按照默认格式 转换成字符串形式 [1, 3, 2]

            Arrays.sort(array1);                                    对一个int数组进行升序排序,注意没有返回值,直接改变原来数组

            Arrays.sort(arry2);                                      对一个字符串 数组进行升序排序,注意没有返回值直接改变原来数组

             Arrays.stream(array [] );                             将数组转换一个流

 Math类

      1.Math.abs( -3.4 )                                             取绝对值返回值3.4

      2.Math.ceil( 3.1)                                                向上取整 返回值4

      3.Math.floor( 3.9)                                                向下取整 返回值3

      4.Math.round( 3.6 )                                           四舍五入 返回值 4

         int( 3.6)                                                          舍弃小数位

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值