API (应用程序接口)
- 看包
- 看构造
- 看成员
Scanner类
-
功能:实现键盘输入数据
-
使用步骤:
- 导包:
import java.util.Scanner;
- 创建:
Scanner sc = new Scanner(System.in);
- 使用:
int a=sc.nextint();
匿名对象
格式:new 类名称();
new Person().name="lin";
new Person().showName();//不会输出 lin 此次会重新创建
//匿名对象只能使用一次
Scanner结合匿名对象
public class Dome01 {
public static void main(String[] args) {
//普通方式
System.out.println("请输入");
Scanner sc =new Scanner(System.in);
int i = sc.nextInt();
System.out.println(i);
//匿名对象
System.out.println("请输入");
int j=new Scanner(System.in).nextInt();
System.out.println(j);
//使用匿名对象进行传参
System.out.println("请输入");
print(new Scanner(System.in));
//匿名对象作为返回值
Scanner sr=get();
int num=sr.nextInt();
System.out.println(num);
}
public static void print(Scanner sc){
int i = sc.nextInt();
System.out.println(i);
}
public static Scanner get() {
return new Scanner(System.in);
}
}
Random类
public class Random2 {
public static void main(String[] args) {
Random r= new Random();
//默认为int的取值范围
int num = r.nextInt();
System.out.println(num);
//范围为左闭右开 [0,10)==0~9
int nums =r.nextInt(10);
System.out.println(nums);
}
}
案例:
//生成1~n的随机数
Random r = new Random();
int num = r.nextInt(10)+1;
对象数组
数组中的元素为对象,本质是存放地址,一旦创建长度不变
Person1[] arry=new Person1[3];
Person1 per1=new Person1(12,"li",18);
Person1 per2=new Person1(13,"lin",19);
Person1 per3=new Person1(14,"wang",20);
arry[0]=per1;
arry[1]=per2;
arry[2]=per3;
System.out.println(arry[0].getName()+"的ID:"+arry[0].getID());
System.out.println(arry[1].getName()+"的ID:"+arry[1].getID());
System.out.println(arry[2].getName()+"的ID:"+arry[2].getID());
ArrayList类
-
解决数组长度不可变的缺点 ,用到泛型编程,泛型是引用类型,不是基本类型,即中的E只能是引用类型不能为基本类型
-
注意事项:对于ArrayList集合来说,直接打印得到的不是地址,而是内容,如果内容为空,得到的是空的中括号【】
//ArrayList常用方法
public boolean add(E e);
public E get(int index);
public E remove(int index);
public int size();
//ArrayList集合的add添加元素一定成功返回值可用可不用,其他集合添加元素不一定成功
//自动生成遍历
ArrayList<String>List=new ArrayList<>();
List.fori//回车即可自动生成
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 |
ArrayList<Integer> list = new ArrayList<>();
list.add(100);
//其余操作一致
String类
-
Java程序中的所有字符串字面值都作为此类的实例实现
-
字符串效果上相当于char[]字符数组,但底层原理是byte[]字节数组
-
创建方式:
//三种构造方法
public String();
public String(char[] array);
public String(byte[] array);
//一种直接创建
String str ="asd";
public class Dome01 {
public static void main(String[] args) {
//使用空参构造,所得数组长度为0
String str1=new String();
System.out.println("第一个数组:"+str1);
//使用字符数组构造
char[] charArray={'a','b','c'};
String str2=new String(charArray);
System.out.println("第二个数组:"+str2);
//使用字节数组
byte[] byteArray={97,98,99};
String str3=new String(byteArray);
System.out.println("第三个数组:"+str3);
//直接创建
String str4="HELLO";
}
}
- 常用方法
- 字符串比较:
//常用的字符串比较方法
public boolean equals(Object obj);//进行值比较
public boolean equalsIngnoreCase(String str)//忽略大小写
//参数可以是任何对象,只有参数是一个字符串并且内容相同的才会给true
//任何对象都可以用Object进行接收
public class Dome02 {
public static void main(String[] args) {
String str1="hello";
char[] str2={'h','e','l','l','o'};
String str3=new String(str2);
System.out.println(str1.equals("hello"));//true
System.out.println("hello".equals(str1));//true
System.out.println(str1.equals(str2));//false,str2不是字符串
System.out.println(str1.equals(str3));//true
}
}
注意事项:如果比较双方有一个常量,把常量写在前面 例:“hello”.equals(str1);
理由:
//当str1=null时
System.out.println(str1.equals("hello"));
//得到报错,空指针异常
System.out.println("hello".equals(str1));
//得到得到false
- 获取字符串
public int length();//获取字符串当前长度
public String concat(String str);//字符串拼接,返回一个新的字符串地址
public char charAt(int index);//获取index位置的字符,索引从0开始
public int indexof(String str);//查找参数字符串在本字符串中首次出现的位置,没有则返回-1值
- 截取字符串
public String substring(int index);//从index开始到结尾截取
public String substring(int begin,int end)//[begin,end),截取范围
- 转化字符串
public char[] toCharArray();//转化为字符数组
public byte[] getBytes();//转化为字节数组
public String replace(CharSequence oldString,CharSequence newString);//将老字符串替换为新的字符串
- 分割字符串
public String[] splist(String regex);//按照规则分割
public class Dome03 {
public static void main(String[] args) {
String str1="aaa,bbb,ccc";
String[] str2=str1.split(",");
for (int i = 0; i < str2.length; i++) {
System.out.println(str2[i]);
}
}
}
注意正则表达式.”应写为“//.”;
- 字符串数组本质是保存字符串的地址
案例
//计算字符出现次数
public class Dome02 {
public static void main(String[] args) {
Scanner scanf =new Scanner(System.in);
System.out.println("请输入数据");
String str=scanf.nextLine();
char[] str2=str.toCharArray();
count(str2);
}
public static void count(char[] str){
if (str.length==0){
System.out.println("无数据");
return;
}
int count_int=0;
int count_upchar=0;
int count_lowchar=0;
int count_other=0;
for (int i = 0; i < str.length; i++) {
if (str[i]>='A'&&str[i]<='Z'){
count_upchar++;
continue;
}else if (str[i]>='a'&&str[i]<='z'){
count_lowchar++;
continue;
}else if (str[i]>='0'&&str[i]<='9'){
count_int++;
continue;
}else{
count_other++;
continue;
}
}
System.out.println("number:"+count_int);
System.out.println("upchar:"+count_upchar);
System.out.println("lowchar:"+count_lowchar);
System.out.println("other:"+count_other);
}
}
static
- 概括
- static 修饰成员变量
- static 修饰成员方法:方法被static修饰则该方法不属于对象属于类
//区别
public class Dome01 {
public static void main(String[] args) {
//使用对象调用方法
Dome01 qq=new Dome01();
qq.add();
//使用静态成员方法
add1();
Dome01.add1();
//对于静态方法,可以通过对象名调用,也可以通过类名调用,但在javac编译时会被翻译成 “类.方法”
//对于本类当中的静态方法,可以省略类名称
}
public void add(){
System.out.println("aa");
}
public static void add1(){
System.out.println("bb");
}
}
-
注意点:静态成员不能直接访问非静态成员,因为内存中是先有的静态内容后有非静态内容;静态方法不能使用this
-
内存情况
- 静态代码块:特点:当第一次用到本类时,静态代码块执行唯一 一次。静态内容总是优先于非静态
public class 类名称{
static{
//静态代码块
}
}
public class Person {
static {
System.out.println("调用静态代码块");
}
public Person(){
System.out.println("调用构造");
}
}
public class Dome02 {
public static void main(String[] args) {
Person per=new Person();
Person per2=new Person();
}
}
//结果如下
调用静态代码块
调用构造
调用构造
- 用途:为静态成员变量进行赋值
Arrays类(工具类)初探
//常用方法
public static String toString(数组)
public static void sort(数组)//默认从小到大,对照ascii码表
int[] array={12,44,76,45,90};
System.out.println(Arrays.toString(array));
Arrays.sort(array);
System.out.println(Arrays.toString(array));
Math类(工具类)
public static double abs(double num);//绝对值
public static double ceil(double num);//向上取整,即向大的数取
public static double floor(double num);//向下取整
public static long round(double num);//四舍五入
扩展
- 只有java.lang包下内容不需要导包,其余都要import语句
int[] array={12,44,76,45,90};
System.out.println(Arrays.toString(array));
Arrays.sort(array);
System.out.println(Arrays.toString(array));
Math类(工具类)
public static double abs(double num);//绝对值
public static double ceil(double num);//向上取整,即向大的数取
public static double floor(double num);//向下取整
public static long round(double num);//四舍五入
扩展
- 只有java.lang包下内容不需要导包,其余都要import语句
- 格式化代码:ctrl+alt+L