字符串和集合

字符串和集合

1. 字符串类型

字符串是对于程序员而言非常重要的信息载体,稳定,兼容,数据量大,可以自定义内容信息方式…

1.1 字符串存储问题
/*
 * 字符串存储和比较
 */
public class Demo1 {
	public static void main(String[] args) {
		String str1 = "郑州今天大雾";
		String str2 = "郑州今天大雾";
		String str3 = new String("郑州今天大雾");
		String str4 = new String(str1);
		
		
		System.out.println("str1 == str2 : " + (str1 == str2)); // T 
		System.out.println("str2 == str3 : " + (str2 == str3)); // F 
		System.out.println("str3 == str4 : " + (str3 == str4)); // F 
		System.out.println("str4 == str1 : " + (str4 == str1)); // F 
	}
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hehddTE7-1668214474874)(img/01-字符串内存分析.png)]

/*
* 【注意】
* 		【强制要求】字符串比较是否一致,必须使用 equals 方法 
*/
System.out.println("str1.equals(str2) : " + str1.equals(str2));
System.out.println("str2.equals(str3) : " + str2.equals(str3));
System.out.println("str3.equals(str4) : " + str3.equals(str4));
System.out.println("str4.equals(str1) : " + str4.equals(str1));

​ API 学会学习 API 文档,在工作中给你一份 API 文档,对于你的伙伴,你的上司而言,他们就认为当前这个技能你已经掌握

1.2 字符串操作相关方法
1.2.1 字符串获取方法
int length();
	字符串有效字符个数
char charAt(int index);
	获取当前字符串指定下标对应的字符数据。
int indexOf(int ch);
	查询指定字符在当前字符串中第一次出现的下标位置,如果没有返回 -1
int lastIndexOf(int ch);	
	查询指定字符在当前字符串中最后一次出现的下标位置,如果没有返回 -1
int indexOf(String str);
	查询指定字符串在当前字符串中第一次出现的下标位置,如果没有返回 -1
int lastIndexOf(String str);	
	查询指定字符串在当前字符串中最后一次出现的下标位置,如果没有返回 -1

补充知识点

int ch ==> indexOf 方法对应的参数格式,之所以用 int 类型参数是考虑到字符有编码值情况,同时 ch 参数变量名称告知/提示程序员当前的实际类型,参与代码运行的类型是字符类型

package com.qfedu.a_string;

public class Demo2 {
	public static void main(String[] args) {
		String str = "ABCDEFGABCDEFG";
		
		System.out.println("length:" + str.length());
		System.out.println("charAt:" + str.charAt(5));
		System.out.println("charAt:" + str.charAt(3));
		System.out.println();
		
		/*
		 * java.lang.StringIndexOutOfBoundsException: String index out of range: 100
		 * 字符串下标越界
		 */
		// System.out.println("charAt:" + str.charAt(100));
		
		System.out.println("indexOf:" + str.indexOf('A'));
		System.out.println("indexOf:" + str.indexOf(65));
		System.out.println("indexOf:" + str.indexOf('B'));
		System.out.println("indexOf:" + str.indexOf(66));
		
		System.out.println();
		System.out.println("indexOf:" + str.lastIndexOf('A'));
		System.out.println("indexOf:" + str.lastIndexOf(65));
		System.out.println("indexOf:" + str.lastIndexOf('B'));
		System.out.println("indexOf:" + str.lastIndexOf(66));
		
		System.out.println();
		System.out.println("indexOf:" + str.indexOf('Z'));
		System.out.println("indexOf:" + str.lastIndexOf('Z'));
		
		System.out.println();
		System.out.println("indexOf:" + str.indexOf("BCD"));
		System.out.println("indexOf:" + str.indexOf("bcd"));
		System.out.println("indexOf:" + str.lastIndexOf("BCD"));
		System.out.println("indexOf:" + str.lastIndexOf("bcd"));
		
	}
}
1.2.2 字符串判断方法
boolean endsWith(String str);
	判断当前调用方法字符串是否以参数字符串结尾
boolean startsWith(String str);
	判断当前调用方法字符串是否以参数字符串开头
