import java.util.Arrays是一个与数组相关的工具类,里面提供了大量静态方法,用来实现数组常见的操作。 public static String toString(数组):将参数数组变成字符串(按照默认格式:[元素1,元素2,元素3...]) pubilc static void sort(数组),按照默认升序
public class Demo01Arrays {
public static void main(String[] args) {
int[] intArray = {1,2,3};
//将int[]数组按照默认格式变成字符串
String intStr = Arrays.toString(intArray);
System.out.println(intStr);
int[] array1 = {5,6,2,1,3,9,8,7};
Arrays.sort(array1);
System.out.println(Arrays.toString(array1));
String[] array2 = {"b","c","a"};
Arrays.sort(array2);
System.out.println(Arrays.toString(array2));
}
}
练习
请使用Arrays相关的API,将一个随机字符串中的所有字符升序排列,并倒序打印
import java.util.Arrays;
public class Test01ArraysTest {
public static void main(String[] args) {
//定义一个随机字符串
String line = "s8asg1re4v";
//转换为字符串数组
char[] chars = line.toCharArray();
//升序排序
Arrays.sort(chars);
//倒序打印
for (int i=chars.length-1;i>=0;i--) {
System.out.print(chars[i]+" ");
}
}
}
public static double abs(double num):获取绝对值 public static double ceil(double num):向上取整 public static double floor(double num):向下取整 public static long round(double num):四舍五入 Math.PI代表近似的圆周率常量(double)
练习
计算在-10.8到5.9之间,绝对值大于6或者小于2.1的整数有多少个?
public class Test02MathPractise {
public static void main(String[] args) {
int count = 0;
double min = -10.8;
double max = 5.9;
for (int i = (int)min; i < max; i++) {
int abs = Math.abs(i);
if (abs>6 || abs<2.1) {
System.out.print(i+" ");
count++;
}
}
System.out.println("总共有"+count+"个");
}
}