您的位置:首页 > 编程语言 > Java开发

【原】Java学习笔记012 - 数组

2017-02-26 11:46 357 查看
package cn.temptation;

public class Sample01 {
public static void main(String[] args) {
// 需求:小店对自己的销售额按季度进行统计,2016年各个季度统计如下:
//         第1季度:1月---10k    2月---12k    3月---9k
//         第2季度:4月---10k    5月---12k    6月---9k
//         第3季度:7月---10k    8月---12k    9月---9k
//         第4季度:10月---10k    11月---12k    12月---9k

// 考虑使用合适的数据类型来存储数据,因为每个季度中的每个月都记录下来的是其销售额,所以每个月记录的都是相同意义的数据
// 考虑使用数组存储
// 第1季度:int[] sale1 = { 10, 12, 9 };
// 第2季度:int[] sale2 = { 10, 12, 9 };
// 第3季度:int[] sale3 = { 10, 12, 9 };
// 第4季度:int[] sale4 = { 10, 12, 9 };

// 数组是一个容器,既然是容器,那么容器中存放元素的空间(即在堆内存开辟出来的空间)是否可以存储使用者指定类型的元素?
// 前面看到在空间中存储基本数据类型的数据是可以的,那么是否可以存储引用数据类型的数组呢?        答:可以的
// 类比:家里的冰箱是一个容器,冰箱里可以存放若干个小的容器冰格(大容器里可以放小容器)

// 二维数组的概念:元素为一维数组的(一维)数组

// 二维数组定义及初始化的形式1(动态初始化)
// 数据类型[][]   数组名      =  new    数据类型[m]
;
// m:表示该二维数组有多少个一维数组
// n:表示每一个一维数组有多少个元素

int[][] yearSale = new int[4][3];
// 显示出的  [[ 表示是二维数组,  类比 显示出 [ 表示是一维数组
System.out.println(yearSale);            // [[I@15db9742

System.out.println("------------------------------");

// yearSale[0]表示取得yearSale这个二维数组的第一个元素,也就是一个一维数组
System.out.println(yearSale[0]);        // [I@6d06d69c
System.out.println(yearSale[1]);        // [I@7852e922
System.out.println(yearSale[2]);        // [I@4e25154f
System.out.println(yearSale[3]);        // [I@70dea4e

System.out.println("------------------------------");

// 以yearSale[0]作为一个整体来看,yearSale[0][0]表示这个整体(一个一维数组)的第1个元素
System.out.println(yearSale[0][0]);        // 0
System.out.println(yearSale[0][1]);        // 0
System.out.println(yearSale[0][2]);        // 0
}
}


package cn.temptation;

public class Sample02 {
public static void main(String[] args) {
// 一维数组定义有两种形式
//        int[] arr1 = new int[3];    // 要求使用这种写法
//        int arr2[] = new int[3];

// 二维数组定义形式1(要求使用这种写法)
int[][] yearSale1 = new int[4][3];
// 二维数组定义形式2(语法可以,但是不要这样写)
int yearSale2[][] = new int[4][3];
// 二维数组定义形式3(等你面试别人的时候问别人)
int[] yearSale3[] = new int[4][3];

System.out.println(yearSale1);        // [[I@15db9742
System.out.println(yearSale2);        // [[I@6d06d69c
System.out.println(yearSale3);        // [[I@7852e922

// 问题:请说明下面变量的类型
int[] i, j[];
// 还是那句话"把没有见过的问题向见过的问题进行转换"
// 类比 int i, j;    等价于    int i;    int j;
// 所以上面的语句按此推理应该等价于        int[] i;    int[] j[];

// 推理之后还是应该验证一下
//        // The local variable i may not have been initialized
//        System.out.println(i);        // 使用eclipse自动判定一下i的类型,为一维数组
//        // The local variable j may not have been initialized
//        System.out.println(    j);        // 使用eclipse自动判定一下j的类型,为二维数组
}
}


package cn.temptation;