boolean isEmpty();
	判断字符串是否为空,JDK1.6 之后版本空字符串为 ""
boolean contains(String str);
	判断当前调用方法字符串是否包含参数目标字符串
boolean equals(Object obj) // Override equals method in Object 重写 Object 方法
    判断当前调用方法字符串和参数字符串是否内容一致
boolean equalsIgnoreCase(String str);
	判断当前调用方法字符串和参数字符串,不区分大小写比较字符串内容

空字符串 “” 有什么用:

Java 中任意数据类型想要变成字符串类型都可以 + “”

String str = 3 + “”; str ==> “3”

String str = ‘A’ + “”; str ==> “A”

String str = true + “”; str ==> “true”

String str = false + “”; str ==> “false”

package com.qfedu.a_string;

public class Demo3 {
	public static void main(String[] args) {
		String str = "ABCDEFG";
		
		System.out.println("endsWith:" + str.endsWith("FG"));
		System.out.println("endsWith:" + str.endsWith("FF"));
	
		System.out.println();
		System.out.println("startsWith:" + str.startsWith("AB"));
		System.out.println("startsWith:" + str.startsWith("AA"));
		
		System.out.println();
		System.out.println("isEmpty:" + str.isEmpty());
		System.out.println("isEmpty:" + "".isEmpty());
		
		System.out.println();
		System.out.println("contains:" + str.contains("AA"));
		System.out.println("contains:" + str.contains("BC"));
	
		System.out.println();
		System.out.println("equals:" + str.equals("ABCDEFG")); 
		System.out.println("equals:" + "ABCDEFG".equals(str));  // 推荐常量在前调用方法,避免错误
		
		System.out.println();
		System.out.println("equalsIgnoreCase:" + str.equalsIgnoreCase("abcdefg")); 
		System.out.println("equalsIgnoreCase:" + "abcdefg".equalsIgnoreCase(str)); 
		
	}
}
1.2.3 字符串转换方法
String(char[] arr);
	【构造方法】将一个字符数组转换为字符串
String(char[] arr, int offset, int length);
	【构造方法】将一个字符数组从指定下标 offset 开始,计数字符个数 length,转换为一个字符串
static String valueOf(char[] arr);
	【静态方法】将一个字符数组转换为字符串
static String valueOf(char[] arr, int offset, int length);
	【静态方法】将一个字符数组从指定下标 offset 开始,计数字符个数 length,转换为一个字符串
char[] toCharArray();
	字符串数据内容转字符数组
package com.qfedu.a_string;

import java.util.Arrays;

public class Demo4 {
	public static void main(String[] args) {
		String str = "abcdefg";
		
		char[] array = str.toCharArray();
		System.out.println(Arrays.toString(array));
		
		System.out.println();
		
		String str2 = new String(array);
		String str3 = new String(array, 1, 3);
		System.out.println(str2);
		System.out.println(str3);
		
		System.out.println();
		String str4 = String.valueOf(array);
		String str5 = String.valueOf(array, 1, 3);
		System.out.println(str4);
		System.out.println(str5);
	}
}
1.2.4 字符串其他方法

【注意】字符串是一个常量,字符串执行方法原串不变

一下所有方法调用方法的字符串都不变,所有的结果都是通过方法返回值新字符串得到

String replace(char oldChar, char newChar);
	调用方法字符串,指定的 oldChar 字符替换为 newChar 字符串内容,最终替换结果通过方法返回值返回一个新的字符串
       
String[] split(String str);
	调用方法字符串,根据指定的参数字符船进行分割操作,得到的结构是一个分割之后的字符串数组。
     例如:
        	String str = "name=张三&age=16&gender=f";
			//当前字符串是一串数据信息,可以转换成 JavaBean 对象,当前字符串是有规律和规则
			//信息采用表达方式为 key=value 模式/键值对模式 同时不同的信息直接使用 & 分割
			String[] arr = str.split("&");
			// arr = {"name=张三", "age=16" ,"gender=f"};
	
String substring(int begin);
	调用方法字符串,从指定下标 begin 位置开始到字符串末尾,截取子字符串。
