Java知识详细巩固_note2(数组_附demo code_其一为杨辉三角简析)

继 Java基础知识的全面巩固_note1(附各种demo code)
拜读《核心技术卷》,笔记之。

提纲

1.1 for each循环
1.2 数组初始化以及匿名数组
1.3 数组拷贝(以及Arrays.copyOf())
1.4 命令行参数
1.5 数组排序
1.6 多维数组
1.7 不规则数组

1.数组

声明:


以及:

1.1 for each循环
for(variable:collection) statement

例如:

for(int element : a)
    System.out.println(element);

打印数组a的每一个元素,一个元素占一行。
另外:

 System.out.println(Arrays.toString(a));
1.2 数组初始化以及匿名数组


!!注意使用这种语句时,不需要调用new。


匿名数组:



在Java中,允许数组长度为0。可用于方法的返回结果为空数组的情况:

1.3 数组拷贝

1)


如图所示显示了拷贝的结果:

2)
如果希望将一个数组的所有值拷贝到一个新的数组中去,就要使用Arrays类的copyOf方法:

3)

4)


下面上一个Demo:

package Test;
//import java.lang.Math;
import java.util.Arrays;
//import java.util.Date;
//import java.util.Scanner;


public class Havaatry {

    public static void main(String[] args)
       {
        int[] a = {3,2,5,6,9,8,4,0};
        int[] b = Arrays.copyOf(a, 16);
        int[] c = Arrays.copyOf(a, 3);
        boolean[] A = {true,true,true,true,true};
        boolean[] B = Arrays.copyOf(A, 16);
        boolean[] C = Arrays.copyOf(A, 3);
        System.out.println(Arrays.toString(a));
        System.out.println(Arrays.toString(b));
        System.out.println(Arrays.toString(c));
        System.out.println(Arrays.toString(A));
        System.out.println(Arrays.toString(B));
        System.out.println(Arrays.toString(C));
       }

}

运行结果:

[3, 2, 5, 6, 9, 8, 4, 0]
[3, 2, 5, 6, 9, 8, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[3, 2, 5]
[true, true, true, true, true]
[true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false]
[true, true, true]
1.4 命令行参数

1.5 数组排序


下面上一个Demo,它产生一个抽奖游戏中的随机数值组合。假如抽奖是从49个数值中抽取6个,那么程序可能的输出结果为:


现在,就可以开始抽取k个数值了。Math.random方法将返回一个[0,1) 之间的随机浮点数。用n乘以这个浮点数,就可以得到 [0,n) 之间的一个随机数,用(int) 强制转换类型,则是一个 [0,n-1] 的随机数。


Demo code:

package Test;
//import java.lang.Math;
import java.util.Arrays;
//import java.util.Date;
import java.util.Scanner;


public class Havaatry {

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

          System.out.print("How many numbers do you need to draw? ");
          int k = in.nextInt();

          System.out.print("What is the highest number you can draw? ");
          int n = in.nextInt();

          // fill an array with numbers 1 2 3 . . . n
          int[] numbers = new int[n];
          for (int i = 0; i < numbers.length; i++)
             numbers[i] = i + 1;

          // draw k numbers and put them into a second array
          int[] result = new int[k];
          for (int i = 0; i < result.length; i++)
          {
             // make a random index between 0 and n - 1
             int r = (int) (Math.random() * n);

             // pick the element at the random location
             result[i] = numbers[r];

             // move the last element into the random location
             numbers[r] = numbers[n - 1];
             n--;
          }

          // print the sorted array
          Arrays.sort(result);
          System.out.println("Bet the following combination. It'll make you rich!");
          for (int r : result)
             System.out.println(r);
       }

}

结果:

How many numbers do you need to draw? 5
What is the highest number you can draw? 12
Bet the following combination. It'll make you rich!
2
6
7
8
9

关键API:


1.6 多维数组





下面上完整代码:

package Test;
//import java.lang.Math;
import java.util.Arrays;
//import java.util.Date;
import java.util.Scanner;


public class Havaatry {

    public static void main(String[] args)
       {
         final double STARTRATE = 10;
          final int NRATES = 6;
          final int NYEARS = 10;

          // set interest rates to 10 . . . 15%
          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];

          // set initial balances to 10000
          for (int j = 0; j < balances[0].length; j++)
             balances[0][j] = 10000;

          // compute interest for future years
          for (int i = 1; i < balances.length; i++)
          {
             for (int j = 0; j < balances[i].length; j++)
             {
                // get last year's balances from previous row
                double oldBalance = balances[i - 1][j];

                // compute interest
                double interest = oldBalance * interestRate[j];

                // compute this year's balances
                balances[i][j] = oldBalance + interest;
             }
          }

          // print one row of interest rates
          for (int j = 0; j < interestRate.length; j++)
             System.out.printf("%9.0f%%", 100 * interestRate[j]);

          System.out.println();

          // print balance table
          for (double[] row : balances)
          {
             // print table row
             for (double b : row)
                System.out.printf("%10.2f", b);

             System.out.println();
          }
       }

}

运行结果:

另外:关于for each 以及 deepToString()

1.7 不规则数组

1)
Java实际上没有多维数组,只有一位数组:

例如,在前面的实例中,balances数组实际上是一个包含10个元素的数组,而每个元素又是一个由6个浮点数组成的数组。如图:


2)



3)由于可以单独地存取数组的某一行,所以可以让两行交换:


4)


也即i中取j的组合算法,这个三角形也即杨辉三角。

编程思路:

下面代码貌似有些长,但也只有三步:

1.  “动态”分配数组;
2.  计算各个数组各个元素的值;
3.  遍历输出;

代码:

package Test;
//import java.lang.Math;
import java.util.Arrays;
//import java.util.Date;
import java.util.Scanner;


public class Havaatry {

    public static void main(String[] args)
       {
         final int NMAX = 10;

          // allocate triangular array      “动态”分配三角阵列
          int[][] odds = new int[NMAX + 1][];
          for (int n = 0; n <= NMAX; n++)
             odds[n] = new int[n + 1];

          // fill triangular array
          for (int n = 0; n < odds.length; n++)//遍历各行
             for (int k = 0; k < odds[n].length; k++)//遍历第n行的各列
             {
                /*
                 * compute binomial coefficient n*(n-1)*(n-2)*...*(n-k+1)/(1*2*3*...*k)
                 */
                int lotteryOdds = 1;
                for (int i = 1; i <= k; i++)
                   lotteryOdds = lotteryOdds * (n - i + 1) / i;

                odds[n][k] = lotteryOdds;
             }

          // print triangular array
          for (int[] row : odds)
          {
             for (int odd : row)
                System.out.printf("%4d", odd);
             System.out.println();
          }
       }
}

运行结果:

   1
   1   1
   1   2   1
   1   3   3   1
   1   4   6   4   1
   1   5  10  10   5   1
   1   6  15  20  15   6   1
   1   7  21  35  35  21   7   1
   1   8  28  56  70  56  28   8   1
   1   9  36  84 126 126  84  36   9   1
   1  10  45 120 210 252 210 120  45  10   1

杨辉三角组合律:

另外:

你可能感兴趣的:(Java知识详细巩固_note2(数组_附demo code_其一为杨辉三角简析))