Java常用基础-String和各类集合

String

public class StringExample {

	public static void main(String[] args) {

		// 1.Java定义字符串
		StringExample.defStr();
//
//		// 2.字符串拼接
//		StringExample.concatStr();

//		// 3.字符串长度
//		StringExample.lengthStr();

		// 4.字符串提取
//		StringExample.subStr();

		// 5.大小写转换
//		StringExample.lowerUpperStr();

		// 6.分割字符串
//		StringExample.spiltStr();

		// 7.字符串替换
//		StringExample.replaceStr();

		// 8.字符串比较
//		StringExample.compareStr();

		// 9.字符串查找
//		StringExample.indexOfStr();

//		String str = " a bc ";
//		System.out.println(str.trim());

	}

	public static void defStr() {
		// 直接定义字符串
		String str = "Hello Java";

		// 使用 String 类定义
		String str1 = new String("Hello Java");
		String str2 = new String(str);

		System.out.println(str);
		System.out.println(str1);
		System.out.println(str2);
	}

	public static void concatStr() {
		// 使用连接运算符“+”
		String str = "南京" + "万得";
		System.out.println(str);

		// 使用 concat() 方法
		str = str.concat("资讯科技有限公司");
		System.out.println(str);

		// StringBuffer
		StringBuffer buffer = new StringBuffer("hello,");
		buffer.append(str);
		buffer.append("abc");
		System.out.println(buffer.toString());
	}

	public static void lengthStr() {
		Scanner input = new Scanner(System.in);
		System.out.println("请设置一个密码:");
		String pass = input.next();// 获取用户输入的密码
		input.close();
		int length = pass.length();// 获取密码的长度
		if (length > 6 && length < 12) {
			System.out.println("密码长度符合规定。");
			System.out.println("已生效,请牢记密码:" + pass);
		} else if (length >= 12) {
			System.out.println("密码过长。");
		} else {
			System.out.println("密码过短。");
		}
	}

	public static void subStr() {
		String str = "南京万得资讯科技有限公司";
		String result = str.substring(2);
		System.out.println(result);

		result = str.substring(2, 4);
		System.out.println(result);
	}

	public static void lowerUpperStr() {
		String str = "Hello,Java";
		System.out.println(str.toLowerCase());// 转换为小写
		System.out.println(str.toUpperCase());// 转换为大写

	}

	public static void spiltStr() {
		String colors = "Red,Black,White,Yellow,Blue";
		String[] arr1 = colors.split(","); // 不限制元素个数
		String[] arr2 = colors.split(",", 3); // 限制元素个数为3

		for (int i = 0; i < arr1.length; i++) {
			System.out.println(arr1[i]);
		}

		System.out.println("前三个颜色为:");
		for (int j = 0; j < arr2.length; j++) {
			System.out.println(arr2[j]);
		}
	}

	public static void replaceStr() {
		String words = "hello java ,hello python";
//		System.out.println(words.replace("hello", "你好"));
//
//		System.out.println(words.replaceFirst("hello", "你好"));
//
//		System.out.println(words.replaceAll("hello", "你好"));

		words = "hello java \\w,hello python";
		System.out.println(words.replace("\\w", "你好"));
		System.out.println(words.replaceAll("\\w", "你好"));
	}

	public static void compareStr() {
		// equals()与==的比较
		String s1 = "Hello";
		String s2 = new String(s1);
		System.out.println(s1.equals(s2));
		System.out.println(s1 == s2);





		// equalsIgnoreCase()
		String s3 = "HELLO";
		System.out.println(s1.equalsIgnoreCase(s3));

		// compareTo() 方法
		String str = "A";
		String str1 = "a";
		System.out.println("str.compareTo(str1)的结果是:" + str.compareTo(str1));
		System.out.println("str1.compareTo(str)的结果是:" + str1.compareTo(str));
		System.out.println("str1.compareTo('a')的结果是:" + str1.compareTo("a"));



	}

	public static void indexOfStr() {
		// indexOf() 方法用于返回字符(串)在指定字符串中首次出现的索引位置,如果能找到,则返回索引值,否则返回 -1。
		String s = "Hello Java";
		int size = s.indexOf('v'); // size的结果为8
		System.out.println(size);

		// lastlndexOf() 方法用于返回字符(串)在指定字符串中最后一次出现的索引位置,如果能找到则返回索引值,否则返回 -1。
		System.out.println(s.lastIndexOf("a"));

	}

}

集合的各类方法(包含list set map)

