2021-08-06 Java练习题目录:
1、使用Collections中的方法,完成List集合的排序,然后反转。
package listpractice;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/8/7 14:32
*
* 1、使用Collections中的方法,完成List<Integer>集合的排序,然后反转。
*/
public class sortReverse {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(2);
list.add(4);
list.add(2);
list.add(5);
list.add(7);
list.add(8);
list.add(9);
list.forEach((i)-> System.out.print(i+" "));
System.out.println();
Collections.sort(list);
list.forEach((i)-> System.out.print(i+" "));
System.out.println();
Collections.reverse(list);
list.forEach((i)-> System.out.print(i+" "));
}
}
2、已知:(BigDecimal使用)
数字1:3.141872872637276738268736872
数字2:4.287827398278372873982732998
(1)求和
(2)求差
(3)求积
(4)相除,保留5位小数,采用四舍五入的方式舍入。
package listpractice;
import java.math.BigDecimal;
import java.math.BigInteger;
import static java.math.BigDecimal.ROUND_HALF_UP;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/8/7 14:40
*
* 2、已知:
* 数字1:3.141872872637276738268736872
* 数字2:4.287827398278372873982732998
* (1)求和
* (2)求差
* (3)求积
* (4)相除,保留5位小数,采用四舍五入的方式舍入。
*/
public class bigDJ {
public static void main(String[] args) {
BigDecimal b1 = new BigDecimal("3.141872872637276738268736872");
BigDecimal b2 = new BigDecimal("4.287827398278372873982732998");
BigDecimal add = b1.add(b2);
BigDecimal sub = b1.subtract(b2);
BigDecimal mul = b1.multiply(b2);
BigDecimal div = b1.divide(b2,5,ROUND_HALF_UP);
System.out.println(add);
System.out.println(sub);
System.out.println(mul);
System.out.println(div);
}
}
3、编写方法,实现二分查找法。
(1)使用二分查找法,从一个int数组中查找目标元素,最后返回元素的下标。如果没有返回-1。
(2)使用二分查找法,从一个List中查找目标元素,最后返回元素的下标。如果没有返回-1。
package listpractice;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/8/7 14:48
* <p>
* 3、编写方法,实现二分查找法。
* (1)使用二分查找法,从一个int数组中查找目标元素,
* 最后返回元素的下标。如果没有返回-1。
* <p>
* (2)使用二分查找法,从一个List<String>中查找目标元素,
* 最后返回元素的下标。如果没有返回-1。
*/
public class BSearch {
public static void main(String[] args) {
Integer[] arr = {1, 5, 4, 3, 2, 3, 8, 9, 0, 4, 5};
Arrays.sort(arr);
int i = binarySearch(arr, 23);
System.out.println(i);
List<Integer> list = Arrays.asList(arr);
int l = Collections.binarySearch(list, 0);
if (l < 0) {
System.out.println(-1);
} else {
System.out.println(l);
}
}
public static int binarySearch(Object[] a, Object key) {
return binarySearch0(a, 0, a.length, key);
}
// 像公共版本,但没有范围检查
private static int binarySearch0(Object[] a, int fromIndex, int toIndex,
Object key) {
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
@SuppressWarnings("rawtypes")
Comparable midVal = (Comparable)a[mid];
@SuppressWarnings("unchecked")
int cmp = midVal.compareTo(key);
if (cmp < 0)
low = mid + 1;
else if (cmp > 0)
high = mid - 1;
else
return mid; // key found
}
return -1; // key not found.
}
}
4、编写方法,实现快速排序。
(1)使用快速排序,对int数组进行由小到大排序。
(2)使用快速排序,对List进行由大到小排序。
package listpractice;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/8/7 15:39
* <p>
* 4、编写方法,实现快速排序。
* (1)使用快速排序,对int数组进行由小到大排序。
* (2)使用快速排序,对List<String>进行由大到小排序。
*/
public class sortIL {
//(1)使用快速排序,对int数组进行由小到大排序。
public static void quickSort(int[] arr, int low, int high) {
int i, j, temp, t;
if (low > high) {
return;
}
i = low;
j = high;
//temp就是基准位
temp = arr[low];
while (i < j) {
//先看右边,依次往左递减
while (temp <= arr[j] && i < j) {
j--;
}
//再看左边,依次往右递增
while (temp >= arr[i] && i < j) {
i++;
}
//如果满足条件则交换
if (i < j) {
t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
//最后将基准为与i和j相等位置的数字交换
arr[low] = arr[i];
arr[i] = temp;
//递归调用左半数组
quickSort(arr, low, j - 1);
//递归调用右半数组
quickSort(arr, j + 1, high);
}
//(2)使用快速排序,对List<String>进行由大到小排序。
public static void quickSortList(List<String> list, int low, int high) {
int i;
int j;
String temp;
String t;
if (low > high) {
return;
}
i = low;
j = high;
//temp就是基准位
temp = list.get(i);
while (i < j) {
//先看右边,依次往左递增
while (temp.compareTo(list.get(j)) >= 0 && i < j) {
j--;
}
//再看左边,依次往右递减
while (temp.compareTo(list.get(i)) <= 0 && i < j) {
i++;
}
//如果满足条件则交换
if (i < j) {
t = list.get(j);
list.set(j, list.get(i));
list.set(i, t);
}
}
//最后将基准为与i和j相等位置的数字交换
list.set(low, list.get(i));
list.set(i, temp);
//递归调用左半数组
quickSortList(list, low, j - 1);
//递归调用右半数组
quickSortList(list, j + 1, high);
}
public static void main(String[] args) {
Random rand = new Random();
int[] i = new int[10];
for (int j = 0; j < i.length; j++) {
i[j] = rand.nextInt(10);
}
System.out.println(Arrays.toString(i));
quickSort(i, 0, i.length - 1);
System.out.println(Arrays.toString(i));
List<String> list = new ArrayList<>();
list.add("34");
list.add("se");
list.add("ze");
list.add("阿斯弗");
list.add("皮特帕克");
list.add("张");
list.add("王");
list.add(";");
list.add("$");
list.add("\\");
list.forEach((l) -> System.out.print(l + " "));
System.out.println();
quickSortList(list, 0, list.size() - 1);
list.forEach((l) -> System.out.print(l + " "));
}
}