public class Sample03 {
public static void main(String[] args) {
// 需求:小店对自己的销售额按季度进行统计,2016年各个季度统计如下:(3月初开店,11月底倒闭)
//         第1季度:3月---9k
//         第2季度:4月---10k    5月---12k    6月---9k
//         第3季度:7月---10k    8月---12k    9月---9k
//         第4季度:10月---10k    11月---12k

// 单纯从存储的角度看,下面这个二维数组是可以存储的(语法上OK)
// 但是会在元素数据的语义上有歧义,到底是这个月没有开张或是倒闭,还是这个月的销售额就是0
//        int[][] yearSale = new int[4][3];
// 用上述语句进行数据存储时,不但需要知道二维数组中有多少个一级元素,而且还需要知道一级元素的最大长度是多少,否则会发生存储不下的问题

// 二维数组定义及初始化的形式2(动态初始化)
// 数据类型[][]   数组名    =  new  数据类型[m][];
// m:表示该二维数组有多少个一维数组

int[][] yearSale = new int[4][];
// 上述语句说明了三件事:
// 1、这个变量的类型是一个二维int数组
// 2、这个二维数组的元素是一维数组类型
// 3、该二维数组的一级元素有4个一维数组组成
System.out.println(yearSale);            // [[I@15db9742

System.out.println(yearSale[0]);        // null
System.out.println(yearSale[1]);        // null
System.out.println(yearSale[2]);        // null
System.out.println(yearSale[3]);        // null
// 引用数据类型的默认值为 null
// 上述语句中new int[4][]这句话,说明了在内存的堆中开辟空间做存储,只说明开辟出4个长度的空间,至于空间里放东西(存放数据)这个事情还没做
// 只知道向空间里放的东西(存放的数据)的类型是int类型的一维数组

// 使用二维数组定义及初始化的形式2(动态初始化)这种形式需要在二维数组定义及初始化后,还需要对二维数组的一级元素进行初始化
yearSale[0] = new int[1];
// 为什么非要放一个一维数组?可以放一个基本数据类型的数据不?            答:不行
// Type mismatch: cannot convert from int to int[]
//        yearSale[0] = 123;
// 侧面证明了 Java语言 是 强类型语言(使用时的随意性得到约束),语法上严格的类型要求可以避免使用上的错误产生
yearSale[1] = new int[3];
yearSale[2] = new int[3];
yearSale[3] = new int[2];

System.out.println(yearSale[0]);        // [I@6d06d69c
System.out.println(yearSale[1]);        // [I@7852e922
System.out.println(yearSale[2]);        // [I@4e25154f
System.out.println(yearSale[3]);        // [I@70dea4e

System.out.println(yearSale[3][0]);        // 0
System.out.println(yearSale[3][1]);        // 0
// 产生异常:java.lang.ArrayIndexOutOfBoundsException: 1
//        System.out.println(yearSale[0][1]);

// 如下写法可以不可以?
// 语法错误:Cannot specify an array dimension after an empty dimension        不能在一个空的维数后定义一个数组维数
// 这个语法错误可以理解为:第一个中括号中的数字表示的是这个二维数组有多少个一维数组
//        第一个中括号中没有数字也就是不知道有多少个一维数组(一级元素),在不知道的情况下就要去给每个一级元素开辟3个长度的空间,如何开辟?编译器做不到
//        int[][] arr = new int[][3];
}
}


package cn.temptation;

public class Sample04 {
public static void main(String[] args) {
// 二维数组定义及初始化的形式3(静态初始化)

// 简写形式
// 数据类型[][]   数组名     =  {
//                                { 二维数组的元素1的元素1, 二维数组的元素1的元素2, ..., 二维数组的元素1的元素n },
//                                { 二维数组的元素2的元素1, 二维数组的元素2的元素2, ..., 二维数组的元素2的元素n },...
//                                { 二维数组的元素m的元素1, 二维数组的元素m的元素2, ..., 二维数组的元素m的元素n },
//                            };
// m:表示该二维数组有多少个一维数组
// n:表示每个一维数组有多少个元素
// 注意:作为这种形式的二维数组的元素(一维数组)的长度可以一致,也可以不一致

int[][] yearSale = { { 10, 12, 9 }, { 10, 12, 9 }, { 10, 12, 9 }, { 10, 12, 9 }};
System.out.println(yearSale);        // [[I@15db9742

int[][] scoreArr = { { 50 }, { 10, 20, 30 }, { 60, 70 } };
System.out.println(scoreArr);        // [[I@6d06d69c

// 完整形式
// 数据类型[][]   数组名     =  new 数据类型[][] {
//                                { 二维数组的元素1的元素1, 二维数组的元素1的元素2, ..., 二维数组的元素1的元素n },
//                                { 二维数组的元素2的元素1, 二维数组的元素2的元素2, ..., 二维数组的元素2的元素n },...
//                                { 二维数组的元素m的元素1, 二维数组的元素m的元素2, ..., 二维数组的元素m的元素n },
//                            };
// m:表示该二维数组有多少个一维数组
// n:表示每个一维数组有多少个元素
// 注意:作为这种形式的二维数组的元素(一维数组)的长度可以一致,也可以不一致

int[][] yearSaleEx = new int[][] { { 10, 12, 9 }, { 10, 12, 9 }, { 10, 12, 9 }, { 10, 12, 9 }};
System.out.println(yearSaleEx);        // [[I@7852e922

int[][] scoreArrEx = { { 50 }, { 10, 20, 30 }, { 60, 70 } };
System.out.println(scoreArrEx);        // [[I@4e25154f

System.out.println(scoreArr[0]);    // [I@70dea4e
System.out.println(scoreArr[1]);    // [I@5c647e05
System.out.println(scoreArr[2]);    // [I@33909752

System.out.println(scoreArr[0][0]);        // 50
}
}


