Java 编程基础

1、入门

设置环境变量: 

Path=C:\Java\jdk\bin
ClassPath=.;C:\Java\jdk\jre\lib\rt.jar;C:\Java\jdk\lib\tools.jar;C:\Java\jdk\lib\dt.jar;
JAVA_HOME=C:\Java\jdk; 

Java程序开发过程:

Java源文件(*.java) -> 编译 -> Java类文件(*.class)(由字节码构成) -> JVM翻译并执行 

源文件 Hello.java :

View Code 
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }

  编译命令:javac Hello.java       其会生成类文件 Hello.class

执行命令:java Hello  

将多个类打包成 jar 包:

jar cvf First.jar Hello.class Welcome.class   // 打包指定的类

jar cvf First.jar First/    //打包指定的目录(文件夹)

2、基本语法

三种注释:

1、单行注释://          

2、多行注释:/*     */

3、文档注释:/**     */            生成Java文档时使用

View Code 
/*
名称:Hello.java
作者:彭飞
日期:2012-8-8
*/
public class Hello {
    public static void main(String[] args) {
        //屏幕中输出Hello World!
        System.out.println("Hello World!");
    }

  变量、数据类型、运算符: 

View Code 
public class Hello {
    public static void main(String[] args) {
        //布尔型
        boolean b = true// false
        
//字符型
        char c = '飞'; // 'A'
        
//整型
        int i = 100; // byte short long
        
//实数型
        float f = 1.23F; // double
        
//字符串
        String s = "彭飞";
                
        //运算符
        
        
//单目运算符:逻辑非  !     增减运算符  ++、--
        
//双目运算符:算数运算符  +、-、*、/、%     关系运算符  >、<=、==、!=     逻辑运算符  &&、||
        
//三目运算符:?:
        
//对象运算符:     A instanceof B  如果A是B的对象,则返回true;否则返回false         
        if ("abc" instanceof String) {
            String ss = (String) "abc";
            System.out.println(ss);
        }
    }

3、流程控制语句  

View Code 
public class Hello {
    public static void main(String[] args) {
        // if语句
        int su = 78;
        if (su >= 100 && su < 1000) {
            System.out.println("3位数");
        } else if (su >= 10) {
            System.out.println("2位数");
        } else if (su >= 10) {
            System.out.println("1位数");
        } else {
            System.out.println("无法作出判断");
        }

        // switch语句
        int a = 1;
        switch (a) { // 注意:n 的值可以为整数或字符,但绝不能为实数或字符串
        case 1:
            System.out.println("111");
        case 2:
            System.out.println("222");
            break;
        case 3:
            System.out.println("333");
            break;
        case 4:
            System.out.println("444");
            break;
        default:
            System.out.println("未知");
        }

        // for语句
        int sum = 0;
        for (int i = 1; i < 10; i++) {
            sum += i;
        }
        System.out.println("1-10的和为:" + sum);

        int[] arr = { 2, 4, 6, 8, 10 };
        for (int n : arr) {
            System.out.print(n + " ");
        }

        // break、continue语句
        for (int i = 1; i <= 10; i++) {
            if (i == 6) {
                break;
            }
            System.out.print(i + " ");// 1 2 3 4 5
        }

        for (int i = 1; i <= 10; i++) {
            if (i == 6) {
                continue;
            }
            System.out.print(i + " ");// 1 2 3 4 5 7 8 9 10
        }

        // while语句
        int c = 1;
        while (c <= 3) {
            System.out.println(c);
            c++;
        }

        // do-while语句
        int d = 1;
        do {
            System.out.println(d);
            d++;
        } while (d <= 3);
    }

4、数组

View Code 
public class Hello {
    public static void main(String[] args) {
        // 一维数组
        int[] a = new int[10];
        int[] b = new int[] { 2, 4, 6, 8, 11 };
        int[] c = { 2, 4, 6, 8, 10 };
        String[] s = new String[5];
        for (int d : b) {
            System.out.println(d);
        }
        // 二维数组
        String[][] arr = new String[2][];
        arr[0] = new String[2];
        arr[1] = new String[3];
        arr[0][0] = "A";
        arr[0][1] = "B";
        arr[1][0] = "C";
        arr[1][1] = "D";
        arr[1][2] = "E";
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.println(arr[i][j]);
            }
        }
    }

5、类与对象

类、实例化: 

View Code 
public class Hello {
    public static void main(String[] args) {
        // 对象实例化
        Person p1 = new Person();
        p1.SetName("pengfei");
        String str = p1.GetName();
        p1.Run();
        // 引用赋值
        Person p2 = p1;
        p2.Run();
    }
}

class Person {
    // 成员变量
    String Name;

