基础02Random类、ArrayList类

Random类

Random类用来生成随机数字。使用起来也是三个步骤:

  1. 导包
    import java.util.Random;

  2. 创建
    Random r = new Random(); // 小括号当中留空即可

  3. 使用
    获取一个随机的int数字(范围是int所有范围,有正负两种):int num = r.nextInt()
    获取一个随机的int数字(参数代表了范围,左闭右开区间):int num = r.nextInt(3)
    实际上代表的含义是:[0,3),也就是0~2

package apiRandom;

import java.util.Random;
import java.util.Scanner;

public class demo01Random {
    public static void main(String[] args) {
        Random ra = new Random();
        int num = ra.nextInt();//nextInt方法产生随机数

        System.out.println("随机生成数:"+num);
     //生成了一百个0-9之间的随机数
        Random r = new Random();//只用来创建对象。 r.nextInt()调用

        for (int i = 0; i < 100; i++) {
            int num01 = r.nextInt(10); // 范围实际上是0~9
            System.out.println(num01);
        }

 /*
题目要求:
根据int变量n的值,来获取随机数字,范围是[1,n],可以取到1也可以取到n。

思路:
1. 定义一个int变量n,随意赋值
2. 要使用Random:三个步骤,导包、创建、使用
3. 如果写10,那么就是0~9,然而想要的是1~10,可以发现:整体+1即可。
4. 打印随机数字
 */
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        //开始上面目标
        Random ra02 = new Random();
        int a = ra02.nextInt(n)+1;
        System.out.println("随机数:"+a);
		
		//重点在random的取值范围。
    }

}

ArrayList类

定义一个数组,用来存储3个Person对象。

public static void main(String[] args) {
        // 首先创建一个长度为3的数组,里面用来存放Person类型的对象
        Person[] array = new Person[3];

        Person one = new Person("迪丽热巴", 18);
        Person two = new Person("古力娜扎", 28);
        Person three = new Person("玛尔扎哈", 38);

        // 将one当中的地址值赋值到数组的0号元素位置
        array[0] = one;
        array[1] = two;
        array[2] = three;

        System.out.println(array[0]); // 地址值
        System.out.println(array[1]); // 地址值
        System.out.println(array[2]); // 地址值

        System.out.println(array[1].getName()); // 古力娜扎
    }
    数组有一个缺点:一旦创建,程序运行期间长度不可以发生改变。

数组的长度不可以发生改变。
但是ArrayList集合的长度是可以随意变化的。

ArrayList<String> list = new ArrayList<>();

对于ArrayList来说,有一个尖括号代表泛型。
泛型:也就是装在集合当中的所有元素,全都是统一的什么类型
注意泛型只能是引用类型,不能是基本类型。

注意事项:
对于ArrayList集合来说,直接打印得到的不是地址值,而是内容。
如果内容是空,得到的是空的中括号:[]

// 创建了一个ArrayList集合,集合的名称是list,里面装的全都是String字符串类型的数据
 // 备注:从JDK 1.7+开始,右侧的尖括号内部可以不写内容,但是<>本身还是要写的。
     ArrayList<String> list = new ArrayList<>();
System.out,println(list);

//输出  []

 // 向集合当中添加一些数据,需要用到add方法。
 list.add("赵丽颖");
 System.out.println(list); // [赵丽颖]

 list.add("迪丽热巴");
 list.add("古力娜扎");
 list.add("玛尔扎哈");
 System.out.println(list); // [赵丽颖, 迪丽热巴, 古力娜扎, 玛尔扎哈]

//        list.add(100); // 错误写法!因为创建的时候尖括号泛型已经说了是字符串,添加进去的元素就必须都是字符串才行

ArrayList当中的常用方法

ArrayList 大小可变的数组
向集合添加元素时,用到add方法

ArrayList当中的常用方法

public boolean add(E e):向集合当中添加元素,参数的类型和泛型一致。返回值代表添加是否成功。
备注:对于ArrayList集合来说,add添加动作一定是成功的,所以返回值可用可不用。
但是对于其他集合(今后学习)来说,add添加动作不一定成功。

public E get(int index):从集合当中获取元素,参数是索引编号,返回值就是对应位置的元素。

public E remove(int index):从集合当中删除元素,参数是索引编号,返回值就是被删除掉的元素。

public int size():获取集合的尺寸长度,返回值是集合中包含的元素个数。

在这里插入图片描述

  public static void main(String[] args) {
      ArrayList<String> list = new ArrayList<>();
      System.out.println(list); // []

      // 向集合中添加元素:add
      boolean success = list.add("柳岩");
      System.out.println(list); // [柳岩]
      System.out.println("添加的动作是否成功:" + success); // true

      list.add("高圆圆");
      list.add("赵又廷");
      list.add("李小璐");
      list.add("贾乃亮");
      System.out.println(list); // [柳岩, 高圆圆, 赵又廷, 李小璐, 贾乃亮]

      // 从集合中获取元素:get。索引值从0开始
      String name = list.get(2);
      System.out.println("第2号索引位置:" + name); // 赵又廷

      // 从集合中删除元素:remove。索引值从0开始。
      String whoRemoved = list.remove(3);
      System.out.println("被删除的人是:" + whoRemoved); // 李小璐
      System.out.println(list); // [柳岩, 高圆圆, 赵又廷, 贾乃亮]

      // 获取集合的长度尺寸,也就是其中元素的个数
      int size = list.size();
      System.out.println("集合的长度是:" + size);
  }

小结:删除元素时,remove的索引值一定是从0号开始

泛型只能是引用类型,不能是基本类型,集合里面保存的都是地址值,基本类型没有地址值
解决:
如果希望向集合ArrayList当中存储基本类型数据,必须使用基本类型对应的“包装类”。
包装类都是引用类型
基本类型 包装类(引用类型,包装类都位于java.lang包下)

byteByte
shortShort
intInteger 【特殊】
longLong
floatFloat
doubleDouble
charCharacter 【特殊】
booleanBoolean

从JDK 1.5+开始,支持自动装箱、自动拆箱。

自动装箱:基本类型 --> 包装类型
自动拆箱:包装类型 --> 基本类型

    public static void main(String[] args) {
        ArrayList<String> listA = new ArrayList<>();
        // 错误写法!泛型只能是引用类型,不能是基本类型
//        ArrayList<int> listB = new ArrayList<>();

        ArrayList<Integer> listC = new ArrayList<>();
        listC.add(100);
        listC.add(200);
        System.out.println(listC); // [100, 200]

        int num = listC.get(1);
        System.out.println("第1号元素是:" + num);
    }

练习:

//生成6个1~33之间的随机整数,添加到集合,并遍历集合。public static void main(String[] args) {
   思路:
1. 需要存储6个数字,创建一个集合,<Integer>
2. 产生随机数,需要用到Random
3. 用循环6次,来产生6个随机数字:for循环
4. 循环内调用r.nextInt(int n),参数是33,0~32,整体+1才是1~33
5. 把数字添加到集合中:add
6. 遍历集合:for、size、get

     Random ra = new Random();
    ArrayList<Integer> list = new ArrayList<>();

    for(int i = 0;i<6;i++){
        int a = ra.nextInt(33)+1;
        list.add(a);
    }

    for(int j = 0; j<list.size();j++){
        System.out.println(list.get(j));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值