package cn.temptation;

public class Sample05 {
public static void main(String[] args) {
// 二维数组的遍历
int[][] yearSale = { { 10, 20, 30 }, { 40, 50, 60 }, { 70, 80, 90 }, { 77, 88, 99 } };

// 类比一维数组的遍历
// 这里二维数组的length属性表示二维数组包含的元素(一维数组)的个数
System.out.println(yearSale.length);        // 4

// 最简单的思路
//        for (int i = 0; i < yearSale[0].length; i++) {
//            System.out.println(yearSale[0][i]);
//        }
//        for (int i = 0; i < yearSale[1].length; i++) {
//            System.out.println(yearSale[1][i]);
//        }
//        for (int i = 0; i < yearSale[2].length; i++) {
//            System.out.println(yearSale[2][i]);
//        }
//        for (int i = 0; i < yearSale[3].length; i++) {
//            System.out.println(yearSale[3][i]);
//        }

// 因为又发现重复的执行,考虑使用循环
//        for (int i = 0; i < yearSale.length; i++) {
//            for (int j = 0; j < yearSale[i].length; j++) {
//                System.out.println(yearSale[i][j]);
//            }
//        }

// 调用方法
printArray(yearSale);
}

// 需求:制作一个二维数组的打印方法printArray,显示例如:[[10, 20, 30], [40, 50, 60], [70, 80, 90], [77, 88, 99]]
// 写法1、在一个方法中打印显示
/**
* 二维数组打印方法
* @param yearSale:二维数组
*/
//    public static void printArray(int[][] arr) {
//        System.out.print("[");
//
//        // 遍历二维数组
//        for (int i = 0; i < arr.length; i++) {
//            if (i == arr.length - 1) {        // 二维数组的最后一个元素 加上"]"
//                // 遍历一维数组
//                System.out.print("[");
//
//                for (int j = 0; j < arr[i].length; j++) {
//                    if (j == arr[i].length - 1) {        // 一维数组的最后一个元素 加上"]"
//                        System.out.print(arr[i][j] + "]");
//                    } else {                // 一维数组的其他元素 加上","
//                        System.out.print(arr[i][j] + ",");
//                    }
//                }
//
//                // 二维数组的最后一个元素 加上"]"
//                System.out.print("]");
//            } else {                // 二维数组的其他元素 加上","
//                // 遍历一维数组
//                System.out.print("[");
//
//                for (int j = 0; j < arr[i].length; j++) {
//                    if (j == arr[i].length - 1) {        // 一维数组的最后一个元素 加上"]"
//                        System.out.print(arr[i][j] + "]");
//                    } else {                // 一维数组的其他元素 加上","
//                        System.out.print(arr[i][j] + ",");
//                    }
//                }
//
//                // 二维数组的其他元素 加上","
//                System.out.print(",");
//            }
//        }
//
//        // 换行
//        System.out.println();
//    }

// 写法2、不同的事情在不同的方法中做
/**
* 二维数组的打印方法
* @param arr:二维数组
*/
public static void printArray(int[][] arr) {
System.out.print("[");

// 遍历二维数组
for (int i = 0; i < arr.length; i++) {
// 1、调用一维数组的打印方法
printArray(arr[i]);

// 2、打印完一维数组后,添加不同的后续内容
if (i == arr.length - 1) {    // 二维数组的最后一个元素 加上"]"
System.out.print("]");
} else {                    // 二维数组的最后一个元素 加上","
System.out.print(",");
}
}

// 换行
System.out.println();
}

/**
* 一维数组的打印方法
* @param arr:一维数组
*/
public static void printArray(int[] arr) {
System.out.print("[");

// 遍历数组
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {    // 一维数组的最后一个元素 加上"]"
System.out.print(arr[i] + "]");
} else {                    // 一维数组的最后一个元素 加上","
System.out.print(arr[i] + ",");
}
}
}
}