String substring(int begin, int end);
	调用方法字符串,从指定下标 begin 位置开始字到指定下标end 结束,截取子字符串。 【要头不要尾】
        
  String toUpperCase();
		调用方法字符串所有的英文字母全部转大写,结果通过返回值新字符串得到
	String toLowerCase();
		调用方法字符串所有的英文字母全部转小写,结果通过返回值新字符串得到
  String trim();
		删除调用方法字符串两端的无效空格,结果通过返回值新字符串得到
public class Demo5 {
	public static void main(String[] args) {
		String str = " abcaABCefghil ";
		String str1 = str.replace('a', 'A');
		
		System.out.println(str1);
		System.out.println();
		String str2 = "name=李四&age=18&gender=f";
		String[] arr = str.split("&");
		System.out.println(Arrays.toString(arr));
		System.out.println();
		String str3 = str.substring(2);
		String str4 = str.substring(4, 6);
		System.out.println(str4);
		System.out.println(str3);
		System.out.println();
		String str5 = str.toLowerCase();
		String str6 = str.toUpperCase();
		String str7 = str.trim();
		System.out.println(str5);
		System.out.println(str6);
		System.out.println(str7);
	}

}
2.集合
2.1为什么要使用集合
目前而言,对应数据量多个/大量,并且数据类型一致的元素,目前的解决方案是数组
数组的弊端:
	1. 容量固定,扩容麻烦
	2. 只能处理/存储一种数据类型
	3. 数组胚胎方法少,使用不方便
	4. 因为支持类型有且只能是一种类型,配套容量,配套方法都需要独立完成,无法复用。

集合可以解决以上问题
	1. 容量不需要调用者/程序员考虑,只管用!!【有扩容】
	2. 支持数据类型多样化,但是严格遵从数据类型一致化要求。【有泛型】
	3. 配套方法多,并且工具类多,且操作手段多。
	4. 支持多元化类型,方法复用度高,使用方便。
2.2 集合类型和整体结构和类型特征分析
interface Collection<E>
	Java 中所有集合的基类接口,规定了集合操作的基础方法

--| interface List<E> extends Collection<E>
	JavaList 特征集合接口,继承 Collection 接口,同时增加了 List 接口特征方法
	特征:有序,可重复
	--| class ArrayList<E> implement List<E>
		底层结构为 Object 数据类型,为可变长数组 List 集合,特征:增删慢,查询快
		
	--| class LinkedList<E> implement List<E>
		底层结构为带有链表头的双向链表结构 特征: 增删快,查询慢
	
	--|class Vector<E> implement List<E>
		底层结构为 object 数据类型,为可变长数组 List 集合,线程安全,性能对比 ArrayList 较差
	
--| interface Set<E> extends Collection<E>
	JavaList 特征集合接口,遵从 Collection 接口
	特征:无序不可重复
	
	--| class HashSet<E> implement Set<E>
		底层结构为 哈希表结构,可以认为是Excel 表格
		
	--| class TreeSet<E> implement Set<E>
		底层结构为 二叉树结构
2.3 Collection 集合常用方法
add(E e);
		添加实例化集合对象过程中,泛型约束对应具体数据类型元素。
	addAll(Collection<? extends E> c);
		添加参数集合到当前调用方法集合中,要求参数集合存储的数据类型:
			1. 和调用方法集合存储类型一致
			2. 调用方法集合存储类型的子类
			
删
	remove(Object obj);
		调用方法集合,删除指定元素
	removeAll(Collection<?> c);
		调用方法集合中,删除和参数集合的交集
	retainAll(Collection<?> c);
		调用方法集合中,仅保留和参数集合的交集
	clear();
		清空调用方法集合所有存储元素

查
	int size();
		集合中有效元素个数
	boolean isEmpty();
		判断当前集合是否为空
	boolean contains(Object obj);
		判断参数对象元素是否在当前集合中存在
	boolean cotainsAll(<Collection<?> c);
		判断参数集合对象是否为当前集合的子类
	Object[] toArray();
		返回集合中所有存储元素的 Object 类型数组
【补充知识点】
class Animal
class Monkey extends Animal
class Tiger extends Animal
class Lion extends Animal

