Java字符串操作的三个作业—考察JAVA字符串数组,字符串截取和集合的相关方法

在这里插入图片描述

作业一:

代码:

Department类:

package xxx.xxx;//这里写自己的包名

public class Department {
   private String deptNo;
   private String deptName;
   private Employee[] employees;
   public Department(String deptNo, String deptName) {
       this.deptNo = deptNo;
       this.deptName = deptName;
   } 
   
   public int getCount(){
       return this.employees.length;
   }
   

   
 public void  setEmployees(Employee[] employee){

	 this.employees=employee;
}
   
   public void show(){

       System.out.println("部门编号:"+this.deptNo);
       System.out.println("部门名称:"+this.deptName);
       System.out.println("员工信息(共" + getCount() + "人):");
       for(int i = 0; i < this.employees.length; i++){
    	   System.out.println((i+1)+","+employees[i].getInfo());
       }
   }

}

Employee类:

package xxx.xxx;//这里写自己的包名

public class Employee {
    private String name;
    private int age;
    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
    public String getInfo() {
        return "姓名:"+getName()+",年龄:"+getAge();
    }
}

Test测试类:

package xxx.xxx;//这里写自己的包名

public class Test {
    public static void main(String[] args) {
        Department department = new Department("B2004", "计算机与信息工程学院");
        Employee employee1 = new Employee("张三", 19);
        Employee employee2 = new Employee("赵四", 18);  
        Employee employee3 = new Employee("王五", 17); 
        Employee employee4 = new Employee("陈六", 16);
        Employee employee5 = new Employee("李七", 20);
         
        Employee[] employees = new Employee[]{employee1,employee2,
        		employee3,employee4,employee5};
        
        department.setEmployees(employees);
        
         
        department.show();
 
    }
}



作业二:
代码:

package xxx.xxx;//这里写自己的包名
/*
 * 判断简历中的姓名是否姓李,提取出简历中的出生日期与联系方式,
 * =>需要提取李伟=>姓名:        与                            空格
 * =>提取出生日期=>出生日期:     与                             空格
 * =>提取联系方式=>联系方式:     与                             空格
 * 
 * 并判断简历中的专业是否是"物联网工程"=>使用string.conmtains=>如果包含就返回ture
 */
public class Test {
	public static void main(String[] args) {
		String str1 ="姓名:李伟 出生日期:1998-12-31 专业:计算计科学与技术 联系方式:13354653465";
		
		//第一次获取:和空格的索引
		int dot1 = str1.indexOf(":");
		int dot2 = str1.indexOf(" ");
		
		String str2 = str1.substring(dot1+1,dot2);
		System.out.println("第一次提取到的字符串为:"+str2);
		if(str2.startsWith("李")) {
			System.out.println("他姓李!");
		}else {
			System.out.println("他不姓李!");
		}
		
		System.out.println("==========================");
		
		//第二次获取:和空格的索引
		int dot3 = str1.indexOf(":",dot1+1);
		int dot4 = str1.indexOf(" ",dot2+1);
		
		String str3 = str1.substring(dot3+1,dot4);
		System.out.println("第二次提取到的字符串为:"+str3);
		System.out.println("出生日期:"+str3);
		
		System.out.println("==========================");
		
		//第三次获取:和空格的索引
		int dot5 = str1.indexOf(":",dot3+1);
		int dot6 = str1.indexOf(" ",dot4+1);

		String str4 = str1.substring(dot5+1,dot6);
		System.out.println("第三次提取到的字符串为:"+str4);
		if(str4.equals("物联网工程")) {
			System.out.println("他是物联网工程专业");
		}else {
			System.out.println("他不是物联网工程专业");
		}
		
		System.out.println("==========================");
		//第三次获取:和空格的索引
		int dot7 = str1.indexOf(":",dot5+1);

		String str5 = str1.substring(dot7+1);
		System.out.println("第四次提取到的字符串为:"+str5);
		System.out.println("他的联系方式是:"+str5);
	}
}


作业三:
补充:知识点:

集合:
集合包含collection与map.
list和set都继承自collection接口,所以大部分方法差不多.
另外,
ArrayList(相当于动态数组=>底层是用数组实现的)
LinkList(底层是用双向链表实现的)

list:
在这里插入图片描述
set:(list与set都继承collection接口,所以大多数方法一样,所以,用到那个方法就去课本查那个方法即可)
这里补充一下:
list与set的区别:
在这里插入图片描述


map:

在这里插入图片描述
map是存储键(key)->值(value)对的.
map是使用键(key),来寻找值的(value)
=>
理解:这里的键(key)有点类似"索引",
只不过,这个"索引"是由自己来定义的,并没有按照一定顺序.
这个键(key)由自己定义,可以是Integer类型,也可以是String类型.

当然,我们也可以用泛型来规范键和值的类型

		//用泛型来规范
        HashMap<String, Integer> hashMap = new HashMap<>();//前边那个<>里写了,后边这个<>就不用写了
        
        //不用泛型来规范
        HashMap hashMap1 = new HashMap();

代码:

package xxx.xxx;//这里写自己的包名

import java.util.HashMap;

public class Test {

    public static  void main(String[] args) {

		/*(1)统计每个单词出现的次数
		(2)有如下字符串"There will be no regret and sorrow if
				you fight with all your strength"(用空格间隔)
		(3)打印格式:
			There=3
			will=1
			your=2
			//........
		*/

        String str1 = "There will be no regret and sorrow if "
                + "you fight with all your strength";

        //一个或者多个空格作为分割,分割每个单词装进字符串数组;
        //通过正则表达式将这个英文句子的每个单词分割开来,再将分割开的单词依次存入字符串数组single_word中
        String[] single_word = str1.split("\\s+");
//注"Java中的 split函数是用于按指定字符(串)或正则去分割某个字符串,结果以字符串数组形式返回;
//所以,上面个句代码也可以写成: String[] single_word = str1.split(" ");

        //创建map对象=>通过泛型:表示键key的类型只能是String类型,值(value)的类型只能是Integer类型
        //这样做的原因是:用键=>来存储单词     用值=>来存储该单词出现的次数    (众所周知,键与值是成对出现的,对吧)
        HashMap<String, Integer> hashMap = new HashMap<>();//前边那个<>里写了,后边这个<>就不用写了



        for (int i = 0; i < single_word.length; i++) {

            //判断集合中是否有该单词
            if(!hashMap.containsKey(single_word[i])) {
                //一开始集合肯定什么都没有
                //在第一次循环中,也就是遍历第一个单词的过程中
                //集合中肯定没有该单词,值定义为1=>表示这个单词有一个了
                hashMap.put(single_word[i], 1);
                //在每次遍历的过程中,第一次遇见这个单词,就把这个单词放入map中,并定义对应的值为1(也就是定义这个单词出现的次数)

            }else {//如果这个单词已经存过了一次及以上,就进入else{}中
                //能进入到else里,说明=>该次循环,正在遍历的那个单词,又一次出现了,那我们就想办法,在这个键下,将对应的值+1

                //定义一个临时变量,来存储,此刻,这个单词已经出现的次数
                int b = hashMap.get(single_word[i]);

                hashMap.put(single_word[i], b + 1);//再在这个键下,将对应的值加1
            }
        }

        System.out.println("打印格式:");
        System.out.println("单词=出现的次数");
        System.out.println(hashMap);



    }

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

踏过山河,踏过海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值