Arrays 类
java.util.Arrays
是一个与数组相关的工具类,里面提供了大量静态方法,用来实现数组常见的操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.tipdm.demo06;
import java.util.Arrays;
public class demo1 { public static void main(String[] args) { int[] inArray = {10, 20, 30}; String intStr = Arrays.toString(inArray); System.out.println(intStr);
int[] array1 = {2, 3, 1, 5, 8}; Arrays.sort(array1); System.out.println(Arrays.toString(array1));
String[] array2 = {"bbb", "aaa", "ccc"}; Arrays.sort(array2); System.out.println(Arrays.toString(array2)); } }
|
1 2 3 4 5
| [10, 20, 30] [1, 2, 3, 5, 8] [aaa, bbb, ccc]
进程已结束,退出代码0
|
题目:请使用Arrays相关的API,将一个随机字符中的所有字符升序排列,并倒序打印。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.tipdm.demo06;
import java.util.Arrays;
public class demo2 { public static void main(String[] args) { String str = "saucxkzjqwfuiosboppwequiow"; char[] charArr = str.toCharArray();
Arrays.sort(charArr);
for (int i = charArr.length - 1; i >= 0; i--) { System.out.print(charArr[i] + " "); } } }
|
1 2
| z x w w w u u u s s q q p p o o o k j i i f e c b a 进程已结束,退出代码0
|
Math 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package com.tipdm.demo06;
public class demo3 { public static void main(String[] args) { System.out.println(Math.abs(-23.2));
System.out.println(Math.ceil(23.1));
System.out.println(Math.floor(23.9));
System.out.println(Math.round(23.4)); System.out.println(Math.round(23.5));
System.out.println(Math.PI); } }
|
1 2 3 4 5 6 7 8
| 23.2 24.0 23.0 23 24 3.141592653589793
进程已结束,退出代码0
|
题目:计算在-10.8到5.9之间,绝对值大于6或者小于2.1的整数有多少个?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.tipdm.demo06;
import java.util.ArrayList;
public class demo4 { public static void main(String[] args) { double minDouble = -10.8; double maxDouble = 5.9; double min = Math.ceil(minDouble); double max = Math.floor(maxDouble); int i = (int) min; ArrayList<Integer> listInt = new ArrayList<>(); while(i <= max){ if(Math.abs(i) > 6 || Math.abs(i) < 2.1){ listInt.add(i); } i++; } System.out.println("总共有:" + listInt.size() + "个"); System.out.println(listInt); } }
|
1 2 3 4
| 总共有:9个 [-10, -9, -8, -7, -2, -1, 0, 1, 2]
进程已结束,退出代码0
|