    // 多参方法 、this引用
    void SetName(String Name) {
        this.Name = Name;
    }

    String GetName() {
        return this.Name;
    }

    // 无参方法、void返回类型
    void Run() {
        System.out.println(this.Name + "在跑啊跑...");
    }

  静态变量与静态方法:

普通成员变量只有在创建了类的对象后才能被使用。与此相反,静态变量可以不先创建对象而直接使用,是所有对象共有的变量。 

View Code 
public class Hello {
    //实例成员变量
    String Name;
    //静态变量
    static String ClassName;
    //静态方法
    static String Welcome(String name){
        return "Hello " + name;
    }
    
    public static void main(String[] args) {
        //静态变量直接通过类名访问
        Hello.ClassName = "Java";
        //实例变量,必须实例化后才可访问
        Hello h = new Hello();
        h.Name = "pengfei";
        //静态方法直接通过类名访问
        String s = Hello.Welcome(h.Name);        
        System.out.println(s);
    }

  只有在对象存在的条件下,才可以使用实例的成员变量与成员方法。然而,静态方法则不需要首先创建对象,可以通过类名直接调用它。

  包与导入:

View Code 
//创建包,并在包中定义类
package MyPack.First;

public class Person {
    public void Run() {
        System.out.println("人在跑啊跑...");
    }
View Code 
//导入包中所有的类
import MyPack.First.*;
//import MyPack.First.Person;

//导入包中某个类
import java.util.Date;

public class Hello {
    public static void main(String[] args) {
        Person p = new Person();
        p.Run();

        Date d = new Date();
        System.out.println(d);
    }

访问控制符:

1、类成员访问控制符:

private:仅在类内部可以访问

public:所有类都可以访问

default(friendly): 只有同一个包中的类才能访问

protected:不仅同一个包中的类,而且位于其他包中的子类也可以访问

2、类访问控制符: 

public:所有类都可以访问

default(friendly): 只有同一个包中的类才能访问

  重载:是指在同一个类中定义多个同名但内容不同的成员方法。 但它们的参数的个数及数据类型都不相同,返回值类型是否相同无关紧要。

View Code 
    void hi() {
        System.out.println("你好~~");
    }

    void hi(String name) {
        System.out.println(name + " , 你好~~");
    }

    void hi(String name1, String name2) {
        System.out.println(name1 + "、" + name2 + " 你好~~");

  构造函数: 

View Code 
public class Person {    
    
    // 缺省构造函数
    public Person() {
        System.out.println("人");
    }
    
    public Person(String name) {
        this();  // 调用缺省构造函数    注意:构造函数的调用必须在第一句;不能在普通方法中调用
        System.out.println(name);
    }
    
    public Person(String name,int age) {
        this(name);
        System.out.println(name + " " + age);
    }    

    public void Run() {
        System.out.println("人在跑啊跑...");
    }

   类的初始化:

View Code 
public class Person {
    //定义的时候就赋值
    public String Name = "pengfei";
    public static String ClassName = "Java";
    
    // 缺省构造函数
    public Person() {
        System.out.println("人");
    }
    public Person(String name) {
        this.Name = name;
    }

    //静态块     在类被加载到内存时会被执行。非静态成员变量不能在静态方法中使用,也不能在静态块中使用。
    static{        
        System.out.println("静态块");
        ClassName = ".NET"; //静态块主要用来初始化静态变量及静态方法 
    }
    
    public void Run() {
        System.out.println("人在跑啊跑...");
    }

对象创建与销毁:实例化(构造函数);垃圾收集器(后台线程)在回收对象时会自动调用对象的 finalize() 方法来释放系统资源。

什么是值传递、引用传递及其区别? 

继承:子类拥有父类的成员 

View Code 
class Person {
    public String Name;

    public Person() {
        System.out.println("Person构造函数");
    }

    public Person(String name) {
        this.Name = name;
        System.out.println("Person构造函数");
    }
}

class Student extends Person {
    public int Score;

    public Student() {
        // super(); 隐含着,调用父类构造函数
        System.out.println("Student构造函数");
    }

    public Student(String name, int score) {
        super(name); // 调用父类构造函数
        this.Score = score;
        System.out.println("Student构造函数");
    }

this 与 super :

this、this() 用来引用对象自身的成员;super、super() 用来引用继承自父类的成员。

this :一个引用对象自身的引用   

this() :本身的构造函数

super :一个用来引用继承而来的成员的引用

super()  :父类的构造函数

覆盖:

View Code 
class Person {
    public String Name;

    public void Run() {
        System.out.println("Person Running");
    }
}

public class Student extends Person {
    // 覆盖
    public void Run() {
        super.Run();// 调用父类中的Run方法
        System.out.println("Student Running");
    }

    public static void main(String[] args) {
        Student s = new Student();
        s.Run();
    }

多态:

View Code 
class Person {
    public void Run() {
        System.out.println("Person.Run");
    }
}

class Student extends Person {
    // 覆盖
    public void Run() {
        System.out.println("Student.Run");
    }

    public void Study() {
        System.out.println("Student.Study");
    }
}

class Teacher extends Person {
    // 覆盖
    public void Run() {
        System.out.println("Teacher.Run");
    }
}

public class Hello {
    public static void main(String[] args) {
        // 多态
        Person p1 = new Student();
        p1.Run();
        Person p2 = new Teacher();
        p2.Run();
        // 类型转换
        Student s = (Student) p1;
        s.Study();
    }

Object 类:所有类的父类,所以 Object 型引用变量可以引用所有的对象。

抽象类:

View Code 
//抽象类
abstract class Person {
    // 抽象方法
    public abstract void Run();

包含抽象方法的类称为抽象类,用 abstract 修饰,抽象类无法创建其对象。

注意:继承抽象类的子类一定要覆盖所继承的抽象方法,因为若不这样做,子类也将成为抽象类。

final 关键字:

public static final double PI = 3.1415926;   // 常量,首次赋值后,后期无法再次赋值。 

public final void f() {...}   // 最终方法,不能被覆盖。

final class MyClass {...}   // 该类不能被继承

接口:

View Code 
//接口
interface IDao {
    public void Show();
}

abstract class Person {
    public String Name;
}

// 实现接口
class Student implements IDao {
    @Override
    public void Show() {
        System.out.println("实现接口");
    }
}

// 继承一个类,同时实现接口
class Teacher extends Person implements IDao {
    public void Run() {
        System.out.println("Teacher.Run");
    }

    @Override
    public void Show() {
        System.out.println("实现接口");
    }

接口中所有方法都是抽象的,所有变量都是静态的(static)和最终的(final)。

实现接口的类只有全部覆盖接口中的所有抽象方法,才能创建对象,类实现接口使用 implements 关键字。

在类的继承中,只能做单一继承,而实现接口时,一次可实现多个接口,这些接口间使用逗号分隔。

一个类不仅可以继承其它类,也可以在继承其它类的同时实现接口。 

Cloneable 接口: 

View Code 
/*
 *克隆,产生对象的副本
 *1、创建该对象的类必须实现  接口
 *2、创建该对象的类必须覆盖Object类的clone方法
 
*/

public class Person implements Cloneable {
    public String Name;

    public Person(String name) {
        this.Name = name;
    }

    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            return null;
        }
    }

    public static void main(String[] args) {
        Person p1 = new Person("pengfei");
        Person p2 = (Person) p1.clone(); // 生成p1的副本
    }

Enumeration 接口:

View Code 
import java.util.Enumeration;

public class Person implements Enumeration {
    private Object[] data;
    private int count = 0;
    public Person(Object[] data){
        this.data = data;
    }    

    @Override
    public boolean hasMoreElements() {        
        return count < data.length;
    }

    @Override
    public Object nextElement() {        
        if(count<data.length){
            return data[count++];
        }
        return null;
    }
    
    public static void main(String[] args) {
        String[] arr = {"pengfei","zhangsan","lisi"};
        Person p = new Person(arr);
        
        while(p.hasMoreElements()){
            System.out.println(p.nextElement());
        }
    }

匿名类: 

View Code 
//接口
interface IDao {
    public void Show();
}

public class Hello {
    public static void main(String[] args) {
        // 匿名类
        IDao dao = new IDao() {
            @Override
            public void Show() {
                System.out.println("实现接口");
            }
        };
        dao.Show();
    }

异常处理:

View Code 
public class Hello {
    public static void main(String[] args) throws Exception {
        try {
            // 可能产生异常的代码段
        } catch (NullPointerException e) {
            // 错误处理
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println(e.getMessage());
        } catch (Exception e) {
            throw e;
        } finally {
            // 无论是否发生异常,总会被无条件执行
        }
    }

6、文件读写(I/O) 

基本操作:

View Code 
import java.io.*;

public class Hello {
    public static void main(String[] args) throws Exception {
        // 实例化,文件
        File f1 = new File("d:\\abc.txt");
        // 实例化,文件夹
        File f2 = new File("d:\\test");
        // 若当前所指文件不存在,则创建之,并返回 true;若当前文件存在,则返回 false
        boolean b1 = f1.createNewFile();
        // 删除当前对象所指文件
        boolean b2 = f1.delete();
        // 测试当前对象所指文件是否存在
        boolean b3 = f1.exists();
        // 测试当前对象是否为文件
        boolean b4 = f1.isFile();
        // 测试当前对象是否为文件夹
        boolean b5 = f1.isDirectory();
        // 文件名称
        String s1 = f1.getName();
        // 文件所在目录
        String s2 = f1.getParent();
        // 文件路径
        String s3 = f1.getPath();
        // 文件大小
        long l = f1.length();
        // 若代表目录,则返回此目录内所有文件名;若非目录,则返回 null
        String[] a1 = f2.list();
        // 若代表目录,则返回此目录内所有文件的File对象;若非目录,则返回 null
        File[] a2 = f2.listFiles();
        // 使用当前对象创建一目录
        boolean b6 = f2.mkdir();
    }

  字符流:

View Code 
import java.io.*;

public class Hello {
    public static void main(String[] args) throws Exception {
        // 文件写
        FileOutputStream fos = new FileOutputStream("d:\\abc.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        BufferedWriter bw = new BufferedWriter(osw);
        bw.write("彭飞");
        bw.newLine();
        bw.write("您好");
        bw.newLine();
        bw.close();
        // 文件读
        FileInputStream fis = new FileInputStream("d:\\abc.txt");
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);
        String str = null;
        while ((str = br.readLine()) != null) {
            System.out.println(str);
        }
        br.close();
    }

View Code 
import java.io.*;

public class Hello {
    public static void main(String[] args) throws Exception {
        // 文件写
        FileWriter fw = new FileWriter("d:\\abc.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write("彭飞");
        bw.newLine();
        bw.write("您好");
        bw.newLine();
        bw.close();
        // 文件读
        FileReader fr = new FileReader("d:\\abc.txt");
        BufferedReader br = new BufferedReader(fr);
        String str = null;
        while ((str = br.readLine()) != null) {
            System.out.println(str);
        }
        br.close();
    }

View Code 
import java.io.*;

public class Hello {
    public static void main(String[] args) throws Exception {
        // 键盘获取数据
        System.out.print("输入数据:");
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String str = br.readLine();
        System.out.println("输入的字符串:" + str);
    }

对象序列化:

View Code 
import java.io.*;

public class Hello {
    public static void main(String[] args) throws Exception {
        Person p = new Person();
        p.Name = "彭飞";
        p.Sex = '男';
        p.Age = 27;
        // 将对象写入
        FileOutputStream fos = new FileOutputStream("d:\\obj.dat");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(p);
        oos.close();
        // 将对象读取
        FileInputStream fis = new FileInputStream("d:\\obj.dat");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Person p1 = (Person) ois.readObject();
        ois.close();
        System.out.println(p1.Name);
    }
}

// 可序列化的对象必须实现 Serializable 接口
class Person implements Serializable {
    public String Name;
    public char Sex;
    public int Age;

7、线程

Runtime 、Process 类:

View Code 
public class Hello {
    public static void main(String[] args) throws Exception {
        // 获取Runtime Object 对象,该对象当Java程序运行时才会产生,所以无法 new
        Runtime rt = Runtime.getRuntime();
        // 执行指定的命令
        Process pr = rt.exec("c:\\windows\\notepad.exe");

        System.out.println("最大内存:" + rt.maxMemory());
        System.out.println("总内存:" + rt.totalMemory());
        System.out.println("空闲内存:" + rt.freeMemory());
        // 运行垃圾回收器,增加可用内存
        rt.gc();
    }

线程:必须实现了 Runnable 接口

两种创建方式: 

View Code 
class MyFirstThread implements Runnable {
    public String Name;

    public MyFirstThread(String name) {
        this.Name = name;
    }

    @Override
    public void run() {
        for (int i = 0; i <= 10; i++) {
            System.out.println(this.Name + i);
        }
    }
}

public class Hello {
    public static void main(String[] args) throws Exception {
        MyFirstThread mft = new MyFirstThread("pengfei");
        Thread t = new Thread(mft);
        t.start();
    }

View Code 
class MySecondThread extends Thread {

    @Override
    public void run() {
        for (int i = 0; i <= 10; i++) {
            System.out.println(super.getName() + i);
        }
    }
}

public class Hello {
    public static void main(String[] args) throws Exception {
        MySecondThread mft = new MySecondThread();
        mft.start();
    }

以上第二种方法显然更简便,但是,如果你的类继承了 Thread 类,就无法再继承其他类了。  

线程同步:

View Code 
class MyTestThread extends Thread {
    private char ch;
    static Object printer = new Object();

    public MyTestThread(char ch) {
        this.ch = ch;
    }

    public void print() {
        for (int i = 0; i < 10; i++) {
            System.out.print(this.ch);
        }
    }

    @Override
    public void run() {
        // 同步块
        synchronized (printer) {
            for (int i = 0; i <= 5; i++) {
                print();
                System.out.println();
            }
        }
    }
}

public class Hello {
    public static void main(String[] args) throws Exception {
        MyTestThread mt1 = new MyTestThread('A');
        MyTestThread mt2 = new MyTestThread('B');
        MyTestThread mt3 = new MyTestThread('C');
        mt1.start();
        mt2.start();
        mt3.start();
    }

View Code 
class Printer {
    // 同步化方法
    public synchronized void print(char ch) {
        for (int i = 0; i < 10; i++) {
            System.out.print(ch);
        }
    }
}

class MyTestThread extends Thread {
    private char ch;
    private Printer priter;

    public MyTestThread(Printer priter, char ch) {
        this.ch = ch;
        this.priter = priter;
    }

    @Override
    public void run() {
        for (int i = 0; i <= 5; i++) {
            priter.print(ch);
            System.out.println();
        }
    }
}

public class Hello {
    public static void main(String[] args) throws Exception {
        Printer priter = new Printer();
        MyTestThread mt1 = new MyTestThread(priter, 'A');
        MyTestThread mt2 = new MyTestThread(priter, 'B');
        mt1.start();
        mt2.start();
    }

8、常用API

java.lang.String

View Code 
public class Hello {
    public static void main(String[] args) throws Exception {
        String str = new String("pengfei");
        // 返回字符串中 index 位置处的字符
        char c1 = str.charAt(3);
        // 比较两字符串的大小。若当前字符串大,就返回正整数;当前字符串小,就返回负整数;若相等,则返回 0
        int n1 = str.compareTo("peng");
        // 比较两字符串的大小,比较时,忽略大小写
        int n2 = str.compareToIgnoreCase("peng");
        // 在当前字符串尾部追加字符串 s
        String s1 = str.concat("hello");
        // 判断当前字符串是否以 s 结尾
        boolean b1 = str.endsWith("fei");
        // 判断当前字符串是否与 s 相同
        boolean b2 = str.equals("pengfei");
        // 判断当前字符串是否与 s 相同,比较时,忽略大小写
        boolean b3 = str.equalsIgnoreCase("pengfei");
        // 返回字符串 s 在当前字符串中首次出现的位置下标
        int n3 = str.indexOf("peng");
        // 从当前字符串下标为 index 处开始查找,并返回字符串 s 在当前字符串中首次出现的位置下标
        int n4 = str.indexOf("e", 2);
        // 返回当前字符串的长度
        int n5 = str.length();
        // 字符串替换
        String s2 = str.replace('e', 'E');
        // 返回当前字符串的一个子字符串
        String s3 = str.substring(3);
        // 将当前字符串转换为小写形式
        String s4 = str.toLowerCase();
        // 将当前字符串转换为大写形式
        String s5 = str.toUpperCase();
        // 去掉字符串的前后空格
        String s6 = str.trim();
        // 将各数据类型转换为字符串
        String s7 = String.valueOf("123");
    }

java.lang.StringBuffer

View Code 
public class Hello {
    public static void main(String[] args) throws Exception {
        StringBuffer sb = new StringBuffer();
        // 追加到当前字符串的尾部
        sb.append("peng");
        sb.append("fei");
        // 在当前字符串中,删除从下标 n 到 下标 m 的字符
        sb.delete(1, 3);
        // 删除当前字符串中下标为 index 的字符
        sb.deleteCharAt(2);
        // 将字符串反转
        sb.reverse();
        // 其余方法与 String 类中的功能相同
    }

HashSet  

View Code 
import java.util.*;

public class Hello {
    public static void main(String[] args) throws Exception {
        /*
         * Set接口 -> HashSet、TreeSet 1、集合内不允许有重复的数据存在 2、集合内数据没有顺序
         
*/
        Set<String> set = new HashSet<String>();
        // 向集合添加元素
        set.add("peng");
        set.add("fei");
        // 获取集合的元素个数
        int size = set.size();
        // 将对象从集合中移除
        set.remove("peng");
        // 测试对象是否存在于集合中
        boolean b1 = set.contains("fei");
        // 测试集合是否为空
        boolean b2 = set.isEmpty();
        // 移除集合中所有元素
        set.clear();
    }

ArrayList

View Code 
import java.util.*;

public class Hello {
    public static void main(String[] args) throws Exception {
        /*
         * List接口 -> ArrayList、LinkedList、Vector 1、允许内部有重复的元素存在 2、内部元素有特定的顺序
         
*/
        List<String> list = new ArrayList<String>();
        // 向集合添加元素
        list.add("peng");
        list.add("fei");
        // 将元素添加至 index 位置
        list.add(1, "hello");
        // 获取集合的元素个数
        int size = list.size();
        // 将对象从集合中移除
        list.remove("peng");
        // 循环遍历集合
        for (String s : list) {
            System.out.println(s);
        }
        // 测试对象是否存在于集合中
        boolean b1 = list.contains("fei");
        // 测试集合是否为空
        boolean b2 = list.isEmpty();
        // 移除集合中所有元素
        list.clear();
    }

HashMap 

View Code 
import java.util.*;

public class Hello {
    public static void main(String[] args) throws Exception {
        /*
         * Map接口 -> HashMap、TreeMap、HashTable 1、元素拥有固定的 key 值 2、key 值不允许重复
         
*/
        Map<String, String> map = new HashMap<String, String>();
        // 向集合添加元素
        map.put("001", "peng");
        map.put("002", "fei");
        // 返回拥有 key 的元素
        String s1 = map.get("001");
        // 判断是否存在拥有 key 的元素
        if (map.containsKey("001")) {
            // 删除拥有 key 的元素
            map.remove("001");
        }
        // 返回集合中元素个数
        int length = map.size();
        //循环遍历
        for(String key : map.keySet()){
            System.out.println(map.get(key));
        }
    }

java.util.Random

View Code 
import java.util.*;

public class Hello {
    public static void main(String[] args) throws Exception {
        Random rnd = new Random();
        // 返回 true 或 false 中的一个
        boolean b = rnd.nextBoolean();
        // 返回 0 ~ n-1 之间的整数
        int n = rnd.nextInt(10);
    }

java.util.Arrays

View Code 
import java.util.*;

public class Hello {
    public static void main(String[] args) throws Exception {
        int[] arr = { 2, 4, 12, 6, 20, 18 };
        // 对数组排序
        Arrays.sort(arr);

        for (int a : arr) {
            System.out.println(a);
        }
    }

  谢谢。。。   

原文地址:https://www.cnblogs.com/pengfei/p/2629016.html