java basic string exercises

1. Problem description
Obtain 6 random numbers within 100, and use StringBuffer to splice the numbers together to distinguish them. (int)(Math.random()*(maximum value-minimum value+1))+minimum value;
Then after dividing by "-", put the numbers into the array, bubble sort the elements in the array and output.
2. Scoring criteria
(1) Obtain 6 random numbers within 100 (5 points)
(2) Use StringBuffer to splice the numbers together and distinguish them by "-" (for example: 2-34-12-4-66-1) (5 points) )
(3) The string is divided by "-" and put into the array (5 points)
(4) Bubble sort the numbers in the array (5 points)
(5) Output the data in the array (5 points)

import java.util.Arrays;
public class Test {
    
    
	public static void main(String[] args) {
    
    
		StringBuffer buffer = new StringBuffer();	
		//获取6个100以内的随机数
		for (int i = 0; i < 6; i++) {
    
    
			int num = (int)(Math.random()*(100-0+1)+0);
			//使用StringBuffer将数字拼接到一起以“-”区分
			buffer.append(num+"-");		
		}
//		subString();左闭右开,删除最后一个“-”
		String string = buffer.substring(0,buffer.lastIndexOf("-"));
		System.out.println(string);	
		//分割
		//(3)字符串按照“-”分割后放入数组
		String[] strings = string.split("-");
		System.out.println(Arrays.toString(strings));		
		//新建一个新的数组 int类型 将strings中每一个元素进行parseInt,放入到数组中
		int[] arr = new int[strings.length];
		//将strings中每一个元素进行parseInt
		for (int i = 0; i < strings.length; i++) {
    
    
			arr[i]=Integer.parseInt(strings[i]);		
		}
		//冒泡排序
		//(4)对数组内的数字进行冒泡排序
		for (int i = 0; i < arr.length-1; i++) {
    
    
			for (int j = 0; j < arr.length-1-i; j++) {
    
    
				if(arr[j]<arr[j+1]) {
    
    
					int temp = arr[j];
					arr[j]=arr[j+1];
					arr[j+1]=temp;
				}
			}
		}
		//(5)输出数组内的数据
		System.out.println(Arrays.toString(arr));
}
}

**2. Title description: **Testing the performance of String, StringBuilder, StringBuffer appending strings, designing a program, requiring random generation of 10000 (1-100) numbers to be appended to String, StringBuilder, StringBuffer respectively, the test consumes time, and Analyze the three from the perspective of speed, thread safety and usage scenarios.
2. Scoring criteria:
(1) The design cycle spliced ​​10,000 times and the example code of character a is correct. (5 points)
(2) The time-consuming result of the instance test is correct. (5 points)
The difference between String, StringBuilder and StringBuffer (from the perspective of speed, thread safety and usage scenarios) is described correctly and added to the comments. (5 points)

public class Test {
    
    
	public static void main(String[] args) {
    
    
		//消耗时间
		long l = System.currentTimeMillis();
		//String
		String str = " ";
		//随机生成10000个(1-100)数字
		for (int i = 0; i < 10000; i++) {
    
    
			str += (int)(Math.random()*101);//(100-0+1)+0)
		}
		long l2 = System.currentTimeMillis();		
		long l3 = l2-l;
		System.out.println("利用String消耗的时间"+l3);	
		//StringBuffer
		StringBuffer buffer = new StringBuffer();
		long l4 = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
    
    
			buffer.append((int)(Math.random()*101));
		}
		long l5 = System.currentTimeMillis();
		long l6 = l5-l4;
		System.out.println("利用StringBuffer消耗的时间"+l6);		
		//StringBuilder
		StringBuilder builder = new StringBuilder();
		long l7 = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
    
    
			builder.append((int)(Math.random()*101));
		}
		long l8 = System.currentTimeMillis();
		long l9 = l8-l7;
		System.out.println("利用StringBuilder消耗的时间"+l9);		
	}
}

operation result
The System.currentTimeMillis() method is used to calculate the time consumed by the code to run.
String, StringBuilder, StringBuffer (from the perspective of speed, thread safety and usage scenarios).
From the time consumption: StringBuilder is the most
efficient and String is the least efficient.
Thread safety:
StringBuffer is thread safe and
StringBuilder thread Unsafe
usage scenarios:
Flexible use of String
StringBuffer is suitable for multi-threaded use
StringBuilder is suitable for single-threaded
3. Problem description
Complete the following questions as required:
(1) Encapsulate a worker class with variables: name, age, salary, entry time, and work method. (5 points)
(2) Create a worker array and add the following four workers to the array. The basic information is as follows: (5 points)
Name, age, salary, entry time
Zhang 3 18 3000 March 26, 2016
Li 4 25 3500 2017 March 26,
Wang 5 22 3200 July 26, 2016
Zhang 6 18 3000 March 26, 2016
(3) Change the string "Name: Zhao 6, Age: 24, Salary: 3300, Entry Date: 2017 04 "Month 15th" is divided, assigned to a worker object, and the Zhang 6 information in the original array is replaced. (10 points)
(4) Use enhanced for loop traversal to output the information of all workers in the array (5 points)
(5) Export the name of the worker with the highest wage. (5 points)
(6) Sort the workers in ascending order by salary, calculate the sort time and output the information of all workers in the set. (10 points)
(7) Output information on all workers whose surname is "Zhang" (5 points)
(8) Output information on workers who entered the job in 2017. (5 points)

