String类的概述和使用

本文深入探讨了Java中的String类,包括其构造方法、特点、常用方法以及面试常考的字符串比较问题。同时介绍了StringBuffer类,说明了其在字符串拼接中的优势,列举了构造方法和主要功能,并对比了String与StringBuffer的区别。此外,还提到了数组的排序和查找算法,如冒泡排序和二分查找,以及Arrays工具类的应用。
摘要由CSDN通过智能技术生成

第12天后半部分内容

String类的概述和使用

(1)多个字符组成的一串数据。
其实它可以和字符数组进行相互转换。
(2)构造方法:
A:public String()
B:public String(byte[] bytes)
C:public String(byte[] bytes,int offset,int length)
D:public String(char[] value)
E:public String(char[] value,int offset,int count)
F:public String(String original)
下面的这一个虽然不是构造方法,但是结果也是一个字符串对象
G:String s = “hello”;
(3)字符串的特点
A:字符串一旦被赋值,就不能改变。
注意:这里指的是字符串的内容不能改变,而不是引用不能改变。
B:字面值作为字符串对象和通过构造方法创建对象的不同
String s = new String(“hello”);和String s = "hello"的区别?
(4)字符串的面试题(看程序写结果)
A:==和equals()
==在复合数据类型比较时,比较的是内存中的存放地址

		String s1 = new String("hello");
		String s2 = new String("hello");
		System.out.println(s1 == s2);// false
		System.out.println(s1.equals(s2));// true

		String s3 = new String("hello");
		String s4 = "hello";
		System.out.println(s3 == s4);// false
		System.out.println(s3.equals(s4));// true

		String s5 = "hello";
		String s6 = "hello";
		System.out.println(s5 == s6);// true
		System.out.println(s5.equals(s6));// true
	B:字符串的拼接
		String s1 = "hello";
		String s2 = "world";
		String s3 = "helloworld";
		System.out.println(s3 == s1 + s2);// false
		System.out.println(s3.equals((s1 + s2)));// true

		System.out.println(s3 == "hello" + "world");// false 这个我们错了,应该是true
		System.out.println(s3.equals("hello" + "world"));// true
(5)字符串的功能
	A:判断功能
		boolean equals(Object obj)
		boolean equalsIgnoreCase(String str)
		boolean contains(String str)
		boolean startsWith(String str)
		boolean endsWith(String str)
		boolean isEmpty()
	B:获取功能
		int length()
		char charAt(int index)
		int indexOf(int ch)
		int indexOf(String str)
		int indexOf(int ch,int fromIndex)
		int indexOf(String str,int fromIndex)
		String substring(int start)
		String substring(int start,int end)
	C:转换功能
		byte[] getBytes()
		char[] toCharArray()
		static String valueOf(char[] chs)
		static String valueOf(int i)
		String toLowerCase()
		String toUpperCase()
		String concat(String str)
	D:其他功能
		a:替换功能 
			String replace(char old,char new)
			String replace(String old,String new)
		b:去空格功能
			String trim()
		c:按字典比较功能
			int compareTo(String str)
			int compareToIgnoreCase(String str) 
(6)字符串的案例
	A:模拟用户登录
	B:字符串遍历
	C:统计字符串中大写,小写及数字字符的个数
	D:把字符串的首字母转成大写,其他小写
	E:把int数组拼接成一个指定格式的字符串
	F:字符串反转
	G:统计大串中小串出现的次数

13天内容到374集

1:StringBuffer(掌握)
(1)用字符串做拼接,比较耗时并且也耗内存,而这种拼接操作又是比较常见的,为了解决这个问题,Java就提供了
一个字符串缓冲区类。StringBuffer供我们使用。
(2)StringBuffer的构造方法
A:StringBuffer()
B:StringBuffer(int size)
C:StringBuffer(String str)
(3)StringBuffer的常见功能
A:添加功能
B:删除功能
C:替换功能
D:反转功能
E:截取功能(注意这个返回值)
(4)StringBuffer的练习(做一遍)
A:String和StringBuffer相互转换
String – StringBuffer
构造方法
StringBuffer – String
toString()方法
B:字符串的拼接
C:把字符串反转
D:判断一个字符串是否对称
(5)面试题
小细节:
StringBuffer:同步的,数据安全,效率低。
StringBuilder:不同步的,数据不安全,效率高。
A:String,StringBuffer,StringBuilder的区别
B:StringBuffer和数组的区别?
(6)注意的问题:
String作为形式参数,StringBuffer作为形式参数。

