1. Scanner类
- 功能
可以实现键盘的输入数据,到程序当中去。
- 一般使用步骤
1.导包
Import 包路径.类名称;
如果需要使用的目标类,和当前类位于同一个包下,则可以省略导包语句不写,只有Java.lang包下的内容不需要导包,其他的包,其他的包需要import语句。
2.创建
类名称 对象名 =new 类名称();
3.使用
对象名.成员方法名();
获取键盘输入的一个int 数字: int 变量名称=对象名.nextInt();
获取键盘输入一个字符串:String 变量名称=对象名.nextInt();
- 格式
Scanner 对象名=new Scanner(System.in)
…
import java.util.Scanner;
public class example {
public static void main(String[] args) {
int r;
float area,perimeter;
double s,c;
final double PI = 3.14;
System.out.println("请输入圆的半径");
Scanner sum=new Scanner(System.in);
r=sum.nextInt();
s=PI*r*r;
c=2*PI*r;
System.out.println("圆的面积为"+s);
System.out.println("圆的周长为"+c);
2. 匿名对象类
-
概念
创建对象时,只有创建对象的语句,却没有把对象地址值赋值给某个变量。虽然是创建对象的简化写法,但是应用场景非常有限。匿名对象是没有对象名的
-
格式
new 类名(参数列表);
new 类名称().变量名称/方法;(在另外一个类当中定义)
注意:匿名对象只能使用唯一次,如果下次要使用,不得不重新创建对象(类名称 对象名=new 类名称)。
- 应用场景
- 创建匿名对象直接调用方法,没有变量名。
new Scanner(System.in).nextInt();
- 一旦调用两次方法,就是创建了两个对象,造成浪费,请看如下代码。
new Scanner(System.in).nextInt();
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){
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类
功能: 用于随机获取数字
使用步骤:
查看类:Java.util.Random:该类需要import导入后使用
查看构造方法:public Random():创建一个新的随机数生成器
查看成员方法:public int nextInt(int n):返回一个随机数,范围在0(包括)和指定值n(不包括)之间的int值
//1. 导包
import java.util.Random;
public class Demo01_Random {
public static void main(String[] args) {
//2. 创建键盘录入数据的对象
Random r = new Random();
for(int i = 0; i < 3; i++){
//3. 随机生成一个数据
int number = r.nextInt(10);
//4. 输出数据
System.out.println("number:"+ number);
}
}
}
4. Arraylist类
概念:
是大小可变的数组的实现,存储在内的数据称为元素。此类提供一些方法来操作内部存储的元素。 ArrayList 中可不断添加元素,其大小也自动增长。
注意事项:
- Arraylist有个<>尖括号表示的是泛型(在集合当中的所有的元素,全部都是统一的什么类型)
- 泛型只能是引用类型,不能是基本类型
- 对于arraylist来说,直接打印的结果不会是地址值,而是内容。如果内容是空的,得到的是[ ]
- Arraylist引用的只能是字符串。
格式:ArrayList<String> 变量名 = new ArrayList<>();
常用方法:
public boolean add(E e) :将指定的元素添加到此集合的尾部。
public E remove(int index) :移除此集合中指定位置上的元素。返回被删除的元素。
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("get:"+list.get(0));
System.out.println("get:"+list.get(1));
System.out.println("get:"+list.get(2));
//public int size():返回集合中的元素的个数
System.out.println("size:"+list.size());
//public E remove(int index):删除指定索引处的元素,返回被删除的元素
System.out.println("remove:"+list.remove(0));
//遍历输出
for(int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
}
}
}
如何存储数据基本类型:
基本类型 | 基本包装类 |
---|---|
byte | Byte |
short | short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | character |
boolean | Boolean |
使用步骤
A.查看类
java.util.ArrayList :该类需要 import导入使后使用。
< E >,表示一种指定的数据类型,叫做泛型。 E ,取自Element(元素)的首字母。在出现 E 的地方,我们使用一种引用数据类型将其替换即可,表示我们将存储哪种引用类型的元素。
例如:ArrayList<String>,ArrayList<Student>
B.查看构造方法
public ArrayList() :构造一个内容为空的集合。
基本格式:
ArrayList<String> list = new ArrayList<String>();
在JDK 7后,右侧泛型的尖括号之内可以留空,但是<>仍然要写。简化格式:
ArrayList<String> list = new ArrayList<>();
C.查看成员方法
public boolean add(E e) : 将指定的元素添加到此集合的尾部。
参数 E e ,在构造ArrayList对象时, 指定了什么数据类型,那么 add(E e) 方法中,只能添加什么数据类型的对象。
public class Test02StudentArrayList {
public static void main(String[] args) {
//创建学生数组
ArrayList<String> list = new ArrayList<>();
//创建学生对象
String s1 = "曹操";
String s2 = "刘备";
String s3 = "孙权";
//打印学生ArrayList集合
System.out.println(list);
//把学生对象作为元素添加到集合
list.add(s1);
list.add(s2);
list.add(s3);
//打印学生ArrayList集合
System.out.println(list);
}
}
5. String类
字符串的概叙和特点:
(1)概叙
java.lang.String 类代表字符串。Java程序中所有的字符串文字(例如 “abc” )都可以被看作是实现此类的实例。类 String 中包括用于检查各个字符串的方法,比如用于比较字符串,搜索字符串,提取子字符串以及创建具有翻译为大写或小写的所有字符的字符串的副本。
(2)特点
1.字符串的内容永不改变
2.字符串可以被共享
3.字符串的效果相当于char [ ] 字符数组,但是底层的原理是byte[ ]字节数组。
“abc” 等效于 char[] data={ ‘a’ , ‘b’ , ‘c’ } 。
使用步骤:
// 无参构造
String str = new String();
// 通过字符数组构造
char chars[] = {‘a’, ‘b’, ‘c’};
String str2 = new String(chars);
// 通过字节数组构造
byte bytes[] = { 97, 98, 99 };
String str3 = new String(bytes);
A.查看类
java.lang.String :此类不需要导入。
B.查看构造方法
public String() :初始化新创建的 String对象,以使其表示空字符序列。
public String(char[] value) :通过当前参数中的字符数组来构造新的String。
public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来构造新的
String。
// 无参构造
String str = new String();
// 通过字符数组构造
char chars[] = {'a', 'b', 'c'};
String str2 = new String(chars);
// 通过字节数组构造
byte bytes[] = { 97, 98, 99 };
String str3 = new String(bytes);
常用方法
public boolean equals (Object anObject) :将此字符串与指定对象进行比较。
public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小
写。
public class String_Demo01 {
public static void main(String[] args) {
// 创建字符串对象
String s1 = "hello";
String s2 = "hello";
String s3 = "HELLO";
// boolean equals(Object obj):比较字符串的内容是否相同
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // false
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
//boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
System.out.println(s1.equalsIgnoreCase(s2)); // true
System.out.println(s1.equalsIgnoreCase(s3)); // true
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
}
}
//Object 是” 对象”的意思,也是一种引用类型。作为参数类型,表示任意对象都可以传递到方法中。
获取功能的方法:
public int length () :返回此字符串的长度。
public String concat (String str) :将指定的字符串连接到该字符串的末尾。
public char charAt (int index) :返回指定索引处的 char值。
public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。
public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
public class String_Demo02 {
public static void main(String[] args) {
//创建字符串对象
String s = "helloworld";
// int length():获取字符串的长度,其实也就是字符个数
System.out.println(s.length());
System.out.println("‐‐‐‐‐‐‐‐");
// String concat (String str):将将指定的字符串连接到该字符串的末尾.
String s = "helloworld";
String s2 = s.concat("**hello itheima");
System.out.println(s2);// helloworld**hello itheima
// char charAt(int index):获取指定索引处的字符
System.out.println(s.charAt(0));
System.out.println(s.charAt(1));
System.out.println("‐‐‐‐‐‐‐‐");
// int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回‐1
System.out.println(s.indexOf("l"));
System.out.println(s.indexOf("owo"));
System.out.println(s.indexOf("ak"));
System.out.println("‐‐‐‐‐‐‐‐");
// String substring(int start):从start开始截取字符串到字符串结尾
System.out.println(s.substring(0));
System.out.println(s.substring(5));
System.out.println("‐‐‐‐‐‐‐‐");
// String substring(int start,int end):从start到end截取字符串。含start,不含end。
System.out.println(s.substring(0, s.length()));
System.out.println(s.substring(3,8));
}
}
转换功能
public char[] toCharArray () :将此字符串转换为新的字符数组。
public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使用replacement字符串替换。
public class String_Demo03 {
public static void main(String[] args) {
//创建字符串对象
String s = "abcde";
// char[] toCharArray():把字符串转换为字符数组
char[] chs = s.toCharArray();
for(int x = 0; x < chs.length; x++) {
System.out.println(chs[x]);
}
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
// byte[] getBytes ():把字符串转换为字节数组
byte[] bytes = s.getBytes();
for(int x = 0; x < bytes.length; x++) {
System.out.println(bytes[x]);
}
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
// 替换字母it为大写IT
String str = "itcast itheima";
String replace = str.replace("it", "IT");
System.out.println(replace); // ITcast ITheima
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
}
}
//CharSequence 是一个接口,也是一种引用类型。作为参数类型,可以把String对象传递到方法中。
分割功能的方法
public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。
public class String_Demo03 {
public static void main(String[] args) {
//创建字符串对象
String s = "aa|bb|cc";
String[] strArray = s.split("|"); // ["aa","bb","cc"]
for(int x = 0; x < strArray.length; x++) {
System.out.println(strArray[x]); // aa bb cc
}
}
}
6. Static类
概述:
关于 static 关键字的使用,它可以用来修饰的成员变量和成员方法,被修饰的成员是属于类的,而不是单单是属于某个对象的。也就是说,既然属于类,就可以不靠创建对象来调用了。
定义和使用格式:
类变量:
使用static静态关键字修饰成员变量
定义格式: static 数据类型 变量名;
静态方法:
当 static 修饰成员方法时,该方法称为类方法 。静态方法在声明中有 static ,建议使用类名来调用,而不需要创建类的对象。调用方式非常简单。
定义格式
修饰符 static 返回值类型 方法名(参数列表){
//执行语句
}
静态方法调用的注意事项:
1.可以直接访问类变量和静态方法。
2.不能直接访问普通成员变量或成员方法。反之,成员方法可以直接访问类变量或静态方法。
3.不能使用 this关键字。(静态方法只能访问成员)
调用方式:
被static修饰的成员可以并且建议通过类名直接访问。虽然也可以通过对象名访问静态成员,原因即多个对象均属于一个类,共享使用同一个静态成员,但是不建议,会出现警告信息。
格式:
/ /访问类变量
类名.类变量名;
//调用静态方法
类名.静态方法名(参数);
静态代码块
public class 类名{
static {
// 执行语句
}
}
位置:类中方法外。
执行:随着类的加载而执行且执行一次,优先于 main方法和构造方法的执行。
7. Arrays工具
1)Java.util.arrrays是一个与数组相关的工具类,里面提供了大量静态方法,用来实现数组的操作。
2)Public static String tostring(数组):将参数数组变成字符串(按照默认格式:[元素1,元素2,元素3…])
3)Public static void sort(数组):按照默认升序(从小到大)对数组的元素进行排序
注意:
1.如果是数据,sort默认使用升序从小到大
2.如果是字符串,sort按照字母升序
3.如果是自定义的类型,那么这个自定义的类需要comparable或者cpmparator
int [] aew={21,23,45};
Arrays.sort(aew);
System.out.println(Arrays.toString(aew));//[21,23,45]
String []sh={"hhhh","wwww","aaaa"};
Arrays.sort(sh);
System.out.println(Arrays.toString(sh));//[aaaa,hhhh,wwww]
8. Math工具
(1)Java.util.math类是数学相关的工具类,里面提供了大量的静态方法,完成与教学运算相关的操作。
(2)Public static double abs(double num):获取绝对值,有多种重载
(3)Public static double ceil(double num):向下取整
(4)Public static double floor (double num):向上取整
(5)Public static long round(double num):四舍五入
(6)Math.PI代表的圆周率常量(double)
//绝对值
System.out.println(Math.abs(-521.1));//521.1
//向上取整
System.out.println(Math.ceil(23.5));//24.0
System.out.println(Math.ceil(25.1));//26.0
//向下取整
System.out.println(Math.floor(23.1));//23.0
System.out.println(Math.floor(23.9));//23.0
System.out.println(Math.floor(25.0));//25.0
//四舍五入
System.out.println(Math.round(23.45));//23
System.out.println(Math.round(23.9));//24
System.out.println(Math.round(24.1));//24