目录
- 1 ArrayList 集合
- 2 API 概述和使用
- 3 字符串
- 4 静态static关键字
- ● 练习
-
- 1 Scanner
- 2 匿名对象
- 3 Random
- 4 对象数组
- 5 ArrayList
- 6 字符串
-
- 1. 定义一个数组,把数组{1,2,3}按照指定格式拼接成一个字符串 ,格式:[word1#word2#word3]
- 2. 统计输入的字符串中各种字符的个数
- 3. 把字符串“i. said. remember. this. moment\n in. the. back .of .my . mind\n”按照分隔符“.”分隔为字符串数组,再将字符串数组转化为char数组和byte数组。不限分隔符字符串中的字符类型。(优化:字符串和分割字符串都自己输入)
- 4. (1)比较以下几个字符串是否是同一个String对象。(2)忽略大小写比较字符串"esEreW#212&REE","EserEw#212&rEe"是否相等
- 5. (1)将 字符串“Qweken#4”与字符串“Tyuken87*90+)_”和字符串“!@3ken4s”拼接成一个字符串。
- 6 全方法复习
1 ArrayList 集合
- 数组的长度不可以发生改变,ArrayList 集合的长度可以随意变化
ArrayList<E>中<E>
指泛型
泛型:装在集合当中的所有元素, 全都是统一的类型
注意:泛型只能是引用类型,不能是基本类型- jdk 1.7开始,右侧的尖括号内部可以不写内容,但<>本身还是要写
ArrayList<String> list = new ArrayList<>();
右侧可以不写泛型内容,但左侧一定要写,这个算是标准写法 - 对于ArrayList直接打印的不是地址值,而是[内容],为空则[],一般为
[元素1,元素2,元素3,元素4]
- ArrayList打印
public static void main(String[] args) {
ArrayList<String> list1 = new ArrayList<>();
list1.add("哈哈");
list1.add("呵呵");
System.out.println(list1);//[哈哈, 呵呵]
}
- ArrayList常用方法和遍历
public boolean add(E e)
向集合中添加元素,参数类型和泛型一致;返回值boolean代表是否添加成功
public E remove(int index)
根据索引删除,返回删除的元素。(索引从0开始)
public int size()
获取长度,返回值是集合中包含的元素个数
public E get(int index)
返回此列表中指定位置的元素。
list.fori
快速生成遍历循环
装箱,拆箱及包装类
- 因为泛型只能是引用类型,不能是基本类型,因为集合存的是地址。所以基本类型对应使用“包装类”
- 引用类型都位于java.lang包下,所以无需导包
基本类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
从JDK1.5开始,支持自动装箱,自动拆箱
自动装箱:基础类型 -> 包装类
自动拆箱:包装类 -> 基础类型
2 API 概述和使用
使用一个类:
1.导包:package语句后 import 包路径.类名称
class语句前,若要用的目标类和当前类位于同一个包下,省略导包。java.lang包下的内容不需要导包,其他的包都需要import语句。
2.创建:类名称 对象名 = new 类名称();
3.使用:对象名.成员方法名();
不同模块之间,不能导包或者互相使用
Scanner类
导包 import java.util.Scanner;
String next()
查找并返回此扫描仪的下一个完整令牌。
int nextInt()
将输入的下一个标记扫描为 int 。
public class Demo01Scanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入一个int给sc");
int num = sc.nextInt();
System.out.println("输入一个String给num");
String str = sc.next();
System.out.println("输出接收的sc,num");
System.out.println(num + "," + str);
}
}
练习一:输入两个int数字,并且求出和值
public class Exercise01InputSum {
public static void main(String[] args) {
inputSum();
}
public static int inputSum(){
Scanner sc = new Scanner(System.in);
System.out.println("输入两个整数");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
System.out.println("总和" + num1 + num2);
return num1 + num2;
}
}
练习二,输入三个数,求max先判断前2个哪个大,再把最大的跟第3个比较
public class Exercise02InputMax {
public static void main(String[] args) {
inputMax();
}
public static double inputMax(){
System.out.println("输入3个整数");
Scanner sc = new Scanner(System.in);
double num1 = sc.nextDouble();
double num2 = sc.nextDouble();
double num3 = sc.nextDouble();
double max = num1 < num2 ? num2 : num1;
max = max < num3 ? num3 : max;
System.out.println("max:" + max);
return max;
}
}
匿名对象
匿名对象只能使用唯一的一次,下次再用不得不再创建一个新对象
public class Demo1Anonymous {
public static void main(String[] args) {
Person one = new Person();
one.name = "芭比";
new Person().name = "肯";
one.showName();//我叫null
new Person().showName();//我叫芭比
}
}
- 匿名对象做参数和返回值
public class Demo2ParamReturn {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
methodParam(sc);
//用匿名对象简化写法
methodParam(new Scanner(System.in));
}
public static void methodParam(Scanner sc){
String str = sc.next();
System.out.println(str);
}
}
public class Demo2ParamReturn {
public static void main(String[] args) {
Scanner sc = methodReturn();
sc.next();
}
public static Scanner methodReturn(){
// Scanner sc = new Scanner(System.in);
// return sc;
//简化写法
return new Scanner(System.in);
}
}
Random生成随机数
【JavaSE API 】生成随机数的2种方法:Random类和Math类的Random方法
- 导包
import java.util.Random;
- 创建
Random r = new Random();
- 使用
获取一个随机的int数字(范围是int所有范围,有正负两种);
参数代表范围,左闭右开。[0,3)
int num = r.nextInt(3);
protected int next(int bits)
生成下一个伪随机数。
int nextInt(int bound)
返回伪随机的,均匀分布 int值介于0(含)和指定值(不包括),从该随机数生成器的序列绘制。
3 字符串
在java.lang包下,所以不用导包。
程序中所有的双引号字符串,都是String类的对象(就算没有new也是)
字符串的特点:
1.内容永不可变
2.因为字符串内容不可变,所以可以共享
3.字符串效果上相当于是char[]字符数组,但是底层原理是byte[]字节数组
字符串的3+1种构造方法
- 1
public String();
创建一个空白字符串, 不含有任何内容 - 2
public String(char[] array);
根据字符数组的内容,来创建对应的字符串 - 3
public String(byte[] array);
根据字节数组的内筒,来创建对应的字符串 - 1种直接创建
String str = "Hello";
注意:直接写上双引号,就是字符串对象。只要是字符串一定是对象
字符串的常量池
public boolean equalsIgnoreCase(String str);
//忽略大小写进行内容比较
equals和==
- ==
对于基本数据类型比较 内容
对于引用类,只有是同一个对象才true(比较的是对象的内存地址) - equals
只能比较引用对象,对于普通类对象,只有同为同一个对象才是true
但对于File,String,Data和包装类,是比较内容
public class Demo2ConstantPools {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "abc";
char[] charArray = {
'a', 'b', 'c'};
String str3 = new String(charArray);
System.out.println(str1 == str2);//true
System.out.println(str2 == str3);//false
}
}
程序当中直接写上的双引号字符串,就在字符串常量池中。
即直接创建的字符串在常量池,若内容相同即为共享一个对象,3种构造方法的字符串不在常量池也不是共享,是由字符数组或字节数组在堆中转化为字节数组的地址,内容相同也不是同一个对象。
public class Demo2ConstantPools {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "abc";
char[] charArray = {
'a', 'b', 'c'};
String str3 = new String(charArray);
byte[] byteArray = {
'a', 'b', 'c'};
String str4 = new String(byteArray);
byte[] b2 ={
'a', 'b', 'c'};
String str5 = new String(b2);
System.out.println(str1 == str2);//true
System.out.println(str2 == str3);//false
System.out.println(str3 == str4);//false
System.out.println(str4 == str5);//false
System.out.println(str1.equals(str2));//true
System.out.println(str2.equals(str3));//true
System.out.println(str3.equals(str4));//true
System.out.println(str4.equals(str5));//true
System.out.println(str1.equalsIgnoreCase("ABC"));//true
}
}
字符串的获取相关方法
public int length();
字符个数
public String concat(String str);
将当前字符串和参数拼接返回
public char charAt(int index);
获取指定索引位置的单个字符(从0开始)
public int indexOf(String str);
查出参数字符串在本字符串中首次出现的索引位置,没有就return -1;
注意:concat方法只会把拼接的结果返回,并不改变调用方法的字符串。除非原字符串接收他的返回值
字符串的截取方法
public String substring(int index);
截取从参数位置一直到字符串末尾。返回新字符串
public String substring(int begin, int end);
截取从begin开始一直到end结束中间的字符串。(按照索引从0开始对应 每一个字符,截取为 [begin,end))
字符串的转换相关方法
public char[] toCharArray();
将当前字符串拆分为字符数组作为返回值
public byte[] getBytes();
获得当前字符串底层的字节数组
public String replace(CharSequence oldString, CharSequence newString);
将所有出现的老字符替换成为新的字符串,返回替换后的新字符串(CharSequence简单理解为可以接受String类型)
字符串的分割方法
public String[] split(String regex);
按照参数的规则将字符串切分成为若干部分
注意: 无法根据.划分做参数,split方法的参数其实是一个正则表达式,如果按照英文句“.”必须写“\\.”
split() 方法根据匹配给定的正则表达式来拆分字符串。
注意: .
、 $
、 |
和 *
等转义字符,必须得加 \\
。
public class Demo3StringMethod {
public static void main(String[] args) {
//字符串的获取相关方法-------------------------------------------------------
String str1 = "Hi~Barbie";
System.out.println(str1.length());
System.out.println(str1.concat("Hi~Ken"));
System.out.println("索引3的字符" + str1.charAt(3));
str1 = str1.concat("bie~hello java $ rusty lake is fate 1 larua is blossom");
System.out.println("str1 : " + str1);
System.out.println("第一次出现bie的索引位置:" + str1.indexOf("bie"));
//字符串的截取方法
System.out.println("从4截取" + str1.substring(4));
System.out.println("从[7,13)截取" + str1.substring(7,13));
//字符串的转换相关方法
char[] ch = str1.toCharArray();
System.out.println("char数组:" + ch);
System.out.println(Arrays.toString(ch));
byte[] by = str1.getBytes();
System.out.println("by数组:" + by + Arrays.toString(by));
for (int i = 0; i < by.length; i++) {
System.out.print(by[i] + ",");
}
System.out.println();
String str2 = "Hoow doo yoou doo?";
System.out.println("str2" + str2);
System.out.println(str2.replace("oo", "*"));
//字符串的分割方法
String[] str3 = str1.split("a");
System.out.println("根据a划分str1:"+ str1);
for (int i = 0; i < str3.length; i++) {
System.out.println(str3[i]);
}
String str3 = "ewr.e..wfs...as.a.d";
String[] atr = str3.split("\\.");
printArray(atr);
String str4 = "ewr\ne.\nwfs.n.as.d";
String[] atr2 = str4.split("\\n");
printArray(atr2);
}
}
4 静态static关键字
用于多个对象共享同一份数据的情况,例如,一个学生类Student,除过姓名成绩等不同的属性外,共同的属性应该为同一间教室,多个对象的教室属性,内容完全相同。无需作为对象成员变量,每次赋不同的值。只需赋值一次,多个对象共享即可。
一旦用了static关键字,那么这样的内容不在属于对象自己,而是属于类的,所以凡是本类的对象,都共享一份static变量或方法。
例如:定义学生类中学号为static,作用:每new一个学生对象,学号都自增统计学生数量
public class DemoStatic {
public static void main(String[] args) {
Student stu1 = new Student("barbie",true,18);
Student stu2 = new Student();
Student stu3 = new Student("奥罗拉",true,16);
Student stu4 = new Student("菲利普",false, 17);
stu3.showInfo();
stu2.showInfo();
stu4.showInfo();
}
}
学生学号:3 姓名:奥罗拉 是否女孩:true 年龄:16
学生学号:2 姓名:null 是否女孩:false 年龄:0
学生学号:4 姓名:菲利普 是否女孩:false 年龄:17
静态方法不属于对象,而是属于类。如果没有static关键字,那么必须首先创建对象,然后通过对象才能使用他。
MyClass obj = new MyClass();
obj.method();
obj.methodStatic();//不推荐,会误以为是普通成员方法。
MyClass.methodStatic();
对于静态方法来说,可以通过对象名进行调用,也可以直接通过类名称来调用
不推荐用对象调用静态方法,因为会误以为是普通成员方法,且编译后也会被javac翻译成为类名称,.静态方法名
有static,都推荐使用类名称进行调用。
对于本类当中的静态方法,可以省略类名称直接调用。
注意:
1.静态不能直接访问非静态
2. 静态方法当中不能用this
因为在内存当中是先有得静态内容,后有的非静态内容。
this代表当前对象,通过谁调用的方法 ,谁就是当前对象。但静态方法不用对象
静态static的内存图
静态方法就算写为对象.方法
,对象.属性
。编译期间也会翻译为类.方法
或类.属性
,然后去方法区找。跟对象无关
public class Student {
//为什么定义两个,因为id属于学生的属性,属于对象,每个对象不一样。但idCount属于属于类
//每次new该类对象才自增,不保存加过的数
private static int idCount = 0;
private int id = 0;
static String room = "一年级一班";
String name;
boolean girl;
int age;
public Student(String name, boolean girl, int age) {
this.id = ++idCount;
this.name = name;
this.girl = girl;
this.age = age;
}
public Student(){
this.id = ++idCount;
}
public void showInfo(){
System.out.println("学生学号:" + this.id + " 姓名:" + this.name + " 是否女孩:" + this.girl + " 年龄:" + this.age);<