The gods are silent-personal CSDN blog post directory
The reason why I did this is very simple, because I haven't written Java in 3 years and I am currently recovering.
Because I have been using Python recently, I will not write the same parts as Python.
- The most basic framework
public class MainClass { public static void main(String[] args) { //主函数代码 } }
- Print:
System.out.println(打印内容);
- Java array of built-in objects
(to be continued) - instantiate object
- built-in objects
int x=1;
- Custom objects:
类名 实例名=new 类名();
- built-in objects
- Object type conversion
- Convert float to int 1
- Float class
intValue()
Float f = 10.5f; int i = f.intValue(); System.out.println("Float转Int:" + i);
- cast
float f1 = 10.5f; int i1 = (int) f1; System.out.println("浮点型强制转换为整型:" + i1);
- Float class
- Convert float to int 1
- Loop ( Java loop (to be continued) )
- Conditional statement 2
- if
if(布尔表达式) { //如果布尔表达式为true将执行的语句 }
- if-else
if(布尔表达式){ //如果布尔表达式的值为true }else{ //如果布尔表达式的值为false }
- if
- static static method
private private method
final - Arrays class
- Assignment
fill()
public static void fill(arrayname,value)
public static void fill(arrayname ,starting index ,ending index ,value)
Output:import java.util.*; public class Example{ public static void main(String[] args) { int array[] = new int[10]; Arrays.fill(array, 1); for (int arrays:array) { System.out.print(arrays+" "); } System.out.println(); Arrays.fill(array, 3, 6, 9); for (int arrays:array) { System.out.print(arrays+" "); } } }
1 1 1 1 1 1 1 1 1 1 1 1 1 9 9 9 1 1 1 1
- Sort
sort()
public static void sort(Object[] arrayname)
all the elements of an array, and sort the array part in order from small to large,
public static void sort(Object[] arrayname,int fromIndex, int toIndex)
that is, sort the elements of array a from fromIndex to toIndex-1.
Output:import java.util.*; public class Example{ public static void main(String[] args) { int array[] = { 2,5,85,30,75,66,-18,0}; Arrays.sort(array,2,5); for (int arrays:array) { System.out.print(arrays+" "); } System.out.println(); Arrays.sort(array); for (int arrays:array) { System.out.print(arrays+" "); } } }
The underlying principle of Arrays.sort():2 5 30 75 85 66 -18 0 -18 0 2 5 30 66 75 85
Assume that the array length is n
1<=n<47, use insertion sort
47<=n<286, use quick sort
n>=286, use merge sort or quick sort (there is a certain order to use Merge sort, use quick sort in no order) - Search
binarySearch()
Use binary search: the array must be sorted before calling.
public static int binarySearch(Object[] a,Object key)
Search in all elements of an array.
Return value:
Within the range of the array, the index value is "-the insertion point index value"
is less than the element in the array, the index value is - 1
Greater than the element in the array, the index value is – (length + 1).
public static int binarySearch(Object[] a,int fromIndex,int toIndex,Object key)
Search within the range specified by the array.
Return value:
Within the search range, the index value is “-Insertion point index value”. If it
is less than the element in the search range, return –( fromIndex + 1)
is greater than the element in the search range, return – (toIndex + 1)
Output:import java.util.*; public class Example{ public static void main(String[] args) { int array[] = { 2,5,85,30,75,66,-18,0}; Arrays.sort(array); for (int arrays:array) { System.out.print(arrays+" "); } System.out.println(); System.out.println(Arrays.binarySearch(array,5)); System.out.println(Arrays.binarySearch(array,-99)); System.out.println(Arrays.binarySearch(array,100)); System.out.println(Arrays.binarySearch(array,60)); System.out.println(Arrays.binarySearch(array,1,5,5)); System.out.println(Arrays.binarySearch(array,1,5,-99)); System.out.println(Arrays.binarySearch(array,1,5,100)); System.out.println(Arrays.binarySearch(array,1,5,60)); } }
-18 0 2 5 30 66 75 85 3 //5在数组内,返回排完序后的索引3 -1 //-99小于数组内元素,返回索引值为-1 -9 //100大于数组内元素,返回索引值为-(length+1)=-(8+1) -6 //60在数组范围内,返回索引值为-插入点索引值=-6 3 //5在搜索范围内,返回排完序后的索引3 -2 //-99小于搜索范围内元素,返回–(fromIndex + 1)=-(1+1)=-2 -6 //100大于搜索范围内元素,返回–(toIndex + 1)=-(5+1)=-6 -6 //60在搜索范围内,索引值为-插入点索引值=-6`
- Comparison
equals()
Returns true if the two specified arrays are equal to each other. Two arrays are considered equal if they contain the same number of elements and all corresponding pairs of elements in both arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order.
public static boolean equals(Object[] arrayname,Object[] arrayname2)
Output:import java.util.*; public class Example{ public static void main(String[] args) { int[] array1 = { 2,5,85,30,75,66,-18,0}; int[] array2 = { 75,2,66,30,5,85,0,-18}; if(Arrays.equals(array1, array2)){ System.out.println("array1等于array2"); } else{ System.out.println("array1不等于array2"); } Arrays.sort(array1); Arrays.sort(array2); for(int arrays:array1){ System.out.print(arrays+" "); } System.out.println(); for(int arrays:array2){ System.out.print(arrays+" "); } System.out.println(); if(Arrays.equals(array1, array2)){ System.out.println("排序后,array1等于array2"); } else{ System.out.println("排序后,array1不等于array2"); } } }
array1不等于array2 -18 0 2 5 30 66 75 85 -18 0 2 5 30 66 75 85 排序后,array1等于array2
- Copy Copy
copyOf()
the elements of the original array to a new array. You can set the length of the copy (that is, the number of elements that need to be copied).public static Object[] copyOf(original,newLength)
copyOfRange()
Copy elements within a certain range to the new array.public static Object[] copyOfRange(original,int from,int to)
from is the starting position of the copy (including ), to is the end position of the copy (not included)
Output:import java.util.*; public class Example{ public static void main(String[] args) { int[] array1 = { 2,5,85,30,75,66,-18,0}; int[] array2 = Arrays.copyOf(array1, 6); int[] array3 = Arrays.copyOfRange(array1, 2, 4); System.out.println(Arrays.toString(array1)); System.out.println(Arrays.toString(array2)); System.out.println(Arrays.toString(array3)); } }
[2, 5, 85, 30, 75, 66, -18, 0] [2, 5, 85, 30, 75, 66] [85, 30]
- Assignment
Deque<Integer> stack = new ArrayDeque<Integer>();
stack.push(Integer.parseInt(arr[i]));
stack.peek()
stack.pop();
- List
List<Integer> list = new ArrayList<Integer>();
list to string:String str = list.toString();
String.join(" ", nums);
list.add(factor);
- Split the string into an array by delimiter:
String[] arr = data.split(", ");
nums = Arrays.asList(data.split(" "));
- String slicing:
str.substring(1, str.length() - 1);
- Check whether the object is None (None in Java means that it is not created by just declaring it):
data.isEmpty()
- Properties and methods:
length
size()
Integer.MIN_VALUE
Integer.MAX_VALUE
- Number to string:
String.valueOf(root.val)
Integer.parseInt(nums.get(i))
- Math class
min()
3
Output result:public class Test{ public static void main(String args[]){ System.out.println(Math.min(12.123, 12.456)); System.out.println(Math.min(23.12, 23.0)); } }
12.123 23.0
Other references used in writing this article: