package exercise;import java.util.Arrays;public class ArraysDemo {public static void main(String[] args) {int[] arr = {1, 2, 3, 4, 5};//将数组变成字符串System.out.println(Arrays.toString(arr));//二分查找法查找元素//细节1:1.数组必须是有序的 2.元素必须是升序的//细节2:如何要查找的元素是存在的,那么返回真实的索引值//相反,返回的是 - 插入点 - 1System.out.println(Arrays.binarySearch(arr, 3));//拷贝数组copyOfint[] newArr = Arrays.copyOf(arr, 6);System.out.println(Arrays.toString(newArr));//拷贝数组copyOfRange(指定范围)//包头不包尾,包左不包右int[] newArr2 = Arrays.copyOfRange(arr, 1, 4);System.out.println(Arrays.toString(newArr2));//填充数组fillArrays.fill(newArr2, 10);System.out.println(Arrays.toString(newArr2));//排序(底层默认快速排序),升序排序int[] arr2 = {9, 8, 7, 6, 5, 4, 3, 2, 1};Arrays.sort(arr2);System.out.println(Arrays.toString(arr2));}
}
针对于sort,如何让其降序排序
package exercise;import java.util.Arrays;
import java.util.Comparator;public class Sort {public static void main(String[] args) {//public static void sort(数组,排序规则)按照指定的规则排序//参数一:要排序的数组//参数二:排序的规则//细节://只能给引用数据类型的数组进行排序//如果数组是基本数据类型的,需要变成其对于的包装类Integer[] arr = {1, 5, 3, 9, 2};//第二个参数是一个接口,所以我们在调用方法的时候,需要传递这个接口的实现类对象,作为排序的规则。//但是这个实现类,我只要使用一次,所以就没有必要单独的去写一个类,直接采取匿名内部类的方式就可以了//简单理解://o1-o2: 升序//o2-o1: 降序Arrays.sort(arr, new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {return o1 - o2;}});System.out.println(Arrays.toString(arr));}
}
package exercise;public class LambdaDemo {public static void main(String[] args) {method(new Swinmming() {@Overridepublic void swim() {System.out.println("重写swim方法~");}});//Lambda表达式method(() -> {System.out.println("重写swim方法~");});}public static void method(Swinmming s) {s.swim();}
}
@FunctionalInterface
interface Swinmming {public abstract void swim();
}
package exercise;import java.util.Arrays;
import java.util.Comparator;public class LambdaDemo1 {public static void main(String[] args) {//lambda的省略规则://1.参数类型可以省略不写。//2.如果只有一个爹数,参数类型可以省略,同时()也可以省略。//3.如果Lambda表达式的方法体只有一行,大括号,分号,return可以省略不写,需要同时省略。Integer[] arr = {4, 3, 2, 7, 6, 9, 1};Arrays.sort(arr, new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {return o1 - o2;}});//Lambda完整表达式Arrays.sort(arr, (Integer o1, Integer o2) -> {return o1 - o2;});//Lambda简写表达式Arrays.sort(arr, (o1, o2) -> o1 - o2);System.out.println(Arrays.toString(arr));}
}
package exercise;import java.util.Arrays;
import java.util.Comparator;public class Demo1 {public static void main(String[] args) {String[] arr = {"yang", "ze", "chao1", "sunshine", "lulushui2"};Arrays.sort(arr, (o1, o2) -> o1.length() - o2.length());System.out.println(Arrays.toString(arr));}
}
GF类:
package exercise;import java.util.Arrays;
import java.util.Comparator;public class GfTest {public static void main(String[] args) {GirlFriend gf1 = new GirlFriend("sunshine", 18, 172);GirlFriend gf2 = new GirlFriend("lulushui", 19, 171);GirlFriend gf3 = new GirlFriend("jiuselu", 19, 171);GirlFriend[] arr = {gf1, gf2, gf3};//精简版Lambda表达式Arrays.sort(arr, (o1,o2)-> {if (o1.getAge() != o2.getAge()) {return o1.getAge() - o2.getAge();} else if (o1.getHeight() != o2.getHeight()) {return o1.getHeight() - o2.getHeight();}return o1.getName().compareTo(o2.getName());});System.out.println(Arrays.toString(arr));}
}
package exercise;public class Demo2 {public static void main(String[] args) {int sum = 0;for (int i = 1; i <= 12; i++) {sum += recursion(i);}System.out.println(sum);}private static int recursion(int n) {if (n == 1 || n == 2) {return 1;}return recursion(n - 1) + recursion(n - 2);}
}
解析:
package exercise;public class Demo3 {public static void main(String[] args) {int number = 1;System.out.println(recursion(number));}private static int recursion(int day) {if (day <= 0 || day >= 11) {return -1;}if (day == 10) {return 1;}return (recursion(day + 1) + 1) * 2;}
}
解析:类似于斐波那契数列,20个台阶爬法=19个台阶爬法+18个台阶爬法。
package exercise;public class Demo4 {public static void main(String[] args) {System.out.println(getCount(20));}private static int getCount(int n) {if (n == 1) {return 1;}if (n == 2) {return 2;}return getCount(n - 1) + getCount(n - 2);}
}