数组排序
若要对数字数组进行排序,可以使用Arraysclass中的一种排序方法:
int[] a = new int[10000];
. . .
Arrays.sort(a);
这种方法使用了快速排序算法的优化版本,这种算法在大多数数据集上都非常有效。
清单3.7中的程序为彩票游戏抽取数字的随机组合。 例如,如果您玩“从49中选择6个数字”彩票,程序可能会打印以下内容:
Bet the following combination. It'll make you rich!
4 7 8 19 30 44
为了选择这样一组随机的数字,我们首先用值1、2、……、n填充一个数字数组.
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(不含)之间的随机浮点数。 通过将结果乘以n,我们得到0到n–1之间的随机数。
int r = (int) (Math.random() * n);
我们将第i个结果设置为该索引处的数字。 最初只是r + 1,但正如您现在所看到的,每次抽奖后,数字数组的内容都会更改:
result[i] = numbers[r];
现在,我们必须确保永远不要再抽该号码-所有彩票号码必须是唯一的。 因此,我们用数组中的最后一个数字覆盖数字[r],并将n减少1。
numbers[r] = numbers[n - 1];
n--;
关键是在每次抽奖中,我们选择一个索引,而不是实际值。 索引指向一个数组,该数组包含尚未绘制的值。抽取了k个彩票数字后,我们对结果数组进行排序以获得更令人愉悦的输出:
Arrays.sort(result);
for (int r : result)
System.out.println(r);
完整版程序:
1 import java.util.*;
2 public class LotteryDrawing
3 {
4 public static void main(String[] args)
5 {
6 Scanner in = new Scanner(System.in);
7
8 System.out.print("How many numbers do you need to draw? ");
9 int k = in.nextInt();
10
11 System.out.print("What is the highest number you can draw? ");
12 int n = in.nextInt();
13
14 // fill an array with numbers 1 2 3 . . . n
15 int[] numbers = new int[n];
16 for (int i = 0; i < numbers.length; i++)
17 numbers[i] = i + 1;
18
19 // draw k numbers and put them into a second array
20 int[] result = new int[k];
21 for (int i = 0; i < result.length; i++)
22 {
23 // make a random index between 0 and n - 1
24 int r = (int) (Math.random() * n);
25 // pick the element at the random location
26 result[i] = numbers[r];
27
28 // move the last element into the random location
29 numbers[r] = numbers[n - 1];
30 n--;
31 }
32
33 // print the sorted array
34 Arrays.sort(result);
35 System.out.println("Bet the following combination. It'll make you rich!");
36 for (int r : result)
37 System.out.println(r);
38 }
39 }
二维数组
让我们看几个定义java二维数组或二维数组的例子。
1. Java二维数组原始类型
int[][] arr = new int[2][3];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = j;
System.out.print(arr[i][j] + " ");
}
System.out.println("");
}
2. Java二维对象数组
String[][] arrStr = new String[3][4];
for (int i = 0; i < arrStr.length; i++) {
for (int j = 0; j < arrStr[i].length; j++) {
arrStr[i][j] = "Str" + j;
System.out.print(arrStr[i][j] + " ");
}
System.out.println("");
}
因此,可以定义原始数据类型以及对象的二维数组。如果看一下上面的例子,二维数组就像一个矩阵,类似于下图。
但是,在Java中没有二维数组的概念。java中的二维数组只是一个数组的组。所以下面的图示确定义了java中的二维数组结构。
Java多维数组示例
如果java中的二维数组是一个数组数组,那么它也应该支持非对称大小,如下图所示。
它在java中绝对没问题。下面是一个描述上述多维数组的示例程序。
public class MultidimensionalArrayExample {
public static void main(String[] args) {
//使用快捷语法创建和初始化二维数组
int[][] arrInt = { { 1, 2 }, { 3, 4, 5 } };
for (int i = 0; i < arrInt.length; i++) {
for (int j = 0; j < arrInt[i].length; j++) {
System.out.print(arrInt[i][j] + " ");
}
System.out.println("");
}
// 多维数组初始化
int[][] arrMulti = new int[2][]; //这是有效的写法
arrMulti[0] = new int[2];
arrMulti[1] = new int[3];
arrMulti[0][0] = 1;
arrMulti[0][1] = 2;
arrMulti[1][0] = 3;
arrMulti[1][1] = 4;
arrMulti[1][2] = 5;
for (int i = 0; i < arrInt.length; i++) {
for (int j = 0; j < arrInt[i].length; j++) {
System.out.print(arrInt[i][j] + " ");
}
System.out.println("");
}
}
}
执行上面示例代码,得到以下结果-
1 2
3 4 5
1 2
3 4 5
参考链接:https://www.yiibai.com/java/two-dimensional-array-java.html