API
API(Application Programming Interface),应用程序编程接口。Java API是一本程序员的字典,是jdk提供给我们使用的类的说明文档。这些类将底层的代码实现封装了起来。我们可以通过查询API的方式,来学习、使用Java提供的类。
使用步骤:
1.打开帮助文档。
2.点击显示,找到索引,看到输入框。
3.在输入框里输入你要查询的内容,回车。
4.看包。java.lang下的类不需要导包,其他的需要。
5.看类的解释说明。
6.学习构造方法。
7.使用成员方法。
Scanner类
可以实现键盘输入数据,到程序当中。
引用类型的一般使用步骤:
1.导包
如果需要使用的目标类和当前类位于同一包下,导包语句可省略。
只有java.lang下的内容不需要导包,其他包都需要import语句。
import 包路径.类名称;
2.创建
类名称 对象名 = new 类名称();
3.使用
对象名.成员方法名()
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//System.in代表从键盘输入
//获取键盘输入的int数字
int num = sc.nextInt();
System.out.println("这个数为" + num);
//获取键盘输入的String字符串
String str = sc.next();
System.out.println("这个字符串为" + str);
}
}
匿名对象
创建对象的标准格式
类名称 对象名 = new 类名称();
匿名对象就只有右边的对象,没有左边的名字和赋值运算符
new 类名称();
注意: 匿名对象只能使用一次,下次再用只能创建一个新对象。
如果确定有一个对象只需要使用唯一的一次,就可以使用匿名对象。
匿名对象作为方法的参数和返回值
import java.util.Scanner;
public class Demo04Anonymous {
public static void main(String[] args) {
//匿名对象作为函数参数
methodParam(new Scanner(System.in));
//匿名对象作为函数的返回值
int num = methodReturn().nextInt();
System.out.println("再次输入的数为" + num);
}
//匿名对象作为函数参数
public static void methodParam(Scanner sc) {
int num = sc.nextInt();
System.out.println("输入的数为" + num);
}
//匿名对象作为函数的返回值
public static Scanner methodReturn() {
return new Scanner(System.in);
}
}
Random类
用来生成随机数字。
使用步骤:
1.导包
import java.util.Random;
2.创建
Random r = new Random();
3.使用
获取一个随机的int数字,范围是int所有范围,有正负
r.nextInt();
获取一个随机的int数字,参数代表了范围,左闭右开区间
r.nextInt(3); //范围为[0,3)即0~2
import java.util.Random;
/*
* 根据int变量n的值,来获取随机数字,范围是[1,n]
*/
public class Demo05Random {
public static void main(String[] args) {
int n = 8;
Random r = new Random();
int num = r.nextInt(n) + 1;
System.out.println(num);
}
}
对象数组
public class Demo07Array {
public static void main(String[] args) {
//创建一个长度为3的数组,用来存放Person类型的对象。
Person[] array = new Person[3];
Person one = new Person("周老大",3);
Person two = new Person("周老二",2);
Person three = new Person("周老三",1);
//分别将one、two、three当中的地址值放入数组中
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[0].getName());
System.out.println(array[1].getName());
System.out.println(array[2].getName());
}
}
在这里插入代码片public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name,int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
ArrayList类
java.util.ArrayList是大小可变的数组的实现,存储在内的数据成为元素。此类提供一些方法来操作内部存储的元素。ArrayList中可不断添加元素,其大小也自动增长。
对于ArrayList来说,有一个尖括号代表泛型。
泛型: 也就是装在集合当中的所有元素,全都是统一的某一类型。
注意: 泛型只能是引用类型,不能是基本类型。
//创建了一个ArrayList集合,集合的名称是list,里面装的全都是String字符串类型的数据
【从jdk1.7+开始,右侧尖括号内部可以不写内容,但是尖括号本身不可省略】
ArrayList<String> list = new ArrayList<String>();
注意:
对于ArrayList集合来说,直接打印的得到的不是地址值,而是内容。
如果内容是空,得到是空的中括号:[ ]
向集合当中添加数据需要用到add方法
对象名.add(); //list.add("aa');
ArrayList中常用的方法和遍历
ArrayList中的常用方法:
1. public boolean add(E e)
【向集合当中添加元素,参数的类型和泛型的类型一致。返回值代表添加是否成功。
*注意:对于ArrayList集合来说,add添加动作一定是成功的,所以返回值可用可不用】
2. public E get(int index)
【从集合当中获取元素,参数是索引编号,返回值就是对应位置的元素。】
3. public E remove(int index)
【从集合当中删除元素,参数是索引编号,返回值就是被删除的元素。】
4.public int size()
【获取集合的尺寸长度,返回值是集合中包含的元素个数。】
ArrayList遍历:
import java.util.ArrayList;
/*
* 遍历
*/
public class Demo08ArrayListEach {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
ArrayList存储基本数据类型
如果希望向集合ArrayList当中存储基本数据类型,必须使用基本类型对应的“包装类”。
基本类型 | 包装类(引用类型,都位于java.lang包下) |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
从jdk 1.5+开始,支持自动装箱、自动拆箱。
自动装箱:基本类型 => 引用类型
自动拆箱:引用类型 => 基本类型