集合-ArrayList

	public static void main(String[] args) {
		// 创建一个空的数组链表对象list,list用来存放String类型的数据
		List<String> list = new ArrayList<String>();

		// 增加元素到list对象中
		list.add("Item1");
		list.add("Item2");
		list.add(2, "Item3"); // 此条语句将会把“Item3”字符串增加到list的第3个位置。
		list.add("Item4");

		// 显示数组链表中的内容
		System.out.println("The arraylist contains the following elements: " + list);

		// 检查元素的位置
		int pos = list.indexOf("Item2");
		System.out.println("The index of Item2 is: " + pos);

		// 检查数组链表是否为空

		boolean check = list.isEmpty();
		System.out.println("Checking if the arraylist is empty: " + check);

		// 获取链表的大小
		int size = list.size();
		System.out.println("The size of the list is: " + size);

		// 检查数组链表中是否包含某元素
		boolean element = list.contains("Item5");
		System.out.println("Checking if the arraylist contains the object Item5: " + element);

		// 获取指定位置上的元素
		String item = list.get(0);

		System.out.println("The item is the index 0 is: " + item);

		// 遍历arraylist中的元素

		// 第1种方法: 循环使用元素的索引和链表的大小
		System.out.println("Retrieving items with loop using index and size list");
		for (int i = 0; i < list.size(); i++) {
			System.out.println("Index: " + i + " - Item: " + list.get(i));
		}

		// 第2种方法:使用foreach循环
		System.out.println("Retrieving items using foreach loop");
		for (String str : list) {
			System.out.println("Item is: " + str);
		}

		// 第三种方法:使用迭代器
		// hasNext(): 返回true表示链表链表中还有元素
		// next(): 返回下一个元素
		System.out.println("Retrieving items using iterator");
		for (Iterator<String> it = list.iterator(); it.hasNext();) {
			System.out.println("Item is: " + it.next());
		}

		// 替换元素
		list.set(1, "NewItem");
		System.out.println("The arraylist after the replacement is: " + list);

		// 移除元素
		// 移除第0个位置上的元素
		list.remove(0);

		// 移除第一次找到的 "Item3"元素
		list.remove("Item3");

		System.out.println("The final contents of the arraylist are: " + list);

		// 转换 ArrayList 为 Array
		String[] simpleArray = list.toArray(new String[list.size()]);
		System.out
				.println("The array created after the conversion of our arraylist is: " + Arrays.toString(simpleArray));
	}

}

Set集合

	public static void main(String[] args) {

		Set<String> set = new HashSet<String>(); // 创建一个空的 Set 集合
		String name1 = new String("李雨");
		String name2 = new String("赵益");
		String name3 = new String("戴文浩");

		set.add(name1);
		set.add(name2);
		set.add(name3);
		set.add(name1);
//		set.remove(name3);

		System.out.println("学生列表:");
		Iterator<String> it = set.iterator();
		while (it.hasNext()) {
			System.out.println((String) it.next()); // 输出 Set 集合中的元素
		}

		System.out.println("学生数量:" + set.size());
//
		System.out.println("学生" + name1 + "是否在集合中:" + set.contains(name1));
		Set<String> set1 = new HashSet<String>(); // 创建一个空的 Set 集合
		set1.add(name1);
		set1.add("abc");
		System.out.println(set.containsAll(set1));
//		System.out.println(set.retainAll(set1));
//		System.out.println(set);
		set.addAll(set1);
		System.out.println(set);

	}

}

Map集合

	public static void main(String[] args) {
		Map<String, String> users = new HashMap<String, String>();
		users.put("11", "李雨"); // 将学生信息键值对存储到Map中
		users.put("22", "赵益");
		users.put("33", "高原");
		users.put("44", "戴文浩");
		System.out.println("******** 学生列表 ********");

		Iterator<String> it = users.keySet().iterator();
		while (it.hasNext()) {
			// 遍历 Map
			Object key = it.next();
			Object val = users.get(key);
			System.out.println("学号:" + key + ",姓名:" + val);
	}

		Scanner input = new Scanner(System.in);
		System.out.println("请输入要删除的学号:");
		int num = input.nextInt();
		input.close();
		if (users.containsKey(String.valueOf(num))) { // 判断是否包含指定键
			users.remove(String.valueOf(num)); // 如果包含就删除
		} else {
			System.out.println("该学生不存在!");
		}
		System.out.println("******** 学生列表 ********");
		Iterator<Map.Entry<String,String>> entryIterator = users.entrySet().iterator();
		while (entryIterator.hasNext()) {
			Map.Entry<String,String> current =  entryIterator.next();
			Object key = current.getKey();
			Object val =current.getValue();
			System.out.println("学号:" + key + ",姓名:" + val);
		}

	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值