黑马程序员_java基础--集合框架(1)

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

String类:字符串(重点)
(1)多个字符组成的一个序列,叫字符串。
  生活中很多数据的描述都采用的是字符串的。而且我们还会对其进行操作。
  所以,java就提供了这样的一个类供我们使用。


(2)创建字符串对象
A:String():无参构造
**举例:
 String s = new String();
 s = "hello";
 sop(s);


B:String(byte[] bys):传一个字节数组作为参数 *****
**举例
 byte[] bys = {97,98,99,100,101};
 String s = new String(bys);
 sop(s);


C:String(byte[] bys,int index,int length):把字节数组的一部分转换成一个字符串 *****
**举例
 byte[] bys = {97,98,99,100,101};
 String s = new String(bys,1,2);
 sop(s);


D:String(char[] chs):传一个字符数组作为参数 *****
**举例
 char[] chs = {'a','b','c','d','e'};
 String s = new String(chs);
 sop(s);


E:String(char[] chs,int index,int length):把字符数组的一部分转换成一个字符串 *****
**举例
 char[] chs = {'a','b','c','d','e'};
 String s = new String(chs,1,2);
 sop(s);


F:String(String str):把一个字符串传递过来作为参数
 char[] chs = {'a','b','c','d','e'};
 String ss = new String(s);
 sop(ss);


G:直接把字符串常量赋值给字符串引用对象(最常用) *****
**举例
 String s = "hello";
 sop(s);

package it.heima.com.string;

public class StringDemo {
	public static void main(String[] args){
		String str = new String("abc");
		String str1 = "abc";	//s1是一个类类型变量, "abc"是一个对象。
								//字符串最大特点:一旦被初始化就不可以被改变。
		//s1和s2有什么区别?
		//s1在内存中有一个对象。
		//s2在内存中有两个对象。
		
		System.out.println(str == str1);
		System.out.println(str.equals(str1));	//String类复写了Object类中equals方法,
												//该方法用于判断字符串是否相同。


	}
}



(3)面试题
A:请问String s = new String("hello");创建了几个对象。
 两个。一个"hello"字符串对象,在方法区的常量池;一个s对象,在栈内存。


B:请写出下面的结果
String s1 = new String("abc");
Strign s2 = new String("abc");
String s3 = "abc";
String s4 = "abc";


sop(s1==s2);  //false
sop(s1==s3);  //false

sop(s3==s4);  //true


C:字符串对象一旦被创建就不能被改变。

指的是字符串常量值不改变。


(4)字符串中各种功能的方法
A:判断
**** boolean equals(Object anObject):判断两个字符串的内容是否相同,复写了Object的方法
**** boolean equalsIgnoreCase(String anotherString):判断两个字符串的内容是否相同,
不区分大小写
**** boolean contains(String s):判断一个字符串中是否包含另一个字符串
注意:判断字符串是否包含特殊字符.直接表示为str.contains(".")
boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束
boolean startsWith(String suffix):测试此字符串是否以指定的前缀开始

boolean isEmpty():测试字符串是否为空


B:获取
***** int length():返回此字符串的长度
***** char charAt(int index):返回指定索引处的 char值
***** int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。 
int indexOf(int ch, int fromIndex):返回在此字符串中第一次出现指定字符处的索引,
  从指定的索引开始搜索。 
int indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引。 
int indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次
出现处的索引,从指定的索引开始。 
*** int lastIndexOf(int ch):返回指定字符在此字符串中最后一次出现处的索引。 
int lastIndexOf(int ch, int fromIndex) 
返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。 
int lastIndexOf(String str) 
返回指定子字符串在此字符串中最右边出现处的索引。 
int lastIndexOf(String str, int fromIndex) 
返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。 
***** String substring(int beginIndex) (注意:该方法substring的String是小写!!!)
返回一个新的字符串,它是此字符串的一个子字符串。 
String substring(int beginIndex, int endIndex) (注意该方法的String是小写!!!)

