java-10:常用api

引用类型的使用步骤:

  1:导包:import 包名.类名称

  如果需要使用的类和当前类同一个包下,就不需要导包,

  java.lang包下的内容不需要导包

  2:创建对象:类名称 对象名 = new 类名称()

  3:使用:对象名.成员方法名()

 

Random类:

//使用random类
//random类的重载方法nextInt(int n)中参数n表示左闭右开区间[0 n)
public class demo01 {
    public static void main(String[] args) {
        //创建random类的对象
        Random ran=new Random();
        //使用num1获取随机数
        int num1=ran.nextInt();
        System.out.println(num1);

        for (int i = 0; i < 10; i++) {
            int num2=ran.nextInt(9);
            System.out.print(num2+"\t");
        }
    }
}
//猜数字小游戏,数字范围1~20以内
        public class demo02 {
            public static void main(String[] args) {
                Random r=new Random();
                Scanner n=new Scanner(System.in);

                System.out.println("输入1~20以内的一个数字:");
                int result=r.nextInt(20)+1;
                while(true){
                    int num=n.nextInt();
                    if(num<result){
                        System.out.println("值小了");
                        continue;
                    }else if (num>result){
                        System.out.println("值大了");
                        continue;
                    }else{
                        System.out.println(num+":你猜对了");
                        break;
                    }
                }
            }
}

ArrayList类
//ArrayList中常用的方法
public class dmeo02 {
    public static void main(String[] args) {
        //创建String类型的ArrayList对象
        ArrayList<String> list=new ArrayList<>();

        //调用add()方法,向list中添加元素,有一个boolean类型的返回值,可以不用
        list.add("迪丽热巴");
        list.add("古力娜扎");
        list.add("马尔扎哈");
        System.out.println(list);
        //使用for循环遍历数组集合
        System.out.println("遍历数组集合");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        //调用get()方法,获取list中的元素,返回值类型为E的返回值类型
        String s1=list.get(2);
        System.out.println("3号元素是:"+s1);

        //调用rmove()方法,删除list中指定的元素,返回一个E类型的返回值
        String whorm=list.remove(2);
        System.out.println("谁被删除了:"+whorm);

        //调用size方法,获取数组列表的长度,返回一个int类型的返回值
        int num=list.size();
        System.out.println("list的长度为:"+num);
    }
}
String类:
字符串的特点:
  1:字符串一但被创建,便永不可改变,
  2:所以字符串是可以共享的
  3:字符串相当于char型字符数组
创建字符串的3+1种方法:
  public String():创建一个空白的字符串
  public String(char[] array):根据字符数组的内容,来创建相应的字符串
  public String(byte[] array):根据字节数组的内容,来创建相应的字符串
//使用3+1种方法创建字符串
public class demo01 {
    public static void main(String[] args) {
        //使用public String()构造方法创建字符串
        String str=new String();
        System.out.println("第一个字符串:"+str);

        //使用public String(char[] array)()构造方法创建
        char[] ch={'A','B','C'};
        String str1=new String(ch);
        System.out.println("第二个字符串:"+str1);

        //使用public String(byte[] array)()构造方法创建
        byte[] by={97,98,99};
        String str2=new String(by);
        System.out.println("第三个字符串:"+str2);
    }
}

字符串常量池:程序当中直接写上的双引号字符串,就在字符串常池当中

对于基本类型:==就是数值的比较

对于引用类型:==就是地址值的比较

 

 

posted @ 2019-09-23 16:53  BatmanY  阅读(186)  评论(0编辑  收藏  举报