Basic knowledge of Java
-
java
- static final int a=0xff;;
- static final int a=067;
- static final int a=126;
- Decimal form 12.37F,-0.423D
- boolean length 1 byte
- Basic data type, reference data type
- toString() converts objects to character
- Conversion between String type and basic type
-
java+java filename, javac command compiles java source file into class bytecode file.
java Hellow Word
javac Hellow Word.java -
java array: array index 0-A.length-1
Create array A=new dataType[10]
Create an array and declare dataType[] A=new dataType[size] -
Character string
//Compare the difference between the ASCII codes of the first letters in two strings st1.compareTo(st2); //Ignore case of letters in characters st1.compareToIgnoreCase(st2); st1.compareTo(object.toString()); //Find the position of a character in a string String s="Hellow World I am lidong"; int last=s.lastIndexOf("lidong"); //Delete a character from a string String str="Hellow Word!"; str.substring(0,pos)+ str.substring(pos+1); //Substitution of letters in strings System.out.println(s.replace('l','a')); //Character reversal String reverse=new StringBuffer(s).reverse().toString(); System.out.println(reverse); //Lookup location int index=s.indexOf("w"); System.out.println(index); //String splitting temp=str.split("//");
- array
//Array sorting System.out.println(array.length); Arrays.sort(array,0,9); for(int i=0;i<array.length;i++){ System.out.println(array[i]); } //Find the location of the element int index=Arrays.binarySearch(array,3); System.out.println("element i In the first place"+index+"Position!"); //Array length String[][] A=new String[5][2]; //Length of one-dimensional array System.out.println(A.length); //Length of 2D array System.out.println(A[0].length); //Array inversion Collections.reverse(arrayList); //Iterate print traversal array for(Iterator<String> iterator=arraylist.iterator();iterator.hasNext();System.out.println(iterator.next())){ } //Get the maximum and minimum values of the array int min= (int) Collections.min(Arrays.asList(array)); int max=(int) Collections.max(Arrays.asList(array));
- java time format processing
Date date=new Date(); //Define time format String dateFormat="yyyy-MM-dd HH:mm:ss"; SimpleDateFormat s=new SimpleDateFormat(dateFormat); System.out.println(s.format(date));
- Definition of generics
public static <E> void show(E[] array){}
- For and ForEach recycling
for (int i = 1; i <= 5; i++) { for (int j = 5; i <= j; j--) { System.out.print(" "); } for (int j = 1; j <= i; j++) { System.out.print("*"); } for (int j = 1; j < i; j++) { System.out.print("*"); } System.out.println(); } for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { System.out.print(i + "*" + j + "=" + i * j+"\t"); } System.out.println(); }
- Exercise topic
//Randomly generate n=10000 floating-point numbers, first output, then sort, then print, and test the sorting time int size=100; Float[] A=new Float[size]; for(int i=0;i<A.length;i++){ A[i]=(float)Math.random()*100; System.out.println("The number generated is:"); System.out.println(A[i]); } Date d=new Date(); Long a=System.currentTimeMillis(); System.out.println("Time before sorting is"+a); Arrays.sort(A); System.out.println("============================"); System.out.println("The sorted number is:"); for(Float element:A){ System.out.println(element); } Long b=System.currentTimeMillis(); System.out.println("The time after sorting is"+b); System.out.println("The time difference is"+(b-a));
- Use of collections
//HashMap traversal, the elements in the collection cannot be duplicate HashMap<String,String> hm=new HashMap<>(); hm.put("one","Hellow Word!"); hm.put("two","Lidong"); hm.put("three","zhnagsan"); hm.put("one","Hellow!"); System.out.println(hm.get("one")); System.out.println(hm.get("two")); System.out.println(hm.get("three")); //The elements in the HashSet cannot be duplicate. The elements after the HashSet will overwrite the elements before it. HashSet<String> set = new HashSet<>(); set.add("Zhang San"); set.add("Li Si"); //Duplicate data add failed set.add("Zhang San"); //for loop traversal set for (String element : set) { System.out.println(element); } System.out.println(set.size()); //Traversal with iterator for(Iterator<String> iterator=set.iterator();iterator.hasNext(); System.out.println(iterator.next())){} //Traversing the list collection: the elements in the list collection can be duplicate List<String> list=new ArrayList<>(); list.add("English?"); list.add("Mathematics"); list.add("English?"); //for loop traversal for(String element:list){ System.out.println(element); } //Iterative traversal Iterator<String> iterator=list.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } //Traverse the key value of HashTable Hashtable h=new Hashtable(); h.put("one","hello"); h.put("two","hello"); h.put("one","hello"); Enumeration e=h.keys(); while(e.hasMoreElements()){ System.out.println(e.nextElement()); }
- java abstract class
Abstract classes cannot be instantiated. If there are abstract methods in a class, the class must be an abstract class. Abstract methods are not necessarily included in an abstract class. - Java notes
/** * javadoc * */ //This is the note /*This is a way*/
- final: the variable is a constant and can only be assigned once