前言
介绍Java数组和字符串的定义和使用。
目录
一、数组
(一)、数组概述
数组:相同类型的元素的集合。数组的地址空间是连续的,数组变量存放的是数组首元素的地址。数组便于查找元素,不利于元素的增删。只能存放小规模的数据,因为内存中很难找到一大块连续的空间来存放。
Java中的相同类型要分情况:
对于基本数据类型而言:各基本数据类型的数组不能存放其它需要强制类型转换的元素,如整形数组不能存放浮点型类型的元素,但可以放入短整型类型的元素。
对于引用数据类型的数组:各引用数据类型可以存放该类型本身的元素,还可以存放它的派生类型的元素。
一般来说,基本类型的元素不能放进引用数据类型的数组中,但是如果该数组是Object数组,或者是各基本数据类型对应的包装类,就可以存放基本数据类型,因为会自动装箱。
注意,数组本身是引用数据类型。
(二)、数组的使用
1.数组的声明和初始化
(1)、静态初始化
格式: 类型[ ] 数组名 = new 类型[ ]{元素1、元素2 ...};
或者 类型[ ] 数组名 = {元素1、元素2 ...};
但在传参时不能用第二种方式创建实参数组,如 fun( {1,2,3} ) 这种形式的传参是不行的。
(2)、动态初始化
格式:类型[ ] 数组名 = new 类型[元素个数];
public class Test01 {
public static void main(String[] args) {
//静态初始化
int[] a = new int[]{1,2,3,4,5,6};
int[] b = {1,2,3,4,5,6};
//动态初始化
//该元素个数可以用变量表示
Object[] c = new Object[10];
}
}
(3)、数组默认初始化值
元素类型 | 默认初始化值 |
整形 | 0 |
浮点型 | 0.0 |
字符型 | 0(编码)或'\u0000' |
布尔型 | false |
引用数据类型 | null |
与成员变量的默认初始值相同
2. 数组的长度
用数组的length属性获取数组的长度,也就是数组中的元素个数。
public class Test01 {
public static void main(String[] args) {
int[] a = new int[]{1,2,3,4,5,6};
System.out.println(a.length);//6
}
}
3.数组元素访问和遍历数组
使用下标访问数组元素,使用循环遍历数组。
public class Test01 {
public static void main(String[] args) {
int[] a = new int[]{1,2,3,4,5,6};
//使用循环遍历数组
for(int i=0; i<a.length; i++){
//使用下标访问数组元素
System.out.println(a[i]);
}
}
}
4.数组的扩容与拷贝
Java数组的扩容只能是再重新开辟一个更大的空间,然后将原数组中的元素拷贝过去。
public class Test01 {
public static void main(String[] args) {
int[] a = new int[]{1,2,3,4,5,6};
//数组的扩容只能是将现有元素拷贝到一个更大的数组之中
int[] b = new int[20];
//数组的拷贝用到System.arraycopy()方法
//该方法的第一个参数是新数组的,第二个参数是拷贝到新数组的起始位置,
//第三个参数是原数组,第四个参数是要拷贝的元素的起始位置,第五个参数是拷贝的元素个数
System.arraycopy(b,0,a,0,a.length);
}
}
(三)、二维数组
二维数组可以理解为是每一个元素是一维数组的特殊的一维数组。也就是说,二维数组的每一个元素都是放的一维数组的首元素地址,或者为null。
public class Test02 {
public static void main(String[] args) {
//二维数组的声明和初始化
//静态初始化
int[][] a = new int[][]{{1,2,3},{4,5,6}};
int[][] b = {{1,2,3},{4,5,6}};
//动态初始化
int[][] c = new int[2][3];
//动态初始化可以没有列,也就是没有第二个参数,但是必须要有第一个参数
int[][] d = new int[2][];
//int[][] e = new int[][3];会报错
//调用元素用到两个下标
System.out.println(a[0][1]);//2
//如果是动态初始化需要先创建该行对应的一维数组,再去访问元素
c[0]=new int[]{1,2,3};//注意,这里必须用到new运算符,不能用{1,2,3}的方式直接赋值
System.out.println(c[0][2]);//3
}
}
注意,在给动态创建的二维数组的元素创建一维数组时,必须用到new运算符,不能用{元素1,元素2 ... }的方式直接赋值,否则编译器会报错。
(四)、数组工具类
所谓的工具类,就是将一些常用方法聚集在一起形成的特殊类。比如数组工具类(Arrays),数学工具类(Math),集合工具类(Collections)等等。工具类都在java.util包下
常见的数组工具类中的方法有:
1.boolean equals(数组1,数组2)
该方法用来判断两个数组是否相等,当然,如果数组中的元素是自定义类型的,记得要重写自定义类型的equals方法。
2.String toString(数组)
直接输出该数组
3.void fill(数组,值)
对数组用该值填充
4.void sort(数组)
数组排序
5.int binarySearch(数组,值)
利于二分法查找某个元素,找到返回该元素下标,否则返回负数
import java.util.Arrays;
public class Test03 {
public static void main(String[] args) {
int[] a = {1,2,3,4,5,6};
int[] b = new int[]{1,2,3,4,5,6};
Object[] o = new Object[]{new Object(),new Object()};
//1.equals方法判断两个数组是否相等
System.out.println(Arrays.equals(a,b));//true
//2.toString方法直接输出该数组
System.out.println(Arrays.toString(a));//[1, 2, 3, 4, 5, 6]
System.out.println(Arrays.toString(o));//[java.lang.Object@4554617c, java.lang.Object@74a14482]
//3.fill方法填充数组
Arrays.fill(b,1);
System.out.println(Arrays.toString(b));//[1, 1, 1, 1, 1, 1]
//4.sort方法对数组排序
int[] c = {6,4,7,5,9,8};
Arrays.sort(c);
System.out.println(Arrays.toString(c));//[4, 5, 6, 7, 8, 9]
//binarySearch方法查找元素
System.out.println(Arrays.binarySearch(a,10));//-7
}
二、字符串
(一)、字符串概述
Java中的字符串是引用数据类型,存储在方法区的常量池当中,一旦被创建就不能被修改。事实上,Java利用了char数组来完成字符串的创建,且该数组被final修饰。
(二)、字符串的构造方法
1.直接用双引号创建
String s1 = "aaa";
2.用byte数组
使用 String( byte[] )构造方法或者 String( byte[],开始下标,元素个数)构造方法。
3.用char数组
使用String( char[] )构造方法或者String( char[],开始下标,元素个数)构造方法。
public class Test04{
public static void main(String[] args) {
//直接使用双引号创建字符串
String s1 = "aaa";
//使用byte数组创建字符串
byte[] b =new byte[]{97,98,99};
String s2 = new String(b);
System.out.println(s2);//abc
String s3 = new String(b,0,2);
System.out.println(s3);//ab
//使用char数组创建字符串
char[] c = new char[]{'d','e','f'};
String s4 = new String(c);
System.out.println(s4);//def
String s5 = new String(c,1,2);
System.out.println(s5);//ef
}
使用第一种和第二、三中方法的区别是,第二、三中方法会在堆区开辟空间,创建出来的字符串变量指向该堆区空间,存放该空间的地址,该空间内再存放指向方法区字符串地址的变量 。而第一种方法直接存放方法区字符串的内存地址。
(三)、字符串的常用方法
以下方法没写访问控制权限修饰符的都是使用的public修饰
1.char charAt(int index)
返回指定下标的字符串
public static void main(String[] args) {
String s1 = "hello";
char i = s1.charAt(1);
System.out.println(i);//e
}
2.int compareTo(String anotherString)
两个字符串比较大小
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hea";
System.out.println(s1.compareTo(s2));//11
//结果大于0表示s1大于s2,小于0表示s1小于s2,等于0表示s1等于s2
}
3.boolean contains(CharSequence subString)
判断是否包含子字符串
public static void main(String[] args) {
String s1 = "hello";
String s2 = "ll";
System.out.println(s1.contains(s2));//true
}
4.boolean endsWith(String suffix)和startsWith( )
判断该字符串是否以某个后缀结束
public static void main(String[] args) {
String s1 = "hello";
String s2 = "lo";
System.out.println(s1.endsWith(s2));//true
System.out.println(s1.startsWith("h"));//true
}
5.boolean equalsIgnoreCase(String anotherString)
判断字符串是否相等,忽略大小写
public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO";
System.out.println(s1.equalsIgnoreCase(s2));//true
}
6.byte[] getBytes( )
将字符串转化为byte数组
public static void main(String[] args) {
String s1 = "hello";
byte[] b = s1.getBytes();
System.out.println(Arrays.toString(b));//[104, 101, 108, 108, 111]
}
7.char[] toCharArray( )
将字符串转化为char数组
public static void main(String[] args) {
String s1 = "hello";
char[] c = s1.toCharArray();
System.out.println(Arrays.toString(c));//[h, e, l, l, o]
}
8.int indexOf( )和lastIndexOf( )
获取某个子字符串在当前字符串中第一次(最后一次出现的位置),负数代表不存在该子字符串
public static void main(String[] args) {
String s1 = "helloabclodf";
int i = s1.indexOf("lo");
int j = s1.indexOf("aa");
System.out.println(i);//3
System.out.println(j);//-1,代表没有该子字符串
}
9.boolean isEmpty( )
判断该字符串是否为空
public static void main(String[] args) {
String s1 = "";
System.out.println(s1.isEmpty());//true
}
10.int length( )
获取字符串长度。注意,数组获取长度用的是它的length属性,而字符串用的是length方法。
public static void main(String[] args) {
String s1 = "hello";
System.out.println(s1.length());//5
}
11.String replace(CharSequence target,CharSequence replacement)
将所有的子字符串替换,该方法生成一个新的字符串
public static void main(String[] args) {
String s1 = "hello world!";
//注意,replace方法生成一个新的字符串
String s2 = s1.replace("world","China");
System.out.println(s2);//hello China!
}
12. String[] split(String regex)
以指定元素拆分字符串
public static void main(String[] args) {
String s1 = "apple]banana]grape]watermelon]lemon]strawberry";
String[] s2 = s1.split("]");
System.out.println(Arrays.toString(s2));//[apple, banana, grape, watermelon, lemon, strawberry]
}
13.String substring(int beginIndex)或者substring(int beginIndex,int endIndex)
返回指定下标内的字符串,使用第二种方法时遵循左闭右开的原则
public static void main(String[] args) {
String s1 = "apple]banana]grape]watermelon]lemon]strawberry";
String s2 = s1.substring(0,5);//左闭右开,得到apple
System.out.println(s2);//apple
}
14.String toLowerCase( )和toUpperCase( )
对字符串进行大小写转换
public static void main(String[] args) {
String s1 = "Hello";
String s2 = s1.toLowerCase();
System.out.println(s2);//hello
String s3 = s1.toUpperCase();
System.out.println(s3);//HELLO
}
15.String trim( )
去除字符串前后空白
public static void main(String[] args) {
String s1 = " Hello ";
String s2 = s1.trim();
System.out.println(s2);//Hello
}
16.static String valueOf( )
将不是字符串的对象转化为字符串,实际上调用了对象的toString()方法。注意,该方法是静态方法,直接用类名调用
public static void main(String[] args) {
int i = 10;
Object o = new Object();
String s1 = String.valueOf(i);
String s2 = String.valueOf(o);
System.out.println(s1);//10
System.out.println(s2);//java.lang.Object@4554617c
}
(四)、字符串拼接
传统的使用连接符(加号)的方式将会产生许多新的中间字符串,浪费大量常量池空间。因此需要不产生中间字符串的方式,即使用StringBuffer进行字符串拼接。原理是字符串的char数组使用了final限定,而StringBuffer的没有。
public static void main(String[] args) {
//StringBuffer的无参构造方法会创建一个默认16个元素的char数组
//可以传入参数指定空间大小
StringBuffer sbf = new StringBuffer(20);
//追加元素用append方法
sbf.append("hello ");
sbf.append("China");
System.out.println(sbf);//hello China
}
StringBuilder和StringBuffer使用方法和原理类似,但StringBuffer中的方法是线程安全的,能保证数据的安全性,因此StringBuffer更常用。
如有错误,希望能批评指正,不胜感激。