# Shiqianfeng against the war classes, Zhangguo Jing #

In the first 26 days of learning Shiqianfeng

Learning is endless, self-improvement for us, far and wide, perseverance. "

Come on, China, Wuhan refueling, refueling forward, their own fuel.

This week a major study Collection packaging and collection system, were to sum up the day by Replay today:
1, packaging after JDK5, providing automatic packing, unpacking, to a large extent simplifies the programming process using a wrapper class
2 , Sting bearing in mind the type of conventional method (17 kinds of commonly used methods)
3, wherein the variable character string (for string concatenation JVM automatically invoked)
. 1) strings JDK1.0 the StringBuffer variable release, slow operating efficiency, security thread;
2 ) StringBuilder variable string JDK1.5 release, running efficiency, thread-safe
3, collection unordered collection system, no subscript.
1) bearing in mind the conventional method
2) List interface features: orderly, subscript element can be repeated.
Implementation class
(1) ArrayList (focus) // JDK7 ago, no-argument constructor directly create Object array of length 10, you do not have and do not create a waste of space. After // JDK8, no-argument constructor to create a direct length 0 array, do not take up space; when you first add elements need to actually allocate space for the array, the array to a real expansion of operations to insert data into an array, (Lazy lazy) with time re-create, or re-loaded, effectively reduce unnecessary memory usage JDK1.2 release fast operating efficiency, thread-safe. Array structure to achieve, fast query, cut slow;
(2) VectorJDK1.0 slow release operating efficiency, thread-safe. Array structure to achieve, fast query, cut slow;
(3) LinkedList (a linked list storage structure, nonlinear) list (List of links) structure to store, query slow, fast additions and deletions.
3) Set interface features: disordered, no subscript elements can not be repeated.
Implementation class
(1) HashSet (focus) elements can not be repeated.
(2) LinkedHashSet (understanding)
(3) TreeSet (to know)
looking for a day of plain feeling good grasp of theoretical knowledge, tools to keep in mind the method, using a level no problem, but aspects of the source code to see if there is some difficulty, still need time to accumulate, to do all things are objects, and all are tools. Still we need to continue to fuel efforts.
# Job
. 4, C
. 6, B
. 7,
. 1) to enhance the salary is a float switch

public int hashCode() {		
	return (int)(name.hashCode()+age+salary);	
}

2) set without removing the subscript 0

s.add(0,new Worker(18,"jerry",2000));

7-8, write code to prove HashSet can not store duplicate objects

public class Worker {
	 int age;
	 String name;
	 double salary;
	 public Worker() {}
	 public Worker(int age, String name, double salary) {
		  super();
		  this.age = age;
		  this.name = name;
		  this.salary = salary;
	 }
	 @Override
	 public int hashCode() {
		  return (int)(name.hashCode()+age+salary);
	 }

	 @Override
	 public boolean equals(Object obj) {
		  if (this==obj) {
			   return true;
		  }
		 if (obj==null) {
			   return false;
		  }
		  if (obj.getClass()!=this.getClass()) {
			   return false;
		  }
		  Worker w=(Worker)obj;
		  if(this.name.equals(w.name)&&
		  this.age==w.age&&this.salary==w.salary) {
			   return true;
		  }
			  return false;
	 }
}
import java.util.HashSet;
import java.util.Set;
public class TestWorker {
	 public static void main(String[] args) {
		  Set <Worker>s=new HashSet<Worker>();
		  s.add(new Worker(18,"tom",2000));
		  s.add(new Worker(18,"tom",2000));
		  s.add(new Worker(18,"jerry",2000));
		  System.out.println(s.size());
	 }
}

. 9,
PUT indicates a method into a key-value pair if the key original value already exists covered, if the key value does not exist is stored.
remove method takes an object parameter that indicates to remove the object.
get method that value, representing the objects get method parameters, return value indicates that the object is returned
in order to get all keys in the Map, the method should be used keySet (), which returns the set value is set
in order to obtain all the values of the Map, should use values (), which returns a value collection set