<? extends E>
	有泛型,有继承 ? 通配符
如果当前 E ==> 约束为 Animal 类型
<? extends Animal>
	要求当前数据类型可以是 Animal类型或者是 Animal 的子类类型
	【泛型的上限】
2.4 集合特征
	List 集合基于 Collection 集合延伸符合特定需求的一种集合形式,主要引入的模式是【下标模式】,并延续Collection 集合规定的方法,增加了符合 List 集合特征方法
	特征:
		1.操作数据的下标形式
		2.数据存储有序,允许重复
			有序
		 		数据添加顺序和数据存储数据一致
			可重复
		 		存储元素可以出现相同
2.5 集合常用方法
add(E e);
		添加实例化集合对象过程中,泛型约束对应具体数据类型元素。
	addAll(Collection<? extends E> c);
		添加参数集合到当前调用方法集合中,要求参数集合存储的数据类型:
			1. 和调用方法集合存储类型一致
			2. 调用方法集合存储类型的子类
    add(int index, E e);
		添加实例化集合对象过程中,泛型约束对应具体数据类型元素。
	addAll(int index, Collection<? extends E> c);
		添加参数集合到当前调用方法集合中,要求参数集合存储的数据类型:
			1. 调用方法集合存储类型一致
			2. 调用方法集合存储类型的子类
			
删
    E remove(int index);
		删除当前集合中指定的下标元素,返回值是被删除的元素对象。
	remove(Object obj);
		调用方法集合,删除指定元素
	removeAll(Collection<?> c);
		调用方法集合中,删除和参数集合的交集
	retainAll(Collection<?> c);
		调用方法集合中,仅保留和参数集合的交集
	clear();
		清空调用方法集合所有存储元素
改
     E set(int index, E e);
		在当前集合中指定下标位置,使用实例化集合对象约束类型元素,替换对应元素,返回值被替换的元素
查
	int size();
		集合中有效元素个数
	boolean isEmpty();
		判断当前集合是否为空
	boolean contains(Object obj);
		判断参数对象元素是否在当前集合中存在
	boolean cntainsAll<Collection<?> c);
		判断参数集合对象是否为当前集合的子类
	Object[] toArray();
		返回集合中所有存储元素的 Object 类型数组
     E get(int index);
		在当前集合中,获取指定下标元素
	List<E> subList(int fromIndex, int toIndex);
		从当前集合中指定 fromIndex 下标开始到 toIndex 下标结束,获取子集合对象,要头不要尾
	int indexOf(Object obj);
		在当前集合中找出指定元素第一次出现的下标位置
	int lastIndexOf(Object obj);
		在当前集合中找出指定元素最后一次出现的下标位置
package com.qf.collection;

import java.util.ArrayList;
import java.util.List;

public class Demo3 {
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		
		
		list.add("a");
		list.add("螺蛳粉");
		list.add("炸蛋");
		list.add("D");
		list.add("鸭掌");
		list.add("c");
		list.add("腊肠");
		list.add("c");
		list.add("B");
		list.add("豆泡");
		System.out.println(list);
		System.out.println(list.get(3));
		System.out.println(list.set(3, "E"));
		System.out.println(list);
		System.out.println(list.remove(2));
		System.out.println(list);
		List<String> list2 = list.subList(0, 6);
		
	
		System.out.println(list.indexOf("a"));
		System.out.println(list.lastIndexOf("B"));
		System.out.println(list.containsAll(list2));
		System.out.println(list2);
		System.out.println(list2.contains("B"));
		System.out.println(list2.lastIndexOf("a"));
		list.addAll(5, list2);
		System.out.println(list);
	}
}

E"));
System.out.println(list);
System.out.println(list.remove(2));
System.out.println(list);
List list2 = list.subList(0, 6);

	System.out.println(list.indexOf("a"));
	System.out.println(list.lastIndexOf("B"));
	System.out.println(list.containsAll(list2));
	System.out.println(list2);
	System.out.println(list2.contains("B"));
	System.out.println(list2.lastIndexOf("a"));
	list.addAll(5, list2);
	System.out.println(list);
}

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值