//(1)封装一个工人类,有变量:姓名、年龄、工资、入职时间,有work方法。(5分)
public class Worker {
    
    
		private String name;
		private int age;
		private int salary;
		private String time;
		public Worker() {
    
    
			super();		
		}
		public Worker(String name, int age, int salary, String time) {
    
    
			super();
			this.name = name;
			this.age = age;
			this.salary = salary;
			this.time = time;
		}
		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 int getSalary() {
    
    
			return salary;
		}
		public void setSalary(int salary) {
    
    
			this.salary = salary;
		}
		public String getTime() {
    
    
			return time;
		}
		public void setTime(String time) {
    
    
			this.time = time;
		}
		@Override
		public String toString() {
    
    
			return "姓名" + name + ",年龄" + age + ", 工资" + salary + ", 入职时间" + time ;
		}
	public void work() {
    
    
		System.out.println("工作");
	}
}
public class Test {
    
    
	public static void main(String[] args) {
    
    
		// (2)创建一个工人数组,在数组中增加如下四个工人,基本信息如下:
        Worker[] workers = new Worker[4];
		//创建4个对象给数组赋值
        workers[0] = new Worker("张3", 18, 3000, "2016年03月26日");
        workers[1] = new Worker("李4", 25, 3500, "2017年03月26日");
        workers[2] = new Worker("王5", 22, 3200, "2016年07月26日");
        workers[3] = new Worker("张6", 18, 3000, "2016年03月26日");
		// (3)将字符串”姓名:赵6,年龄:24,工资:3300,入职时间:2017年04月 15日”进行分割,赋值给一个工人类对象,并且将原数组中的张6信息替换掉。(10分)
		//拆分 拆两次
		String str = "姓名:赵6,年龄:24,工资:3300,入职时间:2017年04月15日";
		// 第一次根据,拆
		String[] strings = str.split(",");
		//遍历过程中 进行二次拆分 将拆分出来的信息创建一个Worker对象
		String[] names = strings[0].split(":");		
		String[] age = strings[1].split(":");
		String[] salary = strings[2].split(":");
		String[] time = strings[3].split(":");	
		// 创建worker对象
		Worker worker = new Worker(names[1], Integer.parseInt(age[1]), Integer.parseInt(salary[1]), time[1]);
		//并且将原数组中的张6信息替换掉
		workers[3] = worker;
		//(4)	利用增强for 循环遍历,输出数组中所有工人的信息
		for (Worker worker2 : workers) {
    
    
			System.out.println(worker2);
		}	
		long l = System.currentTimeMillis();
		System.out.println("按工资排序");
		for (int i = 0; i <workers.length-1; i++) {
    
    
			for (int j = 0; j < workers.length-i-1; j++) {
    
    
				if(workers[j].getSalary()>workers[j+1].getSalary ()){
    
    
					// 交换工人对象
					Worker worker2 = workers[j];
					workers[j] = workers[j+1];
					workers[j+1] = worker2;
					}
				}
			}
		for (Worker worker3 : workers) {
    
    
			System.out.println(worker3);
		} 
		//计算排序时间并输出集合中所有工人的信息
		long l2 = System.currentTimeMillis();
		System.out.println("排序消耗时间:"+(l2-l));
		//(5)	输出工资最高的工人的姓名。
		System.out.println("工资最高的工人的名字:"+workers[3].getName());		
		//输出所有姓“张”的工人信息(5分)
	for (Worker worker2 : workers) {
    
    
		if(worker2.getName().startsWith("张")) {
    
    
			System.out.println("所有姓“张”的工人信息"+worker2);
	}
	}
	//输出"2017年入职的工人信息"
	for (Worker worker2 : workers) {
    
    
			if(worker2.getTime().contains("2017")){
    
    
				System.out.println("2017年入职的工人信息"+worker2);
			}
		}		
	}
}

Run code results

Guess you like

Origin blog.csdn.net/Echoxxxxx/article/details/112370701