10-13, (Map) input the output of the year to take the World Cup team, team input, output World Cup team that year

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class TestHashMap {
	 public static void main(String[] args) {
		  Map<Integer, String> hp=new HashMap<Integer, String>();
		  hp.put(1930, "乌拉圭");
		  hp.put(1934, "意大利");
		  hp.put(1938, "意大利");
		  hp.put(1950, "乌拉圭");
		  hp.put(1954, "德国");
		  hp.put(1958, "巴西");
		  hp.put(1962, "巴西");
		  hp.put(1966, "英格兰");
		  hp.put(1970, "巴西");
		  hp.put(1974, "德国");
		  hp.put(1978, "阿根廷");
		  hp.put(1982, "意大利");
		  hp.put(1986, "阿根廷");
		  hp.put(1990, "德国");
		  hp.put(1994, "巴西");
		  hp.put(1998, "法国");
		  hp.put(2002, "巴西");
		  hp.put(2006, "意大利");
		  System.out.println("请输入一个年份");
		  Scanner scan=new Scanner(System.in);
		  Integer year=scan.nextInt();
		  if (hp.get(year)!=null) {
			   System.out.println("世界杯冠军是"+hp.get(year)+"队");
		  }else {
			   System.out.println("没有举办世界杯");
		  }
		  System.out.println("请输入一个队伍");
		  String team=scan.next();
		  if (hp.values().contains(team)) {
			   for (Integer key : hp.keySet()) {
				    if (hp.get(key).equals(team)) {
					     System.out.println(key+"获得世界杯");
				    }
			   }
		   }else {
			   System.err.println("没有获得世界杯");
		   }
		  scan.close();
	 }
}

11, as the value of the teacher as the key courses, complete a series of class action Map

import java.util.HashMap;
import java.util.Map;
public class TestHashMap {
	 public static void main(String[] args) {
		  Map<String , String > hp=new HashMap<String ,String >();
		  hp.put("Tom", "CoreJava");
		  hp.put("John", "Oracle");
		  hp.put("Susan", "Oracle");
		  hp.put("Jerry", "JDBC");
		  hp.put("Jim", "Unix");
		  hp.put("Kevin", "JSP");
		  hp.put("Lucy", "JSP");
		  hp.put("Allen", "JDBC");
		  hp.put("Lucy", "CoreJava");
		  for (String key : hp.keySet()) {
			  System.out.println(key+"\t"+hp.get(key));
		  }
		  for (String key : hp.keySet()) {
			   if (hp.get(key)=="JSP") {
				    System.out.println(key);
			   }
		  }
	 }
}

12、
B

14, (Map) Enter a string, the string of those output characters, each character occurrences

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class TestHashMap {
	 public static void main(String[] args) {
		  Scanner scan=new Scanner(System.in);
		  System.out.println("输入一段字符串");
		  String s=scan.next();
		  Map<Integer, Character> hp=new HashMap<Integer, Character>();
		  for (Integer i = 0; i < s.length(); i++) {
			   hp.put(i, s.charAt(i));
		  }
		  System.out.print("字符串由");
		  for (Integer key : hp.keySet()) {
			   System.out.print(hp.get(key));
			   System.out.print(",");
		  }
		  System.out.println("组成");
		  System.out.println("每个字符出现的次数为");
		  for (Integer key : hp.keySet()) {
			   System.out.print(hp.get(key));
			   System.out.print("出现");
			   int size=1;
			   for (int i = 0; i < s.length(); i++) {
			      	    if (hp.get(i).equals(hp.get(key))) {
				   	 if (i!=key) {
 				 	     size++;
					  }
			  	     }
			     }
			    System.out.print(size);
			    System.out.println();
		     }
		     System.out.println("程序结束");
		     scan.close();
	 }
}
Published 12 original articles · won praise 0 · Views 263

Guess you like

Origin blog.csdn.net/ZhangXiao_L/article/details/104719757