之所以干这个事原因也很简单,因为我3年没写Java了,现在在复健。
因为我最近都在用Python,所以跟Python一样的部分我就不写了。
很多内容是几句话说不清的,我就会另开一篇博文来写,链接也放到本文中。
public class MainClass {
public static void main(String[] args) {
//主函数代码
}
}
System.out.println(打印内容);
int x=1;
类名 实例名=new 类名();
intValue()
Float f = 10.5f;
int i = f.intValue();
System.out.println("Float转Int:" + i);
float f1 = 10.5f;
int i1 = (int) f1;
System.out.println("浮点型强制转换为整型:" + i1);
if(布尔表达式)
{
//如果布尔表达式为true将执行的语句
}
if(布尔表达式){
//如果布尔表达式的值为true
}else{
//如果布尔表达式的值为false
}
fill()
public static void fill(arrayname,value)
public static void fill(arrayname ,starting index ,ending index ,value)
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()
public static void sort(Object[] arrayname)
对一个数组的所有元素进行排序,并且是按从小到大的顺序public static void sort(Object[] arrayname,int fromIndex, int toIndex)
对数组部分排序,也就是对数组a的下标从fromIndex到toIndex-1的元素排序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+" ");
}
}
}
2 5 30 75 85 66 -18 0
-18 0 2 5 30 66 75 85
binarySearch()
public static int binarySearch(Object[] a,Object key)
在一个数组的所有元素中进行查找public static int binarySearch(Object[] a,int fromIndex,int toIndex,Object key)
在该数组指定的范围内进行查找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`
equals()
public static boolean equals(Object[] arrayname,Object[] arrayname2)
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
copyOf()
将原始数组的元素,复制到新的数组中,可以设置复制的长度(即需要被复制的元素个数) public static Object[] copyOf(original,newLength)
copyOfRange()
将某个范围内的元素复制到新的数组中 public static Object[] copyOfRange(original,int from,int to)
from为拷贝的开始位置(包含),to为拷贝的结束位置(不包含)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]
Deque stack = new ArrayDeque();
stack.push(Integer.parseInt(arr[i]));
stack.peek()
stack.pop();
List list = new ArrayList();
String str = list.toString();
String.join(" ", nums);
list.add(factor);
String[] arr = data.split(", ");
nums = Arrays.asList(data.split(" "));
str.substring(1, str.length() - 1);
data.isEmpty()
length
size()
Integer.MIN_VALUE
Integer.MAX_VALUE
String.valueOf(root.val)
Integer.parseInt(nums.get(i))
min()
4public 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
compareTo()
5public int compareTo( NumberSubClass referenceName )
public class Test{
public static void main(String args[]){
Integer x = 5;
System.out.println(x.compareTo(3));
System.out.println(x.compareTo(5));
System.out.println(x.compareTo(8));
}
}
1
0
-1
本文撰写过程中使用的其他参考资料: