Java Machine Trial Preparation
General structure: import Java.util.Scanner;
public class main{
public static void Main (string[] args) {
System.out.println ();
}
}
Input stream :
String: Scanner input = new Scanner (system.in);
while (Input.hasnextline ()) {
String str = input.nextline ();
}
Integer: Scanner input = new Scanner (system.in);
while (Input.hasnext ()) {
String str = Input.nextint ();
}
TreeMap using: red black tree implementation, key value pair, default key value key ascending order
Definition: Treemap<integer,integer>map = new treemap<integer,integer> ();
Determines whether to include the specified key name key: if (map. ContainsKey (Key))
Gets The value that corresponds to the key: map.get (key);
put key value pairs in map : Map.put (Key,value);
Gets all the key values into the set set: map.keyset ();
Traversing the key-value pairs in the output map : For (Integer key: map.keyset ()) {
SYSTEM.OUT.PRINTLN (key + "" + map.get (key));
}
use of TreeSet: based on treemap, single element, elements in ascending order
Definition: treeset<integer> set = new Treeset<integer> ();
for (int i=0;i<num;i++) {
int cur = s.nextint ();
Put treeset : set.add (cur);
}
Traversal: For (Integer i:set) {
System.out.println (i);
}
ArrayList Dynamic array:
Definition: ArrayList<String> set = new arraylist<string> ();
for (int i=0;i<num;i++) {
String str = s.nextline ();
String Join array:set.add (str);
}
Sort by ascending: Collections.sort (set);
Traversing an array of output strings: for (string str:set) {
System.out.println (str);
}
definition array: int []arr=new int[128];
array Length: int len =arr.length;
length of the string:int len =str.length ();
To retrieve the position of a character in a string:
Previous:str. IndexOf (""); Retrieves a space after a trip and returns the position of a space
From backward:Str.lastindexof (""); Retrieve space from backward, return space position
retrieving characters in a string:Str.charat (i); return character
The string is split into a string array: String strarr[]= Str.split (""); (splits a string into a string array with a space)
string converted to uppercase:str.touppercase ();
string converted to lowercase:str.tolowercase ();
integer conversion to binary:String str = integer.tobinarystring (n); (Convert to on string form)
the string is converted to an integer:int i= integer.parseint (str,); int i= integer.parseint (str,+) (string is a decimal integer, converted to
convert integers to Strings: String s=string.valueof (i); String s=integer.tostring (i);
the character is converted to an integer:str.charat (i)-' 0 '; or Str.charat (i)-;
character to integer:int num = integer.valueof (ch[i]);
type cast: int n= (int) num; ( Num to be Double type of)
Convert string to character array:char ch[]=str.tochararray ();
intercept string:str.substring (0,8): intercepts a substring of position 0 to position 7
Str.substring (8): intercepts a substring from position 8 to the end
0-9:48-57
A- z:65-90
A- z:97-122
Note: Integers are too large to be converted to string processing!
Java Machine Test Essentials