Arrays是对数组进行操作的类,它也属于java.util包,这里主要介绍Arrays类中的两个工具。
一,public static String toString()
toString的作用它可以将数组转换成字符串
例如:
int [] str={10,20,30,40,50};
String str2=Arrays.toString(str);
二,public static void sort()
sort的作用是对数组进行排序。
例如:
int [] str3={20,3,47,15,35};
Arrays.sort(str3);
注:如果是数值,是按从小到大排序,如果是字符串,则会按照字母的升序进行排序。
代码实现:
package chap11;
import java.util.Arrays;
public class Segment1 {
public static void main(String[] args) {
int [] str={10,20,30,40,50};
String str2=Arrays.toString(str);//将数组变成字符串
System.out.println(str2);
int [] str3={20,3,47,15,35};
Arrays.sort(str3);//将数组进行升排序
for (int i = 0; i < str3.length; i++) {
System.out.println(str3[i]);
}
}
}
注:使用Arrays类时记得导包,import java.util.Arrays;?
运行:
D:\Java\jdk1.8.0_271\bin\java.exe "-javaagent:D:\java\IntelliJ IDEA Community Edition 2021.1.1\lib\idea_rt.jar=52624:D:\java\IntelliJ IDEA Community Edition 2021.1.1\bin" -Dfile.encoding=UTF-8 -classpath D:\java\jdk1.8.0_271\jre\lib\charsets.jar;D:\java\jdk1.8.0_271\jre\lib\deploy.jar;D:\java\jdk1.8.0_271\jre\lib\ext\access-bridge-64.jar;D:\java\jdk1.8.0_271\jre\lib\ext\cldrdata.jar;D:\java\jdk1.8.0_271\jre\lib\ext\dnsns.jar;D:\java\jdk1.8.0_271\jre\lib\ext\jaccess.jar;D:\java\jdk1.8.0_271\jre\lib\ext\jfxrt.jar;D:\java\jdk1.8.0_271\jre\lib\ext\localedata.jar;D:\java\jdk1.8.0_271\jre\lib\ext\nashorn.jar;D:\java\jdk1.8.0_271\jre\lib\ext\sunec.jar;D:\java\jdk1.8.0_271\jre\lib\ext\sunjce_provider.jar;D:\java\jdk1.8.0_271\jre\lib\ext\sunmscapi.jar;D:\java\jdk1.8.0_271\jre\lib\ext\sunpkcs11.jar;D:\java\jdk1.8.0_271\jre\lib\ext\zipfs.jar;D:\java\jdk1.8.0_271\jre\lib\javaws.jar;D:\java\jdk1.8.0_271\jre\lib\jce.jar;D:\java\jdk1.8.0_271\jre\lib\jfr.jar;D:\java\jdk1.8.0_271\jre\lib\jfxswt.jar;D:\java\jdk1.8.0_271\jre\lib\jsse.jar;D:\java\jdk1.8.0_271\jre\lib\management-agent.jar;D:\java\jdk1.8.0_271\jre\lib\plugin.jar;D:\java\jdk1.8.0_271\jre\lib\resources.jar;D:\java\jdk1.8.0_271\jre\lib\rt.jar;D:\java\IdeaProjects\zlwjava\out\production\zlwjava chap11.Segment1
[10, 20, 30, 40, 50]
3
15
20
35
47
Process finished with exit code 0
|