BC68-X pattern
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
String str = String.join("",Collections.nCopies(n," "));
for (int i = 0; i < n; i++) {
char[] temp = str.toCharArray();
temp[i] = '*';
temp[n-i-1] = '*';
System.out.println(new String(temp));
}
}
}
}
I have done several questions about outputting patterns, and the code gets longer and longer. Refer to other people's code to learn new knowledge.
-
List l = new ArrayList();// Using the ArrayList class to instantiate the List collection
-
It is often necessary to concatenate the items of an array or List into a string through a separator. The general implementation logic is to connect through member + separator, and then cut off the last separator in the result. The String.Join method implements this function.
List names=new ArrayList<String>(); names.add("1"); names.add("2"); names.add("3"); System.out.println(String.join("-", names)); String[] arrStr=new String[]{ "a","b","c"}; System.out.println(String.join("-", arrStr)); 输出: 1-2-3 a-b-c
-
The ncopies(int, T) method is used to return an immutable list of n copies of the specified object. Declaration: public static List nCopies(int n, T o)
Parameters: n-- the number of elements in the returned list; o-- the repeated elements in the returned list. Return value: The specified object of n copies of the immutable list returned by the method call. Exception: IllegalArgumentException-- If n <0 this exception is thrown.
BC74-HTTP status code
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<Integer,String> map = new HashMap<>(16);
map.put(200,"OK");
map.put(202,"Accepted");
map.put(400,"Bad Request");
map.put(403,"Forbidden");
map.put(404,"Not Found");
map.put(500,"Internal Server Error");
map.put(502,"Bad Gateway");
while (sc.hasNext()) {
int state = sc.nextInt();
System.out.println(map.get(state));
}
}
}
After using switch to watch other people's method, I found this approach, and learn about map.
In the collection of Map, elements exist in pairs (understood as husband and wife). Each element is composed of two parts, a key and a value, and the corresponding value can be found through the key. The collection in the Map cannot contain duplicate keys, and the values can be duplicated; each key can only correspond to one value.
HashMap<K,V>: The structure of the hash table used to store data. The access sequence of elements cannot be guaranteed to be consistent. To ensure the uniqueness and non-repetition of the key, the hashCode() method and equals() method of the key need to be rewritten.
LinkedHashMap<K,V>: There is a subclass LinkedHashMap under HashMap, which uses the hash table structure + linked list structure to store data. The linked list structure can ensure that the access sequence of the elements is consistent; the unique and non-repetitive keys that can be ensured by the hash table structure, the hashCode() method and equals() method of the key need to be rewritten.
Note: The collection in the Map interface has two generic variables <K,V>. When using, the two generic variables must be assigned data types. The data types of the two generic variables <K,V> can be the same or different.
BC80-Login Verification
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String[] strs=sc.nextLine().split(" ");
if(strs[0].equals("admin")&&strs[1].equals("admin")) {
System.out.println("Login Success!");
}else {
System.out.println("Login Fail!");
}
}
}
Method for judging the same string: String.equals()
BC85-a number containing the number 9
public class Main {
public static void main(String[] args) {
int sum=0;
for(Integer i=1;i<=2019;i++) {
if(i.toString().contains("9")) {
sum++;
}
}
System.out.println(sum);
}
}
- toString() has two forms: toString() returns a String object representing an Integer value; toString(int i) returns a String object representing a specified int.
- contains() determines whether there is a substring in the string. It returns true if it has, and false if it doesn't.
- Integer is a wrapper class of int, int is a basic data type of java;
Integer variables must be instantiated before they can be used, and int variables are not needed;
Integer is actually a reference to an object, when new is an Integer, it is actually generated A pointer points to this object; while int stores the data value directly;
the default value of Integer is null, and the default value of int is 0.
other
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] a = {
9, 8, 7, 2, 3, 4, 1, 0, 6, 5};
Arrays.sort(a);
for(int i = 0; i < a.length; i ++) {
System.out.print(a[i] + " ");
}
}
}
输出:
0 1 2 3 4 5 6 7 8 9
1. Arrays.sort(int[] a)
This form is to sort all the elements of an array, and in the order from small to large.
2. Arrays.sort(int[] a, int fromIndex, int toIndex)
This form is to sort the array part, that is, to sort the elements of the subscript of array a from fromIndex to toIndex-1. Note: the subscript is toIndex The elements do not participate in sorting.