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

2018-08-27 00:27:00 135

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

提纲

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

1.数组

声明:

img_cbc577b34ddf41f5e882970119235ed0.png

以及:
img_19176039cb640e848a38e4c01527670a.png

1.1 for each循环
for(variable:collection) statement
img_eddfd6857b5dcb84ab5d87b4a960a39b.png

例如:

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

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

img_b4d8bbd66ff3716870b3e4065c974062.png

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

img_e48e3dc96f52d8281e8786a0d2d30aa6.png

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


匿名数组:

img_0c425d28e17d0ed1d953ad90b100d6cc.png

img_36bbed3e38f3b17d7537cee3d704ef23.png

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

1.3 数组拷贝

1)

img_4df03a7723e19e2cddf16849e7942902.png

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

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

img_32d0692f89a9109368a3f2537c88f3e5.png

3)
img_25bf033a04778b16ddad87a7507bd09b.png

4)

img_3e54315a618a1b0930c42cb17ee867d2.png

下面上一个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 命令行参数
img_aa7f0a015840099374cbc2a0cd28ec36.png
img_bc9ce09812278ef1a2ba78563035d6b8.png

img_11fbd42630445442c4ab8f1ec46eec3f.png
1.5 数组排序

img_2d4a56ed40baa7529c2f8823c8b69b69.png

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

img_498bb2e89edc668d1a8b5a5e47d4bf0c.png

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

img_e44d1d771d6538c74da4d3910a89eda4.png

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:

img_c7f0d779fa117f0fde0844f26fcf3242.png

img_dbe282014686b468bc29febd9f8066f3.png

1.6 多维数组
img_a1d9c1643f2c369089f7ad18fa79a345.png

img_eacdf0ce54f3bb3eb50e0142cccde80d.png

img_50dbb65e58e3bed7c3763fe13e6a10af.png

img_f575e857f9cb8f7e31d7b52a78535be9.png

img_95b1d0094048f172e5ab52d739fc6142.png

img_2c22c16410bf3159375bbe304f00221b.png

下面上完整代码:

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();
          }
       }

}

运行结果:

img_846fdac90c24b5502479249f3ecabfaa.png

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

img_6da83a4d3f272dfac42ad3f751357b53.png

1.7 不规则数组

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

img_d71d4985764557ca088aafb7e1349cb0.png

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

img_f8e4781eb5e4e431ffe9da63d4f1e64b.png


2)


img_cb2006923b980d1261c2700d50244356.png

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

img_c6f3c06c1c208b9c1393f0dbd8874ef0.png


4)

img_abe9659558b31dfcf204ab5bd85a40ca.png

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

编程思路:

img_b4ff63071ee99bb50663b235745f6db6.png

img_91522e9c90da3508a50f8779e712fb47.png

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

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

杨辉三角组合律:

img_db07b1a233d5eee9cc07c55a026cb7a7.png

另外:

img_3dc3e09fe862b3b3b32db6dee7dbba54.png

java random code string demo 数组

作者

李蔚蓬
TA的文章

相关文章