包装类:基本数据类型对应的引用类型
byte Byte short Short char Character int Integer long Long double Double boolean Boolean
字符串类型的整数转成int类型的整数 int i = Integer.parseInt(“123”)
集合不能存储基本数据类型
ArrayList list = new ArrayList<>();
数字转为2进制
public static String toBinaryString(int number){
StringBuilder sb = new StringBuilder();
while(true){
if(number == ){
break;
}
in remaindar = num % 2;
sb.inseret(0,remaindar);
number = number / 2;
}
return sb.toString();
}
二分查找:
`public class erfen {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7,8,9,0};
int index = binarysearch(arr,6);
System.out.println(index);
}
public static int binarysearch(int[] arr, int number) {
int max = arr.length - 1;
int min = 0;
while (true) {
int mid = (min+max)/2;
if (min > max) {
return -1;
}
if(arr[mid]>number){
max = mid -1;
}else if (arr[mid]<number){
min = mid+1;
}else{
return mid;
}
}
}
}
冒泡排序:
int[] arr = {2,3,1,4,5};
for (int i = 0; i < arr.length-1; i++) {
for(int j = 0;j <arr.length-1-i;j++){
if(arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
选择排序:
int[] arr = {11,2,3,4,5};
for (int i = 0; i < arr.length-1; i++) {
for(int j = i+1;j<arr.length;j++){
if(arr[i]>arr[j]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
快速排序:
public class QuickSort {
public static void main(String[] args) {
int[] arr = {6, 1, 2};
quickSort(arr, 0, arr.length - 1);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public static void quickSort(int[] arr, int i, int j) {
int start = i;
int end = j;
if (start > end) {
return;
}
int baseNumber = arr[i];
while (start != end) {
while (true) {
if (end <= start || arr[end] < baseNumber) {
break;
}
end--;
}
while (true) {
if (end <= start || arr[start] > baseNumber) {
break;
}
start++;
}
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
int temp = arr[i];
arr[i] = arr[start];
arr[start] = temp;
// System.out.println(start+" "+end);
quickSort(arr, i, start - 1);
quickSort(arr, start + 1, j);
}
}
插入排序:
public class Insert {
public static void main(String[] args) {
int[] arr = {3, 24, 5, 6, 1, 2};
int startIndex = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > arr[i + 1]) {
startIndex = i + 1;
break;
}
}
for (int i = startIndex; i < arr.length; i++) {
int j = i;
while (j > 0 && arr[j] < arr[j - 1]) {
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
j--;
}
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
ArraysAPI int[] arr = {1,2,3};
Arrays.toString(arr) > [1,2,3]
Arrays.binarySearach(arr,1) 返回索引 不存在返回:-插入点减一 。例4 返回-4
Arrays.copyOf(arr,10)
Arrays.fill(arr,100) 数据填充
Arrays.sort(arr)
Lambda表达式:
可以用来简化匿名内部类的书写
只能简化函数式接口的匿名内部类的写法
函数式接口:有且仅有一个抽象方法的接口叫做函数式接口