返回一个新字符串,它是此字符串的一个子字符串,包含头不包含尾。 


C:转换
***** byte[] getBytes():(很常用!)从字符串到字节数组的方法
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 
将字符从此字符串复制到目标字符数组。 
***** char[] toCharArray():(很常用!)从字符串到字符数组的方法
**** static String copyValueOf(char[] data) 
返回指定数组中表示该字符序列的 String。 
static String copyValueOf(char[] data, int offset, int count) 
返回指定数组中表示该字符序列的 String。 
***** static String valueOf(数据类型):把该数据类型的数据转换成字符串。
*** String toLowerCase():把字符串转换成小写
String toUpperCase():把字符串转换成大写
*** 字符串的连接

String concat(String str):将指定字符串连接到此字符串的结尾。


D:替换
String replace(char oldChar, char newChar):用新字符替换旧字符(替换所有)
String replace(String target, String replacement):用新的子串换旧串
E:分割
String[] split(String regex):根据指定的字符串把一个字符串分割成一个字符串数组
F:
String trim():去除字符串的前后空格
G:
int compareTo(String anotherString) 
按字典顺序比较两个字符串。 
int compareToIgnoreCase(String str) 
按字典顺序比较两个字符串,不考虑大小写。 

package it.heima.com.string;

public class StringMethodDemo {
	public static void main(String[] args){
		//method_get();
		//method_is();
		//method_trans();
		//method_replace();
		//method_split();
		//method_sub();
		method_trim();
	}
	
	public static void method_get(){
		String str = "hgjahgjahk";
		//获取字符串长度
		sop(str.length());
		//根据索引获取字符
		sop(str.charAt(3));
		//根据字符获取索引
		sop(str.indexOf('a'));
		//反向索引一个字符出现位置。
		sop(str.lastIndexOf('g'));

	}
	
	public static void method_is(){
		String str = "HelloDemo.java";
		String str1 = new String("hellodemo.java");
		//	2.1 字符串中是否包含某一个子串。
		sop("字符串str中是否包含子串Hello:"+str.contains("Hello"));
		//	2.2 字符中是否有内容。
		sop("字符串str是否为空:"+str.isEmpty());
		//	2.3 字符串是否是以指定内容开头。
		sop("字符串str是否是以Hello开头:"+str.startsWith("Hello"));
		//	2.4 字符串是否是以指定内容结尾。
		sop("字符串str是否是以.java结尾:"+str.startsWith("Hello"));
		//	2.5 判断字符串内容是否相同。复写了Object类中的equals方法。
		sop("判断字符串str和str1的内容是否相同:"+str.equals(str1));
		//	2.6 判断内容是否相同,并忽略大小写。
		sop("判断字符串str和str1的内容是否相同,并忽略大小写:"+str.equalsIgnoreCase(str1));

	}
	
	public static void method_trans(){
		char[] arr = {'a','b','c','d','e','f'};
		//	 将字符数组转成字符串
		String str = new String(arr);
		sop("str= "+str);
		//  将字符串转成字符数组
		char[] ch = str.toCharArray();
		System.out.print("ch = [");
		
		for(int i=0; i<ch.length; i++){
			if(i==ch.length-1)
				System.out.print(ch[i]);
			else
				System.out.print(ch[i]+",");
		}
		sop("]");
		
		//	 将字符串转成字节数组
		byte[] b = str.getBytes();
		System.out.print("b = [");
		
		for(int i=0; i<b.length; i++){
			if(i==b.length-1)
				System.out.print(b[i]);
			else
				System.out.print(b[i]+",");
		}
		sop("]");
		//	 将字节数组转成字符串
		String str2 = new String(b);
		sop("str2= "+str2);
		//	 将基本数据类型转成字符串
		sop(3+"");
		sop(String.valueOf(4));
		sop(String.valueOf(4.0));
		//特殊:字符串和字节数组在转换过程中,是可以指定编码表的。
		 
	}
	
	public static void method_replace(){
		//替换
		String str = "Hello java !";
		String str1 = str.replace("java", "world");
		sop("str= "+str);
		sop("str1= "+str1);
		//如果要替换的字符不存在,返回的还是原串。
	}
	public static void method_sub(){
		//获取子串
		String s = "abcdef";
		sop(s.substring(2));//从指定位置开始到结尾。如果角标不存在,会出现字符串角标越界异常。
		sop(s.substring(2,4));//包含头,不包含尾。s.substring(0,s.length());
	}
	
	public static void  method_split(){
		//切割
		String s = "zhagnsa,lisi,wangwu";
		String[] arr  = s.split(",");
		for(int x = 0; x<arr.length; x++){
			sop(arr[x]);
		}
	}

	public static void method_trim(){
		//将字符串两端的多个空格去除
		String str = "  kahgolajh   ";
		sop(str.trim());
		sop(str);
	}

	
	public static void sop(Object obj){
		System.out.println(obj);
	}
}




StringBuffer
(1)字符串的缓冲区,是一个容器。
(2)它和String的区别
a,它是缓冲区可变长度的。
b,可以直接操作多个数据类型

c,最终会通过toString方法变成字符串

(3)构造方法
StringBuffer() 构造一个其中不带字符的字符串缓冲区,初始容量为 16 个字符。
StringBuffer(int num) 构造一个不带字符,但具有指定初始容量的字符串缓冲区。
StringBuffer(String str) 构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容。
(4)常用方法

1,存储。
	StringBuffer append():将指定数据作为参数添加到已有数据结尾处。
	StringBuffer insert(index,数据):可以将数据插入到指定index位置。
2,删除。
	StringBuffer delete(start,end):删除缓冲区中的数据,包含start,不包含end。
	StringBuffer deleteCharAt(index):删除指定位置的字符。	
3,获取。
	char charAt(int index) 
	int indexOf(String str) 
	int lastIndexOf(String str) 
	int length() 
	String substring(int start, int end) 
4,修改。
	StringBuffer replace(start,end,string);
	void setCharAt(int index, char ch) ;
5,反转。
	StringBuffer reverse();
 6,
	将缓冲区中指定数据存储到指定字符数组中。
	void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 
练习:

package it.heima.com.string;

public class StringBufferDemo {
	public static void main(String[] args){
		//method_update();
		//method_del();
		//method_add();
		method_get();
		StringBuffer sb = new StringBuffer("abcdefg");
		sop(sb.reverse()); // 反转
		
	}
	
	public static void method_update(){
		StringBuffer sb = new StringBuffer("abcdefg");
		sb.replace(0,2,"aaa");	//含头不含尾
		sop(sb);
		
		sb.setCharAt(0, 'b');
		sop(sb);
		
	}
	
	public static void method_del(){
		StringBuffer sb = new StringBuffer("abcdefg");
		sop("没删除之前的sb为:"+sb);
		//sb.delete(0,sb.length());   //清除缓冲区
		sb.delete(0, 1);
		sop("删除后的sb = "+sb+";");
		
		sb.deleteCharAt(2);
		sop(sb);
	}
	
	public static void method_add(){
		StringBuffer sb = new StringBuffer();
		sb.append("abc").append(true).append(3.0);
		sop(sb);
		
		StringBuffer sb1 = sb.append(3.0);
		sop("sb == sb1:"+(sb == sb1));
		
		sb.insert(1, "qq");
		sop(sb.toString());
	}
	
	public static void method_get(){
		StringBuffer sb = new StringBuffer("abcdefg");
		sop(sb.charAt(2));
		sop(sb.indexOf("cd"));
		sop(sb.lastIndexOf("ef"));
		sop("长度为:"+sb.length());
		sop(sb.substring(1, 4));//含头不含尾
	}
	public static void sop(Object obj){
		System.out.println(obj);
	}
}


(5)字符串和StringBuffer的转换
String-->StringBuffer通过构造:
如:StringBuffer sb = new StringBuffer(String str)
StringBuffer--String通过toString方法 
如:StringBuffer sb = new StringBuffer();
  sb.toString();


