Java notes, java notes
ArrayList <String> List = new ArrayList <String> (); // defines a dynamic array List. add (temp); // add the string List <Integer> ret = new ArrayList <Integer> (); ret. add (I + 1 );
String. Trim method-Trim () removes leading and trailing white spaces from the current String object. The java. lang. string. split method Splits a string into substrings and returns the result as a string array. String [] res = s. trim (). split ("\ s +"); remove the leading and trailing spaces and cut them into sb words. append (res [I]). append (""); // Add the word + "" And return sb each time. toString (). trim (); // remove the leading and trailing spaces of the string
- The number is converted to a letter, which exists in the string.
arr.insert(0,(char)('A'+n%26));
- Hexadecimal conversion functions
1 decimal to hexadecimal: 2 Integer. toHexString (int I) 3 decimal to octal 4 Integer. toOctalString (int I) 5 decimal to binary 6 Integer. toBinaryString (int I) 7 hexadecimal format to convert to decimal 8 Integer. valueOf ("FFFF", 16 ). toString () 9 octal decimal 10 Integer. valueOf ("876", 8 ). toString () 11 binary to decimal 12 Integer. valueOf ("0101", 2 ). toString ()
· Convert Integers to binary integers. toBinaryString (I );
Array sorting
· Arrays.sort(nums);
Set <Integer> s = new HashSet <Integer> ();
Contains () to determine whether an element exists in HashSet (). If yes, true is returned. Otherwise, false is returned.
Add () if the set does not contain the specified element, add the specified element. If this Set does not contain meet (e = null? E2 = null: e. equals (e2) e2, e2 is added to the Set. Otherwise, false is returned if no value is added.
Remove: if the specified element exists in this set, remove it.
Clear removes all elements from this set.
- Java. lang. Character. getNumericValue () method
'''
· Java. lang. Character. getNumericValue (char ch) returns the int value represented by the specified Unicode Character. For example, the character '\ u216c' (Roman numerals 50) returns an int value of 50.
· Uppercase AZ letters ('\ u0041' to '\ u005A'), lowercase letters ('\ u0061' to '\ u007A '), the value of the full width variant ('\ uFF21' passed '\ uFF3A' and '\ uFF41 "passed' \ uFF5A ') ranges from 10 to 35. This is an independent Unicode specification. The values of these characters are not assigned numerical values.
· If the character does not have a numeric value,-1 is returned. If a character has a numeric value and cannot be expressed as a non-negative integer (for example, a numeric value),-2 is returned.
'''
import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class Main{public static void print(int index ,Object obj){System.out.println(String.format("{%d},%s",index,obj.toString()));}public static void main(String[] args){//demoString();demoList();}public static void demoList(){List<String> strList=new ArrayList<String>();for(int i=0;i<4;i++){strList.add(String.valueOf(i));}print(1,strList);List<String> strListB=new ArrayList<String>();for(int i=0;i<4;i++){strListB.add(String.valueOf(i*i));}strList.addAll(strListB);print(2,strList);strList.remove(0);print(3,strList);strList.remove(String.valueOf(1));print(4,strList);print(5,strList.get(1));Collections.sort(strList);print(6,strList);Collections.sort(strList, new Comparator<String>() {@Overridepublic int compare(String o1, String o2) {return o2.compareTo(o1);}});print(7,strList);}/*{1},[0, 1, 2, 3]{2},[0, 1, 2, 3, 0, 1, 4, 9]{3},[1, 2, 3, 0, 1, 4, 9]{4},[2, 3, 0, 1, 4, 9]{5},3{6},[0, 1, 2, 3, 4, 9]{7},[9, 4, 3, 2, 1, 0]*/public static void demoString(){String str="Hello nowcoder";print(1,"Hello!");print(2,str.charAt(6));print(3,str.codePointAt(1));print(4,str.compareTo("Hello mewcoder"));print(5,str.compareTo("Hello pewcoder"));print(6,str.contains("Hello"));print(7,str.indexOf('e'));print(8,str.compareToIgnoreCase("Hello Nowcoder"));print(9,str.concat("!!"));print(10,str.endsWith("nowcoder"));print(11,str.startsWith("Hello"));print(12,str.lastIndexOf('o'));print(13,str.toUpperCase());print(14,str.replace('o','a'));print(15,str.replaceAll("o|l","a"));print(16,str.replaceAll("Hello","hi"));StringBuilder sb =new StringBuilder();sb.append(true);sb.append(1);sb.append(2.2);print(17,sb.toString());print(18,"a"+"b"+String.valueOf(12));}/*{1},Hello!{2},n{3},101{4},1{5},-2{6},true{7},1{8},0{9},Hello nowcoder!!{10},true{11},true{12},10{13},HELLO NOWCODER{14},Hella nawcader{15},Heaaa nawcader{16},hi nowcoder{17},true12.2{18},ab12*/}
List<Integer> array = Arrays.asList(new Integer[]{1,2,3,4,5});Collections.shuffle(array);