第一章 Scanner类
Scanner类的功能:
可以实现键盘输入数据,到程序当中。
备注:
System.in 系统输入指的是通过键盘录入数据。
引用类型的一般使用步骤:
- 导包
import 包路径.类名称;
如果需要使用的目标类,和当前类位于同一个包下,则可以省略导包语句不写。
只有java.lang包下的内容不需要导包,其他的包都需要import语句。 - 创建
类名称 对象名 = new 类名称(); - 使用
对象名.成员方法名()
获取键盘输入的一个int数字:int num = sc.nextInt();
获取键盘输入的一个字符串:String str = sc.next();
public static void main(String[] args) {
// 2. 创建
// 备注:System.in代表从键盘进行输入
Scanner sc = new Scanner(System.in);
// 3. 获取键盘输入的int数字
int num = sc.nextInt();
System.out.println("输入的int数字是:" + num);
// 4. 获取键盘输入的字符串
String str = sc.next();
System.out.println("输入的字符串是:" + str);
匿名对象【了解】
创建对象的标准格式:
类名称 对象名 = new 类名称();
匿名对象就是只有右边的对象,没有左边的名字和赋值运算符。
new 类名称();
注意事项:匿名对象只能使用唯一的一次,下次再用不得不再创建一个新对象。
使用建议:如果确定有一个对象只需要使用唯一的一次,就可以用匿名对象。
public static void main(String[] args) {
// 左边的one就是对象的名字
Person one = new Person();
one.name = "高圆圆";
one.showName(); // 我叫高圆圆
System.out.println("===============");
// 匿名对象
new Person().name = "赵又廷";
new Person().showName(); // 我叫:null
}
1.普通的使用方式
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
2.匿名对象的方式
int num = new Scanner(System.in).nextInt();
System.out.println("输入的是:" + num);
3.使用一般的写法传入参数
Scanner sc = new Scanner(System.in);
methodParam(sc);
4.使用匿名对象来传入参数
methodParam(new Scanner(System.in));
public static void methodParam(Scanner sc) {
int num = sc.nextInt();
System.out.println("输入的是:" + num);
}
5.使用匿名对象作为方法的返回值
Scanner sc = methodReturn();
int num = sc.nextInt();
System.out.println("输入的是:" + num);
public static Scanner methodReturn() {
Scanner sc = new Scanner(System.in);
return sc;
return new Scanner(System.in);
}
第二章:random类
Random类用来生成随机数字。使用起来也是三个步骤:
使用方法
- 导包
import java.util.Random; - 创建
Random r = new Random(); // 小括号当中留空即可 - 使用
获取一个随机的int数字(范围是int所有范围,有正负两种):int num = r.nextInt()
获取一个随机的int数字(参数代表了范围,左闭右开区间):int num = r.nextInt(3)
实际上代表的含义是:[0,3),也就是0~2
第三章:ArrayList类
数组有一个缺点:一旦创建,程序运行期间长度不可以发生改变。
所以使用集合:ArrayList
使用数组储存对象
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来说,有一个尖括号代表泛型。
泛型:也就是装在集合当中的所有元素,全都是统一的什么类型。
注意:泛型只能是引用类型,不能是基本类型。
注意事项:
对于ArrayList集合来说,直接打印得到的不是地址值,而是内容。
如果内容是空,得到的是空的中括号:[]
实例:
public static void main(String[] args) {
// 创建了一个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当中的常用方法有:
-
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);
}
集合的遍历
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("迪丽热巴");
list.add("古力娜扎");
list.add("玛尔扎哈");
// 遍历集合
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
如果希望向集合ArrayList当中存储基本类型数据,必须使用基本类型对应的“包装类”。
基本类型 包装类(引用类型,包装类都位于java.lang包下)
byte Byte
short Short
int Integer 【特殊】
long Long
float Float
double Double
char Character 【特殊】
boolean Boolean
从JDK 1.5+开始,支持自动装箱、自动拆箱。
自动装箱:基本类型 --> 包装类型
自动拆箱:包装类型 --> 基本类型
public class Demo05ArrayListBasic {
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);
}