package cn.temptation;

public class Sample06 {
public static void main(String[] args) {
// 需求:小店对自己的销售额按季度进行统计,2016年各个季度统计如下:(3月初开店,11月底倒闭)
//         第1季度:3月---4k
//         第2季度:4月---10k    5月---12k    6月---9k
//         第3季度:7月---10k    8月---19k    9月---9k
//         第4季度:10月---10k    11月---12k
// 使用二维数组计算2016年该店的销售总和、月最高销售额、月最低销售额

//        int[][] saleArr = { { 4 }, { 10, 12, 9 }, { 10, 19, 9 }, { 10, 12 } };

// 下句语法正确,再次理解"二维数组就是元素为一维数组的一维数组"
int[][] saleArr = {};
//        System.out.println(saleArr);        // [[I@15db9742
//        System.out.println(saleArr.length);        // 0

System.out.println("2016年该店的销售总和为:" + yearTotal(saleArr) + "k元");

System.out.println("2016年该店的月最高销售额为:" + monthMaxSale(saleArr) + "k元");

System.out.println("2016年该店的月最低销售额为:" + monthMinSale(saleArr) + "k元");
}

/**
* 计算年销售总和
* @param arr:二维数组
* @return:年销售总和
*/
public static int yearTotal(int[][] arr) {
int total = 0;

for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
total += arr[i][j];
}
}

return total;
}

/**
* 统计月最高销售额
* @param arr:二维数组
* @return:月最高销售额
*/
public static int monthMaxSale(int[][] arr) {
int max = 0;

for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
max = (max > arr[i][j]) ? max : arr[i][j];
}
}

return max;
}

/**
* 统计月最低销售额
* @param arr:二维数组
* @return:月最低销售额
*/
public static int monthMinSale(int[][] arr) {
// 如下写法逻辑错误,如果没有比0小的销售额,最小值就一直为0
//        int min = 0;

// 如下写法对于没有销售额的二维数组来说,执行时会产生异常
// 产生异常:java.lang.ArrayIndexOutOfBoundsException: 0
// 考虑设置二维数组的第一个二级元素为比较的标杆
//        int min = arr[0][0];

// 如果没有销售额时,如何处理?
int min = (arr.length == 0) ? 0 : arr[0][0];

for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
min = (min < arr[i][j]) ? min : arr[i][j];
}
}

return min;
}
}


package cn.temptation;

import java.util.Scanner;

public class Sample07 {
public static void main(String[] args) {
// 需求:编写程序,输出如下图形(杨辉三角)
// 1
// 1    1
// 1    2    1
// 1    3    3    1
// 1    4    6    4    1
// 1    5    10    10    5    1

// 思路:(找规律)
// 1、第n行有n个数(n从1开始)
// 2、第1行和第2行均为1,从第3行开始,每行的首尾均为1
// 3、从第3行的第2列开始到该行的倒数第二个数结束,该位置上的数等于上一行同列的数  + 上一行前一列的数
//       从第4行的第2列开始到该行的倒数第二个数结束,该位置上的数等于上一行同列的数  + 上一行前一列的数
//         ......以此类推

System.out.println("键盘录入杨辉三角的行数:");
Scanner input = new Scanner(System.in);
int row = input.nextInt();
input.close();

// 考虑存储杨辉三角中的数进行计算,都是数字(具有相同意义),考虑使用数组;
// 因为涉及到行列,考虑使用二维数组;
// 因为行中的列数不同,所以声明及初始化数组时使用只有一级元素长度没有二级元素长度的定义方式
int[][] arr = new int[row][];

// 根据规律1
//        arr[0] = new int[1];
//        arr[1] = new int[2];
//        arr[2] = new int[3];
//        ...
//        arr
= new int[n+1];

for (int i = 0; i < row; i++) {
// 二维数组中的每个一级元素依据规律1创建相应长度的一维数组
arr[i] = new int[i + 1];

// 依据规律2:第1行和第2行均为1,从第3行开始,每行的首尾均为1
arr[i][0] = 1;
arr[i][i] = 1;
}

// 依据规律3:从第3行的第2列开始到该行的倒数第二个数结束,该位置上的数等于上一行同列的数  + 上一行前一列的数
//                从第4行的第2列开始到该行的倒数第二个数结束,该位置上的数等于上一行同列的数  + 上一行前一列的数
//         ......以此类推
for (int i = 2; i < arr.length; i++) {            // 外层循环的i表示行
for (int j = 1; j < arr[i].length - 1; j++) {        // 内层循环的j表示列
arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
}
}

// 调用方法打印数组
printArray(arr);
}

/**
* 打印数组
* @param arr:二维数组
*/
public static void printArray(int[][] arr) {
// 遍历数组
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + "\t");
}

