数组

声明数组

``````int[] a;
``````

``````int[] a = new int[100];
``````

``````int[] a = {2, 3, 5, 7, 11, 13};
``````

``````String[] authors = {"James", "Kobe", "Curry", "Durant",};
``````

``````new int[] {17, 19, 23, 29, 31, 37}
``````

``````smallPrimes = new int [] {17, 19, 23, 29, 31, 37};
``````

``````new elementType[0];

new elementType2[] {};
``````

访问数组

``````int[] a = new int[100];
for (int i = 0; i < 100; i++) {
a[i] = i;
}
``````

``````String[] names = new String[10];
``````

``````for (int i=0; i < 10; i++) names[i] = "";
``````

``````for (int i=0; i<a.length; i++) {
System.out.println(a[i]);
}
``````

for each循环

Java有一种功能很强的循环结构，可以用来依次处理数组(或者其他元素集合)中的每个元素，而不必考虑指定下标值。这种增强的for循环的语句格式为：

``````for (variable: collection) statement
``````

``````int[] a = {2, 3, 4, 5, 6};
for (int element: a) {
System.out.println(element);
}
``````

``````2
3
4
5
6
``````

``````for (int i = 0;i < a.length; i++) {
System.out.println(a[i]);
}
``````

`for each循环语句的循环变量将会遍历数组中的每个元素，而不是下标值`

数组拷贝

``````public class SevenSample {
public static void main(String[] args) {
int[] smallPrimes = {2, 3, 4, 5, 6, 7, 8};
// 拷贝smallPrimes
int[] luckyNumbers = smallPrimes;
System.out.println(Arrays.toString(luckyNumbers));
}
}
``````

``````[2, 3, 4, 5, 6, 7]
``````

``````import java.util.Arrays;

public class SevenSample {
public static void main(String[] args) {
int[] smallPrimes = {2, 3, 4, 5, 6, 7};
int[] copiedLuckyNumbers = Arrays.copyOf(smallPrimes, smallPrimes.length);
System.out.println(Arrays.toString(copiedLuckyNumbers));
}
}
``````

``````[2, 3, 4, 5, 6, 7]
``````

`Array.copyOf`方法中，第1个参数是拷贝的对象，第2个参数是新数组的长度。这个方法通常用来增加数组的大小：

``````luckNumbers = Arrays.copyOf(luckyNumbers, 2 * luckyNumbers.length);
``````

①如果数组元素是数值型，那么额外的元素将被赋值为0；

②如果数组元素是布尔值，那么额外的元素将被赋值为`false`

③如果长度小于原始数组的长度，则只拷贝前面的值

命令行参数

``````public class EightSample {
public static void main(String[] args) {
if (args.length == 0 || args[0].equals("-h"))
System.out.print("Hello, ");
else if (args[0].equals("-g"))
System.out.print("Goodbye, ");
for (int i = 1; i < args.length; i++)
System.out.print(" " + args[i]);
System.out.println("!");
}
}
``````

``````java EightSample -g cruel world
``````

`args`数组将包含以下内容：

``````args[0]: "-g"
args[1]: "cruel"
args[2]: "world"
``````

数组排序

``````int[] a = new int[10000];
...
Arrays.sort(a)
``````

实战

``````下注以下组合，它会使你发财
8
30
32
43
46
49
``````

``````public class NinthSample {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

System.out.println("你要抽多少次？");
int k = in.nextInt();

System.out.println("你能抽取的最高数字是什么？");
int n = in.nextInt();

// 从数字1 2 3...n填入数组numbers
int[] numbers = new int[n];
for (int i = 0; i < numbers.length; i++)
numbers[i] = i + 1;

// 抽取k次并放入第二个数组中
int[] result = new int[k];
for (int i = 0; i < result.length; i++)
{
// 0到n-1之间创建一个随机索引
int r = (int) (Math.random() * n);

// 从随机的位置获取元素
result[i] = numbers[r];

// 将最后一个元素移动到随机位置
numbers[r] = numbers[n - 1];
n--;
}

Arrays.sort(result);
System.out.println("下注以下组合，它会使你发财");
for (int r: result)
System.out.println(r);
}
}
``````

要想选择一个随机的数字集合，就要首先将值1,2,...,n存入数组numbers中：

``````int[] numbers = new int[n];
for (int i=0; i < numbers.length; i++)
numbers[i] = i + 1;
``````

用第二个数组存放抽取出来的数：

``````int[] result = new int[k];
``````

现在，就可以开始抽取k个数了。`Math.random`方法返回一个0到1之间(包含0，不包含1)的随机浮点数。用n乘以浮点数，就可以得到从0到n-1之间的一个随机数。

``````int r = (int) (Math.random() * n);
``````

下面将result的第i个元素设置为`numbers[r]`存放的数值，最初是`r+1`。但正如所看到的，numbers数组的内容在每一次抽取之后都会发生变化。

``````result[i] = numbers[r];
``````

现在，必须确保不会再次抽取到那个数，因为所有抽彩的数必须不相同。因此，这里用数组中的最后一个数覆盖`number[r]`，并将n减1。

``````numbers[r] = numbers[n - 1];
n--;
``````

关键在于每次抽取的都是下标，而不是实际的值。下标指向数组中包含尚未抽取过的值。
在抽取了k个数之后，可以对result数组进行排序，这样可以让输出效果更好：

``````Arrays.sort(result);
for (int r: result)
System.out.println(r);
``````

Arrays API

``````static String toString(xxx[] a)

static xxx[] copyOf(xxx[] a, int end)
static xxx[] copyOfRange(xxx[] a, int start, int end)

static void sort(xxx[] a)

static int binarySearch(xxx[] a, xxx v)
static int binarySearch(xxx[] a, int start, int end, xxx v)

static void fill(xxx[] a, xxx v)

static boolean equals(xxx[] a, xxx[] b)

``````

多维数组

``````double[][] balances;
``````

``````balances = new double[NYEARS][NRATES];
``````

``````int[][] magicSquare =
{
{1, 2, 3, 4},
{5, 6, 7, 8}
};
``````

实战

``````public class ElevenSample {
public static void main(String[] args) {
// 初始利率
final double STARTRATE = 10;
// 列数
final int NRATES = 6;
// 行数
final int NYEARS = 10;

// 定义利息数组，数组长度为6
double[] interestRate = new double[NRATES];
for (int j = 0; j < interestRate.length; j++)
interestRate[j] = (STARTRATE + j) / 100.0;

// 定义余额数组，一维表示年，二维表示利率
double[][] balances=  new double[NYEARS][NRATES];
Arrays.fill(balances[0], 10000);

for (int i = 1; i < balances.length; i++) {
for (int j = 0; j < balances[i].length; j++) {
double oldBalance = balances[i - 1][j];
double interest = oldBalance * interestRate[j];
balances[i][j] = oldBalance + interest;
}
}

for (int j = 0; j < interestRate.length; j++)
System.out.printf("%9.0f%%", 100 * interestRate[j]);
System.out.println();

for (double[] row : balances) {
for (double b: row)
System.out.printf("%10.2f", b);
System.out.println();
}
}
}
``````