1、API
- API(Application Programming Interface),应用程序编程接口。Java API是一本程序员的
字典,是JDK中提供给我们使用的类的文档说明书。(就是把底层的代码实现封装起来实现某功能提供给我们使用) - API使用步骤(能够明确API的使用步骤)
- 打开帮助文档。
- 点击显示,找到索引,看到输入框。
- 你要找谁?在输入框里输入,然后回车。
- 看包。java.lang下的类不需要导包,其他需要。
- 看类的解释和说明。
- 学习构造方法。
- 使用成员方法。
2、Scanner扫描器类
2.1 Scanner类
- 一个可以解析基本数据类型和字符串的简单文本扫描器。
例如,以下代码使用户能够从 System.in (系统输入,即键盘录入)中读取一个数:
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
备注:System.in 系统输入,指的是通过键盘录入数据。
2.2 引用类型使用步骤
- 导包:使用import关键字导包,在类的所有代码之前导包,引入要使用的类型,java.lang包下的所有类无需导入,举例:
import 包名.类名;
java.util.Scanner;
2.3 创建对象
- 使用该类的构造方法,创建一个该类的对象。
数据类型 变量名 = new 数据类型(参数列表);
Scanner sc = new Scanner(System.in);
2.4 调用方法
调用该类的成员方法,完成指定功能。
变量名.方法名();
int i = sc.nextInt(); // 接收一个键盘录入的整数
2.5 Scanner使用步骤
-
查看类:
java.util.Scanner:该类需要import导入后使用。 -
查看构造方法:
public Scanner(InputStream source): 构造一个新的Scanner,它生成的值是从指定的输入流扫描的。 -
查看成员方法:
public int nextInt():将输入信息的下一个标记扫描为一个int值。
//1. 导包
import java.util.Scanner;
public class Demo01_Scanner {
public static void main(String[] args) {
//2. 创建键盘录入数据的对象
Scanner sc = new Scanner(System.in);
//3. 接收数据
System.out.println("请录入一个整数:");
int i = sc.nextInt();
//4. 输出数据
System.out.println("i:"+i);
}
}
2.6 匿名对象(了解)
-
概念: 匿名对象就是以前创建对象时去掉赋值号左边的那一坨即没有名字的对象,只适合调用(使用)一次方法的时候使用,也可以作为实参和返回值
-
创建对象时,只有创建对象的语句,却没有把对象地址值赋值给某个变量。虽然是创建对象的简化写法,但是应用场景非常有限
// Scanner sc = new Scanner(System.in);//有名对象,对象名是sc
- 匿名对象 :没有变量名(名字)的对象,如new Scanner(System.in);
new 类名(参数列表);
new Scanner(System.in);
- 应用场景
- 创建匿名对象直接调用方法,没有变量名
new Scanner(System.in).nextInt();
- 一旦调用两次方法,就是创建了两个对象,造成浪费
new Scanner(System.in).nextInt();//new,新建的意思,每new一次就创建新的对象,开辟新的内存空间,浪费内存
new Scanner(System.in).nextInt();
小贴士:一个匿名对象,只能使用一次。
- 匿名对象可以作为方法的参数和返回值
- 作为参数:
class Test {
public static void main(String[] args) {
// 普通方式
Scanner sc = new Scanner(System.in);
input(sc);
//匿名对象作为方法接收的参数
input(new Scanner(System.in));
}
public static void input(Scanner sc){//sc = new Scanner(System.in);
System.out.println(sc);
}
}
- 作为返回值
class Test2 {
public static void main(String[] args) {
// 普通方式
Scanner sc = getScanner();
}
public static Scanner getScanner(){
//普通方式
//Scanner sc = new Scanner(System.in);
//return sc;
//匿名对象作为方法返回值
return new Scanner(System.in);
}
}
3、Random随机数类
3.1 随机数类
Random r = new Random();
int i = r.nextInt();//产生一个随机整数,有负数等
3.2 Random使用步骤
-
查看类:
java.util.Random:该类需要 import导入使后使用。 -
查看构造方法:
public Random():创建一个新的随机数生成器。 -
查看成员方法:
public int nextInt(int n):返回一个伪随机数,范围在0(包括)和指定值 n(不包括n)之间的int值。
3.3 练习
- 猜数字小游戏: 游戏开始时,会随机生成一个1-100之间的整数
number。 - 玩家猜测一个数字
guessNumber,会与number作比较,系统提示大了或者小了,直到玩家猜中,游戏结束。
// 导包
import java.util.Random;
public class Test02Random {
public static void main(String[] args) {
// 系统产生一个随机数1-100之间的。
Random r = new Random();
int number = r.nextInt(100) + 1;//1-100
while(true){
// 键盘录入我们要猜的数据
Scanner sc = new Scanner(System.in);//这句代码,放循环外面就不用不断创建对象更省内存
System.out.println("请输入你要猜的数字(1-100):");
int guessNumber = sc.nextInt();
// 比较这两个数据(用if语句)
if (guessNumber > number) {
System.out.println("你猜的数据" + guessNumber + "大了");
} else if (guessNumber < number) {
System.out.println("你猜的数据" + guessNumber + "小了");
} else {
System.out.println("恭喜你,猜中了");
break;
}
}
}
}
4、ArrayList集合类
4.1 引入——数组存对象
- 使用学生数组,存储三个学生对象,代码如下:(能够使用数组存储自定义类型并遍历)
class Student {
private String name;
private int age;
public Student() {//构造方法,构造对象,创建对象的时候调用使用,new
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {//getter
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {//setter
this.age = age;
}
}
public class Test01StudentArray {
public static void main(String[] args) {
//创建学生数组
Student[] students = new Student[3];//Student类,类型,引用数据类型都是null3固定死了
//创建学生对象
Student s1 = new Student("曹操",40);
Student s2 = new Student("刘备",35);
Student s3 = new Student("孙权",30);
//把学生对象作为元素赋值给学生数组
students[0] = s1;
students[1] = s2;
students[2] = s3;
//遍历学生数组,能够使用数组存储自定义类型并遍历
for(int x=0; x<students.length; x++) {//x索引
Student s = students[x];//s = new Student("曹操",40);
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
到目前为止,我们想存储对象数据,选择的容器,只有对象数组。而数组的长度是固定的,无法适应数据变化的需求。
为了解决这个问题,Java提供了另一个容器java.util.ArrayList 集合类,让我们可以更便捷的存储和操作对象数据。
4.2 ArrayList类
-
java.util.ArrayList底层是大小可变的数组的实现,存储在内的数据称为元素。 -
此类提供一些方法来操作内部存储的元素。
ArrayList中可不断添加元素,其大小也自动增长,所以是个可以自动增长的容器,叫集合,相当于气球
4.3 ArrayList使用步骤
查看类
java.util.ArrayList <E>:该类需要 import导入使后使用。
<E> ,表示一种指定的数据类型,叫做泛型(就业班学)。E ,取自Element(元素)的首字母。在出现E 的地方,我们使用一种引用数据类型将其替换即可,表示我们将存储哪种引用类型的元素。代码如下:
ArrayList<String>,ArrayList<Student>
查看构造方法
public ArrayList():构造一个内容为空的集合,容器
基本格式:能够使用ArrayList集合的构造方法创建ArrayList集合对象
ArrayList<String> list = new ArrayList<String>();
在JDK 7后,右侧泛型的尖括号之内可以留空,这叫可推导可省略原则(后面就业班学习lambda表达式也有这个原则)简化格式:能够使用ArrayList集合的构造方法创建ArrayList集合对象
ArrayList<String> list = new ArrayList<>();
查看成员方法
-
public boolean add(E e): 将指定的元素添加到此集合的尾部。(能够使用ArrayList集合存储数据)参数
E e,在构造ArrayList对象时,<E>指定了什么数据类型,那么add(E e)方法中,只能添加什么数据类型的对象。
使用ArrayList类,存储三个字符串元素,代码如下:
public class Test02StudentArrayList {
public static void main(String[] args) {
//创建集合对象调用add方法添加元素
ArrayList<String> list = new ArrayList<>();
System.out.println(list);//[]
//添加元素,能够使用ArrayList集合存储数据
String s1 = "曹操";
String s2 = "刘备";
String s3 = "孙权";
list.add(s1);//能够使用ArrayList集合存储数据
list.add(s2);
list.add(s3);
//打印集合,即打印集合对象名,默认会调用toString()方法,输出指定格式比如带有[],多个元素以逗号空格隔开
System.out.println(list);//[曹操, 刘备, 孙权]
}
}
4.4 常用方法和遍历(能够使用ArrayList集合中常用的方法)
对于元素的操作,基本体现在——增、删、改、查。常用的方法有:
-
public boolean add(E e):将指定的元素添加到此集合的尾部//增 -
public E remove(int index):移除此集合中指定位置上的元素。返回被删除的元素//删 -
public E set(int index, E element):设置集合指定位置的元素为后面传入的元素,返回值被替换元素//改 -
public E get(int index):返回此集合中指定位置上的元素。返回获取的元素//查 -
public int size():返回此集合中的元素个数。遍历集合时,可以控制索引范围,防止越界//查
这些都是最基本的方法,操作非常简单,代码如下:
public class Demo01ArrayListMethod {
public static void main(String[] args) {
//创建集合对象
ArrayList<String> list = new ArrayList<String>();
//添加元素
list.add("hello");
list.add("world");
list.add("java");
//public E get(int index):返回指定索引处的元素
System.out.println(list.get(0));//hello
System.out.println(list.get(1));//world
System.out.println(list.get(2));//java
//public int size():返回集合中的元素的个数
System.out.println(list.size());//3
//public E remove(int index):删除指定索引处的元素,返回被删除的元素
System.out.println(list.remove(0));//hello
//public E set(int index, E element):设置集合指定位置的元素为后面传入的元素
list.set(0,"666");
//遍历输出,得到集合里面的每一个元素,能够使用ArrayList集合存储字符串并遍历
for(int i = 0; i < list.size(); i++){
System.out.println(list.get(i));//666换行输出java
}
}
}
4.5 如何存储基本数据类型
ArrayList对象不能存储基本类型,只能存储引用类型的数据。类似**<int>不能写**,但是存储基本数据类型对应的包装类(引用数据类型)型是可以的。所以,想要存储基本类型数据,<>中的数据类型,必须转换后才能编写,转换写法如下:
| 基本类型 | 基本类型对应的包装类(引用数据类型) |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| double | Double |
| float | Float |
| char | Character |
| boolean | Boolean |
我们发现,只有Integer和Character需要特殊记忆,其他基本类型只是首字母大写即可。那么存储基本类型数据,代码如下:
public class Demo02ArrayListMethod {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();//<不能写int>
list.add(1);//int和Integer可以互相转换
list.add(2);//存的都是对象
list.add(3);
list.add(4);
System.out.println(list);//[1, 2, 3, 4]
}
}
4.6 ArrayList练习
数值添加到集合
生成6个1~33之间的随机整数,添加到集合,并遍历
public class Test01ArrayList {
public static void main(String[] args) {
// 创建ArrayList 对象
ArrayList<Integer> list = new ArrayList<>();
// 添加随机数到集合
// 创建Random 对象
Random random = new Random();
for (int i = 0; i < 6; i++) {
int r = random.nextInt(33) + 1;
list.add(r);
}
// 遍历集合输出,list.for
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
对象添加到集合(能够使用ArrayList集合存储自定义对象并遍历)
自定义4个学生对象,添加到集合,并遍历
public class Test02ArrayList {
public static void main(String[] args) {
//创建集合对象
ArrayList<Student> list = new ArrayList<Student>();
//创建学生对象
Student s1 = new Student("赵丽颖",18);
Student s2 = new Student("唐嫣",20);
Student s3 = new Student("景甜",25);
Student s4 = new Student("柳岩",19);
//把学生对象作为元素添加到集合中
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
//遍历集合
for(int x = 0; x < list.size(); x++) {
Student s = list.get(x);
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
打印集合方法,(能够使用ArrayList类作为形式参数和返回值)
定义以指定格式打印集合的方法(ArrayList类型作为参数),使用{}扩起集合,使用@分隔每个元素。输出格式参照 {元素@元素@元素}。
public class Test03ArrayList {
public static void main(String[] args) {
// 创建集合对象
ArrayList<String> list = new ArrayList<String>();
// 添加字符串到集合中
list.add("张三丰");
list.add("宋远桥");
list.add("张无忌");
list.add("殷梨亭");
// 调用方法
printArrayList(list);//{张三丰@宋远桥@张无忌@殷梨亭}//
}
public static void printArrayList(ArrayList<String> list) {
// 拼接左括号
System.out.print("{");
// 遍历集合
for (int i = 0; i < list.size(); i++) {
// 获取元素
String s = list.get(i);
// 拼接@符号
if (i != list.size() - 1) {
System.out.print(s + "@");
} else {
// 最后一个元素,拼接右括号
System.out.print(s + "}");
}
}
}
}
获取集合方法
定义获取偶数元素集合的方法(ArrayList类型作为返回值)
public class Test04ArrayList {
public static void main(String[] args) {
// 创建ArrayList 对象
ArrayList<Integer> list = new ArrayList<>();
// 添加随机数到集合
// 创建Random 对象
Random random = new Random();
for (int i = 0; i < 20; i++) {
int r = random.nextInt(1000) + 1;
list.add(r);//这么写不太合理,有可能都是奇数
}
// 调用偶数集合的方法
ArrayList<Integer> arrayList = getArrayList(list);
System.out.println(arrayList);
}
public static ArrayList<Integer> getArrayList(ArrayList<Integer> list) {
// 创建小集合,来保存偶数
ArrayList<Integer> smallList = new ArrayList<>();
// 遍历list
for (int i = 0; i < list.size(); i++) {
// 获取元素
Integer num = list.get(i);
// 判断为偶数,添加到小集合中
if (num % 2 == 0){
smallList.add(num);
}
}
// 返回小集合
return smallList;
}
}
4.7 扩展,普通for循环遍历集合元素的过程中,删除元素两种方案
import java.util.ArrayList;
public class Test13 {
public static void main(String[] args) {
//遍历过程中删除元素,注意事项
ArrayList<String> al = new ArrayList();
al.add("a");
al.add("b");//1//b
al.add("b");//2//c
al.add("c");
for (int i = al.size() - 1; i >= 0; i--) {//最大索引到0
String s = al.get(i);
if ("b".equals(s)){
al.remove(i);
}
}
//遍历
for (int i = 0; i < al.size(); i++) {
System.out.println(al.get(i));
}
}
}
4.8 扩展,集合去重,旧集合存了重复数据,通过新集合去重
//contains(Object o):如果此集合中包含指定的元素o,返回 true,否则返回false
import java.util.ArrayList;
public class Test15 {
public static void main(String[] args) {
//扩展,集合去重,旧集合存了重复数据,通过新集合去除
ArrayList<Integer> al = new ArrayList();
al.add(1);
al.add(666);
al.add(1);
al.add(888);
ArrayList<Integer> list = method(al);//list新的集合,去重集合
System.out.println(list);
}
//扩展,集合去重,旧集合存了重复数据,通过新集合去除,当做返回值
public static ArrayList<Integer> method(ArrayList<Integer> al) {
//新集合,空的容器
ArrayList<Integer> alnew = new ArrayList();
//遍历旧的集合一个个拿到元素,问新集合是否包含它,如果不包含,就添加到新的集合,包含不添加,去重
for (int i = 0; i < al.size(); i++) {
int e = al.get(i);//e旧集合里面每一个元素,鸡蛋
if (!alnew.contains(e)){//true,新集合是否包含旧集合元素,就添加到新的集合,包含不添加,去重
alnew.add(e);
}
}
return alnew;//地址值,对象,集合
}
本文围绕Java展开,介绍了API的使用步骤,还详细讲解了Scanner扫描器类、Random随机数类和ArrayList集合类。包括各类型的使用步骤、构造方法、成员方法等,同时给出了相关练习及扩展内容,如ArrayList集合的元素操作、存储基本数据类型等。

被折叠的 条评论
为什么被折叠?