// 换行
System.out.println();
}
}
}


package cn.temptation;

import java.util.Scanner;

public class Sample08 {
public static void main(String[] args) {
// 需求:制作一个成绩查询系统
// 提供的数据有:
// 1、给定学生的学号(1001, 1002, 1003)
// 2、给定学生的三门功课的成绩(语文、数学、英语)
// 要求:
// 1、输入某个同学的学号,显示出该同学的三门功课的成绩
// 2、输入某一门功课的编号(0---语文,1---数学,2---英语),显示该门功课成绩不及格的同学的学号,以及有多少个同学不及格

// 关键点:学生的学号 和 其各门功课成绩如何对应起来?    考虑让学号数组的索引 和 成绩数组的一级元素的索引同步
int[] studentArr = { 1001, 1002, 1003 };
int[][] scoreArr = { { 10, 20, 30 }, { 40, 50, 60 }, { 70, 80, 90 } };
// 课程数组的索引 和 成绩数组的二级元素的索引同步
String[] courseArr = { "语文", "数学", "英语" };

// 如下写法把学号和成绩糅合在一起存储也可以,但是不建议这样存储,因为我们说过数组是存储同一类型数据的容器
// 这里说的同一类型不仅指的是同一种数据类型,也指的是有相同的语义类型的数据
//        int[][] arr = { { 1001, 10, 20, 30 }, { 1002, 40, 50, 60 }, { 1003, 70, 80, 90 } };

Scanner input = new Scanner(System.in);
System.out.println("输入学生的学号:");
int number = input.nextInt();
// 调用方法
getScoreByNumber(courseArr, studentArr, scoreArr, number);

// 换行
System.out.println();

System.out.println("输入某一门功课的编号(0---语文,1---数学,2---英语)");
int course = input.nextInt();
// 调用方法
getNGInfo(courseArr, studentArr, scoreArr, course);

input.close();
}

/**
* 根据学号获取该生的各门功课成绩
* @param courseArr:    课程名称数组
* @param studentArr:    学生学号数组
* @param scoreArr:    学生成绩数组
* @param number:        查询时录入的学生学号
*/
public static void getScoreByNumber(String[] courseArr, int[] studentArr, int[][] scoreArr, int number) {
for (int i = 0; i < studentArr.length; i++) {
if (number == studentArr[i]) {        // 根据录入的学生学号在数组中找到对应的该学生的学号
// 因为学号数组的索引 和 成绩数组的一级元素的索引同步,所以在 成绩数组的一级元素对应的一维数组中查找该生的各门功课的成绩
for (int j = 0; j < scoreArr[i].length; j++) {
if (j == scoreArr[i].length - 1) {
System.out.print(courseArr[j] + ":" + scoreArr[i][j]);
} else {
System.out.print(courseArr[j] + ":" + scoreArr[i][j] + ",");
}
}
}
}
}

/**
* 显示该门功课成绩不及格的同学的学号,以及有多少个同学不及格
* @param courseArr:    课程名称数组
* @param studentArr:    学生学号数组
* @param scoreArr:    学生成绩数组
* @param course:        录入的课程名称索引
*/
public static void getNGInfo(String[] courseArr, int[] studentArr, int[][] scoreArr, int course) {
int total = 0;

System.out.println("【" + courseArr[course] + "】未及格学生的学号列表:");
for (int i = 0; i < studentArr.length; i++) {
if (scoreArr[i][course] < 60) {
total++;
System.out.print(studentArr[i] + "\t");
}
}

// 换行
System.out.println();

System.out.println("【" + courseArr[course] + "】未及格学生的人数为:" + total);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: