目录
数组
一维数组
数组是相同数据类型的有序集合,数组可以表示任何相同类型的一组内容(包括引用类型和基本类型)其中存放的每一个数据称为数组的一个元素。
语法:
public static void main(String[] args) {
int[] array; //类型[]表示这是个数组类型
}
数组类型比较特殊,它本身也是类,但是编程不可见(底层C++编写,在运行时动态创建)即使是基本类型的数组,也是以对象的形式存在的,并不是基本数据类型。所以我们要创建一个数组,同样需要使用new关键字:
public static void main(String[] args) {
int[] array=new int[10]; //在创建数组时,需要指定数组长度
Object obj =array; //因为同样是类,肯定是继承自Object的,所以可以直接向上转型
}
同样可以使用Object类的各种用法。
静态初始化:
public static void main(String[] args) {
int[] array=new int[]{1,2,3,4,5};
//缩写int[] array={1,2,3,4,5};
System.out.println(array.length);
}
数组长度不能为负数。
length是在一开始就确定的,而且是final类型的,不允许修改。
创建出来的数组,每个位置上都有默认值,如果是引用类型就是null,如果是基本数据类型就是0,或者是false,跟对象成员变量的默认值是一样的。
访问数组的某一个元素:
public static void main(String[] args) {
int[] array=new int[]{1,2,3,4,5};
System.out.println(array[1]); //变量名[下标]
}
//输出2
也可以通过这种方式为数组的元素赋值或修改值:
public static void main(String[] args) {
int[] array=new int[]{1,2,3,4,5};
array[0]=888;
System.out.println(array[0]);
}
数组的下标是从0开始的,不是从1开始。第一个元素下标是0,访问元素不能写成负数或超出范围,否则会出现异常。
判断相同和打印数组:
public static void main(String[] args) {
int[] a={1,2,3,4,5};
int[] b={1,2,3,4,5};
System.out.println(a.equals(b)); //输出false
System.out.println(equals(a,b)); //输出true
}
private static boolean equals(int[]a,int[]b){
for (int i = 0; i < a.length; i++)
if(a[i]!=b[i]) return false;
return true;
}
打印整个数组中的元素也只能一个一个访问:
public static void main(String[] args) {
int[] a={1,2,3,4,5};
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
//简写: for (int i :a){
// System.out.print(i);
// }
}
//输出1 2 3 4 5
对于基本类型的数组来说不支持自动装箱和拆箱。
由于基本数据类型和引用类型不同,所以说int类型的数组不能被Object类型的数组变量接收。引用类型的话是可以的,且支持向上转型和向下转型。
引用类型用final就是不能改变它引用的对象,数组里面的值可以修改:
public static void main(String[] args) {
final int[] a={1,2,3,4,5}; //加final里面的值可以改
a[0]=100;
System.out.println(a[0]);
}
多维数组
语法:
public static void main(String[] args) {
int[][] a={{1,2},{3,4},{5,6}};
System.out.println(a[0][1]);
}
public static void main(String[] args) {
int[][] arr =new int[][]{{1,2,3,4},
{5,6,7,8}};
System.out.println(arr[1][1]);
}
//输出6
可以引用或修改某一个数组:
public static void main(String[] args) {
int[][] arr =new int[][]{{1,2,3,4},
{5,6,7,8}};
int[]a=arr[0]; //引用
arr[0]=new int[]{0,0,0,0}; //修改
System.out.println(arr[0][1]);
}
遍历多维数组:
需要多次嵌套循环:
public static void main(String[] args) {
int[][] arr =new int[][]{{1,2,3,4,0,3},
{5,6,7,8}};
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
可变长参数
数组的延伸应用。
我们在使用时,可以传入0~n个对应的实参:
public static void main(String[] args) {
test("1","2","3");
}
private static void test(String... str){
System.out.println(str[0]);
}
//输出1
private static void test(String... str){
for(String s:str){
System.out.println(s);
}
}
//输出
1
2
3
如果同时存在其他参数,那么可变长参数只能放在最后。且可变长参数只能有一个。
字符串
String类
String本身也是一个类,只不过它比较特殊,每个用双引号括起来的字符串,都是String类型的一个实例对象。
字符串构造:
public static void main(String[] args) {
// 使用常量串构造
String s1 = "hello";
System.out.println(s1);
// 直接newString对象
String s2 = new String("hello");
System.out.println(s2);
// 使用字符数组进行构造
char[] array = {'h','e','l','l'};
String s3 = new String(array);
System.out.println(s3);
}
//....
String对象的比较:
如果是直接双引号创建的字符串,如果内容相同,为了优化效率始终都是同一个对象:
public static void main(String[] args) {
String s1 = "hello world";
String s2 = "hello world";
System.out.println(s1==s2);
}
//输出true
如果使用构造方法主动创建两个新的对象,那么就是不同的对象了:
public static void main(String[] args) {
String s1 = new String("hello world");
String s2 = new String("hello world");
System.out.println(s1==s2);
}
//输出false
String类重载了equals方法用于判断和比较内容是否相同:
public static void main(String[] args) {
String s1 = new String("hello world");
String s2 = new String("hello world");
System.out.println(s1.equals(s2));
}
//输出true
字符串的操作方法:
替换:
public static void main(String[] args) { String s1="hello world"; String s2=s1.replace("hello","hi"); System.out.println(s2); }
截取:
public static void main(String[] args) { String s1 ="hello world".substring(0,3); System.out.println(s1); }
String substring(int beginIndex) 从指定索引截取到结尾,
String substring(int beginIndex, int endIndex) 截取部分内容:
public static void main(String[] args) { String s1 ="hello world"; System.out.println(s1.substring(3)); //输出lo world System.out.println(s1.substring(0,3)); //输出hel }
public static void main(String[] args) { String s1 ="hello world"; String s2=s1.substring(s1.length()-3); System.out.println(s2); } //输出rld
分割:
String[] split(String regex) 将字符串全部拆分
String[] split(String regex, int limit) 将字符串以指定的格式,拆分为limit组
public static void main(String[] args) { String s1 ="hello world"; String[] s2=s1.split(" ");//这里是通过空格分隔 for(String s:s2){ System.out.println(s); } } //输出 hello world
拆分是特别常用的操作,另外有些特殊字符作为分割符可能无法正确切分, 需要加上转义。
- 字符"|","*","+"都得加上转义字符,前面加上 "\\"
- 如果是 "\" ,那么就得写成 "\\\\"
- 如果一个字符串中有多个分隔符,可以用"|"作为连字符
转化:
字符串和字符数组转换:
public static void main(String[] args) { String str ="hello world"; char[] chars=str.toCharArray(); System.out.println(chars[1]); }
public static void main(String[] args) { char[]chars=new char[]{'q','a','q'}; String str=new String(chars); System.out.println(str); }
大小写转换:
toLowerCase()大写转小写,toUpperCase()小写转大写。
public static void main(String[] args) { String s1="hello"; System.out.println(s1.toUpperCase( )); }
......
......
StringBuilder类
由于String的不可更改特性,为了方便字符串的修改,Java中又提供StringBuilder和StringBuffer类。这两个类大部分功能是相同的。
String和StringBuilder类不能直接转换:
String变为StringBuilder: 利用StringBuilder的构造方法或append()方法
StringBuilder变为String: 调用toString()方法。
public static void main(String[] args) {
StringBuilder builder=new StringBuilder();//一开始创建时,内部什么都没有
builder
.append("AAA")//使用append方法将字符串拼接到后面
.append("BBB")
.reverse();//链式调用,也可一个个输入
System.out.println(builder);//当字符串编辑完后,就可以使用toString转换为字符串了
}
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder("hello");
StringBuilder sb2 = sb1;
sb1.append(' ');// hello
sb1.append("world"); // hello world
sb1.append(123); // hello world123
System.out.println(sb1); // hello world123
System.out.println(sb1 == sb2); // true
System.out.println(sb1.charAt(0)); // 获取0号位上的字符 h
System.out.println(sb1.length()); // 获取字符串的有效长度14
System.out.println(sb1.capacity());// 获取底层数组的总大小
sb1.setCharAt(0, 'H'); // 设置任意位置的字符 Hello world123
sb1.insert(0, "Hello world!!!"); // Hello world!!!Hello world123
System.out.println(sb1);
System.out.println(sb1.indexOf("Hello")); // 获取Hello第一次出现的位置
System.out.println(sb1.lastIndexOf("hello")); // 获取hello最后一次出现的位置
sb1.deleteCharAt(0); // 删除首字符
sb1.delete(0,5); // 删除[0, 5)范围内的字符
String str = sb1.substring(0, 5); // 截取[0, 5)区间中的字符以String的方式返回
System.out.println(str);
sb1.reverse(); // 字符串逆转
str = sb1.toString(); // 将StringBuffer以String的方式返回
System.out.println(str);
}
String、StringBuffer、StringBuilder的区别
- String的内容不可修改,StringBuffer与StringBuilder的内容可以修改。
- StringBuffer与StringBuilder大部分功能是相似的。
- StringBuffer采用同步处理,属于线程安全操作;而StringBuilder未采用同步处理,属于线程不安全操作。
正则表达式
详细内容可见:https://www.runoob.com/regexp/regexp-syntax.html