StringBuilder
和StringBuffer的功能是一样的,但是有区别:
StringBuffer(JDK1.0)是线程安全的。
StringBuilder(JDK1.5)不保证线程安全。

一般来说,我们写的程序都是单线程的,所以,用StringBuilder,效率高。

JDK版本的升级原则:
A:提高效率
B:提高安全性
C:简化书写


基本数据类型的对象包装类
(1)为了更方便的操作每个基本数据类型,java对其提供了很多的属性和方法供我们使用。
(2)用途:
**将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能操作该数据。
**常用的操作之一:用于基本数据类型与字符串之间的转换。
A:方便操作
B:用于和字符串进行相互转换
(3)基本数据类型和对象类型的对应

(4)构造方法


字段摘要:
static int MAX_VALUE 值为 2^31-1 的常量,它表示 int 类型能够表示的最大值         
static int MIN_VALUE  值为 -2^31 的常量,它表示 int 类型能够表示的最小值
static Class<Integer> TYPE 表示基本类型int的Class 实例
          
Integer(int value) 构造一个新分配的Integer对象,它表示指定的int值。
Inreger(String s) 注意:s必须是纯数字的字符串。否则会有异常NumberFormatException
                       
(5)几个常用的方法
Integer.toBinaryString();
以二进制(基数 2)无符号整数形式返回一个整数参数的字符串表示形式。
Integer.toOctalString();
以八进制(基数 8)无符号整数形式返回一个整数参数的字符串表示形式。
Integer.toHexString();
以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式。
static int Integer.parseInt(String s) 将字符串参数作为有符号的十进制整数进行解析,
字符串必须是int型范围内的数字字符串
static int Integer.parseInt(String s,int basic) 
使用第二个参数指定的基数,将字符串参数解析为有符号的整数.
字符串必须是int型范围内的数字字符串
short shortValue() 以short类型返回该Integer的值。          
int intValue() 以int类型返回该Integer的值。  
static Integer valueOf(int num) 返回一个表示指定的 int 值的 Integer 实例。
static Integer valueOf(String s) 返回保存指定的String的值的Integer对象。           
                static Integer valueOf(String s, int radix) 
返回一个Integer对象,该对象中保存了用第二个参数提供的基数进行
解析时从指定的String中提取的值。 


(6)类型转换
int -- Integer
int num = 20;
A:Integer i = new Integer(num);
B:Integer i = Integer.valueOf(num);
Integer -- int
Integer i = new Integer(20);
A:int num = i.intValue();

int -- String
int num = 20;
A:String s = String.valueOf(num);
B:String s = ""+num;
C:String s = Integer.toString(num);
String -- int
String s = "20";
A:int num = Integer.parseInt(s);
B:Integer i = new Integer(s);或者Integer i = Integer.valueOf(s);
 int num = i.intValue();

class IntegerDemo1 
{
	public static void main(String[] args) 
	{
		
//		Integer x = new Integer(4);
		Integer x = 4;//自动装箱。//new Integer(4)
		x = x/* x.intValue() */ + 2;//x+2:x 进行自动拆箱。变成成了int类型。和2进行加法运算。
					//再将和进行装箱赋给x。		

		Integer m = 128;
		Integer n = 128;

		sop("m==n:"+(m==n));

		Integer a = 127;
		Integer b = 127;

		sop("a==b:"+(a==b));//结果为true。因为a和b指向了同一个Integer对象。
						//因为当数值在byte范围内容,对于新特性,如果该数值已经存在,则不会再开辟新的空间。
	}

	public static void method()
	{
		Integer x = new Integer("123");

		Integer y = new Integer(123);

		sop("x==y:"+(x==y));
		sop("x.equals(y):"+x.equals(y));
	}

	public static void sop(String str)
	{
		System.out.println(str);
	}
	
}
注意:Integer多了一个值null,这样可能会出现空指针异常,要先对Integer判断,不为空再操作。

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值