2:数组高级以及Arrays(掌握)
(1)排序
A:冒泡排序
相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处。同理,其他的元素就可以排好。

public static void bubbleSort(int[] arr) {
		for(int x=0; x<arr.length-1; x++) {
			for(int y=0; y<arr.length-1-x; y++) {
				if(arr[y] > arr[y+1]) {
					int temp = arr[y];
					arr[y] = arr[y+1];
					arr[y+1] = temp;
				}
			}
		}
	}
			

B:选择排序
把0索引的元素,和索引1以后的元素都进行比较,第一次完毕,最小值出现在了0索引。同理,其他的元素就可以排好。

public static void selectSort(int[] arr) {
			for(int x=0; x<arr.length-1; x++) {
				for(int y=x+1; y<arr.length; y++) {
					if(arr[y] < arr[x]) {
						int temp = arr[x];
						arr[x] = arr[y];
						arr[y] = temp;
					}
				}
			}
		}
(2)查找
	A:基本查找
		针对数组无序的情况
	


public static int getIndex(int[] arr,int value) {
	int index = -1;
	
	for(int x=0; x<arr.length; x++) {
		if(arr[x] == value) {
			index = x;
			break;
		}
	}
	
	return index;
}
	B:二分查找(折半查找)
		针对数组有序的情况(千万不要先排序,在查找)
public static int binarySearch(int[] arr,int value) {
				int min = 0;
				int max = arr.length-1;
				int mid = (min+max)/2;
				
				while(arr[mid] != value) {
					if(arr[mid] > value) {
						max = mid - 1;
					}else if(arr[mid] < value) {
						min = mid + 1;
					}
					
					if(min > max) {
						return -1;
					}
					
					mid = (min+max)/2;
				}
				
				return mid;
			}
(3)Arrays工具类
	A:是针对数组进行操作的工具类。包括排序和查找等功能。
	B:要掌握的方法
		把数组转成字符串:
		排序:
		二分查找:
(5)把字符串中的字符进行排序
	举例:
		"edacbgf"
		得到结果
		"abcdefg"

3:Integer
(1)为了让基本类型的数据进行更多的操作,Java就为每种基本类型提供了对应的包装类类型
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
(2)Integer的构造方法
A:Integer i = new Integer(100);
B:Integer i = new Integer(“100”);
注意:这里的字符串必须是由数字字符组成
(3)String和int的相互转换
A:String – int
Integer.parseInt(“100”);
B:int – String
String.valueOf(100);

上课笔记

this关键字
(1)this:是当前类的对象引用,简单的记,它就代表当前类的一个对象
​ 记住:哪个对象调用方法,该方法内部的this就代表那个对象
​(2)this的应用场景:
​ 解决了局部变量隐藏成员变量的问题
this三大作用:
this调用属性、调用方法、用this表示当前对象
this用法:
1.普通直接引用
2.形参与成员名字的重复,用this区分
3.引用构造函数

StringBuffer: 线程安全的可变字符串

1.我们如果对字符串进行拼接操作,每次拼接都会构成一个新的String对象,既耗时,又浪费空间

StringBuffer可以解决这个问题。

2.StringBuffer和String的区别?

前者长度和内容可变,后者不可变
3.StringBuffer的构造方法:
public StringBuffer():无参构造方法 public StringBuffer(int capacity):指定容量的字符串缓冲区对象
public StringBuffer(String str)指定字符串内容的字符串缓冲区对象

4.StringBuffer()的方法:
public int Capacity();返回当前容量 理论值
public int length() 返回长度(字符数) 实际值

StringBuffer的添加功能:
public StringBuffer append(String str)
可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
public StringBuffer insert(int offset,String str)
在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
StringBuffer的删除功能:

public StringBuffer deleteCharAt(int index)

删除指定位置字符,并返回本身

public StringBuffer delete(int start,int end)

删除从指定位置开始,并从指定位置结束字符,并返回本身

StringBuffer的截取功能:
注意返回值类型不是StringBuffer

public String substring(int start)

public String substring(int start,int end)

StringBuffer的替换功能:

public StringBuffer replace(int start,int end,String str),从start到end用str替换

StringBuffer的反转功能:
public StringBuffer reverse()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值