Arrays类
package demo02;
import java.util.Arrays;
将数组变成字符串用toString,字符串变数组用toCharArray。
Arrays类位于java.util包下,是一个与数组相关的工具类,里面提供了大量的静态方法,用来实现数组的常见的操作。
* Arrays类的常用方法:
public static String toString(数组);将参数数组变成字符串,格式:[s,d,a]
public static void sort();将参数数组升序(从小到大)排序,数字从小到大,字母从a到z
备注:如果是数值,从小到大
如果是字符串,按字母升序
如果是自定义的类型,那么这个自定义的类需要有Comparable或者Comparator接口支持。
* */
public class Demo01Arrays {
public static void main(String[] args) {
int[] arr = {1,2,3};
String str = Arrays.toString(arr);
System.out.println(str);
int[] arr1 = {1,24,546,5,23,63,};
Arrays.sort(arr1);
System.out.println(Arrays.toString(arr1));
}
}
Arrays类常用方法练习
package demo02;
import java.util.Arrays;
public class Demo03ArraysTest {
public static void main(String[] args) {
String str = "adagguia214525436IKGIGI";
char[] chars = str.toCharArray();
Arrays.sort(chars);
for (int i = chars.length - 1; i >= 0; i--) {
System.out.print(chars[i]);
}
}
}
Math类
package demo02;
* Math类位于java.util包下,是数学相关的工具类,里面提供大量的静态方法,完成与数学运算相关的操作。
* public static double abs(double num):获取绝对值
* public static double ceil(double num);向上取整
* public static double floor(double num);向下取整
* public static long round(double num);四舍五入,本质是加0.5取整。
* Math.PI圆周率。
* */
public class Demo02Math {
public static void main(String[] args) {
double a = 32.0;
double b = -12.5;
double a1 = Math.abs(a);
double b1 = Math.abs(b);
double c = 10.6;
double d = 12.9;
double c1 = Math.ceil(c);
double d1 = Math.ceil(d);
double e = 11.1;
double f = 18.9;
double e1 = Math.floor(e);
double f1 = Math.floor(f);
double g = 12.4;
double h = 19.9;
double g1 = Math.round(g);
double h1 = Math.round(h);
System.out.println(a1);
System.out.println(b1);
System.out.println(c1);
System.out.println(d1);
System.out.println(e1);
System.out.println(f1);
System.out.println(g1);
System.out.println(h1);
}
}
Math类常用方法练习
package demo02;
题目:
计算在-10.8到5.9之间,绝对值大于6或者小于2.1的整数有多少个?
分析:
确定了范围,for
起点位置-10.8转化成-10,两种办法:
1.可以使用Math.ceil向上取整
2.强转成int,自动舍弃所有小数位
每个数字都是整数,所以步进表达式是num++,这样每次都是+1的
一旦发现一个数字,需要计数器++
* */
public class MathTest {
public static void main(String[] args) {
int count = 0;
double max = 5.9;
double min = -10.8;
for (int i = (int)min; i < max;i++){
int abs = Math.abs(i);
if(abs > 6 || abs < 2.1){
System.out.println(i);
count++;
}
}
System.out.println("符合要求的数的个数为:" + count);
}
}