做微信h5的网站,产品推广文案范文,查找网站后台的软件,垂直电商网站有哪些数组 概述数组定义实例展示实战演练 二维数组定义数组元素的使用数组初始化器实战演练#xff1a;矩阵计算 #x1f4ab;不规则二维数组实战演练#xff1a;杨辉三角形 概述
⚡️数组是相同数据类型的元素集合。各元素是有先后顺序的#xff0c;它们在内存中按照这个先后顺… 数组 概述数组定义实例展示实战演练 二维数组定义数组元素的使用数组初始化器实战演练矩阵计算 不规则二维数组实战演练杨辉三角形 概述
⚡️数组是相同数据类型的元素集合。各元素是有先后顺序的它们在内存中按照这个先后顺序连续存放在一起 ⚡️Java中数组元素可以为基本数据类型也可以为引用数据类型(对象)。 ⚡️数组可分为一维数组和多维数组。
数组定义
使用数组一般需要三个步骤 1声明数组声明数组名称和元素的数据类型。 2创建数组为数组元素分配存储空间。 3使用数组对数组元素操作。 1、声明数组一维数组 使用数组之前需要声明数组声明数组就是告诉编译器数组类型和数组名 【注】声明数组仅仅是声名一个数组对象的引用声明数组时不为数组元素分配内存因此 [ ]中不需给出元素的个数
例: double []marks;String []words;2、创建数组 创建数组是为数组的每个元素分配存储空间使用new语句格式如下 数组名 new 数据类型 [个数]; //动态分配内存给数组
例: marks new double[5];words new String[3];数组声明和创建可以写在一个语句中 数据类型 [ ]数组名 new 数据类型 [个数];
例: double []marks new double[5];String []words new String[3]; 当数组被成功创建后数组中元素会被自动赋予一个默认值 根据元素类型的不同默认初始化的值也是不一样的。
double []marks;
marks new double[5];marks[0] 79;marks[1] 84.5;marks[2] 63;marks[3] 90;marks[4] 98; 3、访问数组元素 定义了一个数组并用运算符new为它分配了内存空间后就可以引用数组中的每一个元素了。 数组元素的使用方式是 arrayName [index] 数组名 下标⚠️下标从0到arrayName.length-1 数组作为对象提供了一个length成员变量它表示数组元素个数访问该成员变量的方法为 arrayName.length
实例展示
package shujia_test1;public class Csdn5_1 {public static void main(String[] args) {double[] marks new double[5];marks[0] 79;marks[1] 84.5;marks[2] 63;marks[3] 90;marks[4] 98;System.out.println(marks[2]);System.out.println(marks.length);// 输出每个元素值for (int i 0; i marks.length; i) {System.out.print(marks[i] );}}}
运行结果 注Java运行时系统要对数组元素的范围进行越界检查 4、数组初始化器 在声明数组的同时利用初始化器对数组元素初始化它是在一对大括号中给出数组的每个元素值。
double[] marks new double[]{79, 84.5, 63,90, 98};
String[] words new String[]{Java, is, cool };简单形式数据类型 []数组名{初值0初值1…初值n} double[] marks {79, 84.5, 63, 90, 98};String[] words {Java, is, cool};重点注意 这种方法创建数组不能指定大小系统根据元素个数确定大小 int [5]arr {2,4,6,8,4};
//非法系统自动统计数据个数不可指定长度。 在Java中创建数组时通常需要指定数组的大小即数组中元素的数量。这是因为Java中的数组是静态的一旦创建其大小就不能改变。这意味着你必须在声明数组时就指定其能够存储的元素数量。
实战演练
编写程序要求用户从键盘输入5个整数并存放到一个数组中然后计算所有元素的和、最大值、最小值、平均值。
package shujia_test1;import java.util.Scanner;public class Csdn5_2 {public static void main(String[] args) {Scanner scanner new Scanner(System.in);// 创建一个数组来存储输入的整数int[] numbers new int[5];// 提示用户输入5个整数并将它们存储在数组中System.out.println(请输入5个整数);for (int i 0; i numbers.length; i) {numbers[i] scanner.nextInt();}// 初始化和变量来存储总和、最大值、最小值int sum 0;int max numbers[0];int min numbers[0];// 遍历数组计算总和、最大值、最小值for (int number : numbers) {sum number;if (number max) {max number;}if (number min) {min number;}}// 计算平均值double average (double) sum / numbers.length;// 输出结果System.out.println(总和为 sum);System.out.println(最大值为 max);System.out.println(最小值为 min);System.out.printf(平均值为%.2f%n, average);// 关闭scannerscanner.close();}}
运行结果
二维数组定义
1、二维数组声明 elementType[][] arrayName; 数据类型 [][] 数组名; 或 elementType arrayName[][]; elementType[] arrayName[]; 2、二维数组创建 arrayName new elementType[row][col]; arrayName new elementType[row][]; //适用不规则二维数组 例int [][]matrix new int[2][3];二维数组创建后每个元素被指定为默认值0
数组元素的使用
访问二维数组元素形式如下 arrayName[index1][index2] 数组名 第1维下标 第2维下标 用matrix.length得到matrix数组的大小 用matrix[0].length得到matrix[0]数组的大小
package shujia_test1;public class Csdn5_3 {public static void main(String[] args) {int array[][] new int[2][3];System.out.println(array.length);System.out.println(array[0].length);}
}
结果
例
int [][]matrix new int[2][3];
matrix[0][0] 80;
matrix[0][1] 75;
matrix[0][2] 78;
matrix[1][0] 67;
matrix[1][1] 87;
matrix[1][2] 98;
System.out.println(matrix[1][2]);
// 98
for ( int i0; i matrix.length; i ){for ( int j0; j matrix[0].length; j ){System.out.print(matrix[i][j] );}System.out.println();//换行
}
结果80 75 78 67 87 98 访问二维数组时下标也不能超出范围否则抛出异常。
数组初始化器
初始化器在声明数组的同时为数组元素初始化 int[][] matrix {{15,56,20,-2}, {10,80,-9,31}, {76,-3,99,21}}; 这种方法创建二维数组不能指定大小,系统根据元素个数确定大小 int[3][4] matrix {{15,56,20,-2}, {10,80,-9,31}, {76,-3,99,21}}; matrix.length值为3 matrix[1].length值为4
实战演练矩阵计算
编写程序计算AB、A-B和矩阵A的转置(矩阵的行和列互换)。
package shujia_test1;public class Csdn5_4 {public static void main(String[] args) {// 定义矩阵A和Bint[][] A { { 1, 3, 5 }, { -3, 6, 0 }, { 13, -5, 7 }, { -2, 19, 25 } };int[][] B { { 0, -1, -2 }, { 7, -1, 6 }, { -6, 13, 2 }, { 12, -8, -13} };// 计算ABint[][] APlusB addMatrices(A, B);printMatrix(A B , APlusB);// 计算A-Bint[][] AMinusB subtractMatrices(A, B);printMatrix(A - B , AMinusB);// 计算A的转置int[][] transposeA transposeMatrix(A);printMatrix(Transpose of A , transposeA);}// 计算两个矩阵的和public static int[][] addMatrices(int[][] A, int[][] B) {int rowsA A.length;int colsA A[0].length;int rowsB B.length;int colsB B[0].length;int rows Math.min(rowsA, rowsB);int cols Math.min(colsA, colsB);int[][] result new int[rows][cols];for (int i 0; i rows; i) {for (int j 0; j cols; j) {result[i][j] A[i][j] B[i][j];}}return result;}// 计算两个矩阵的差public static int[][] subtractMatrices(int[][] A, int[][] B) {int rows Math.min(A.length, B.length);int cols Math.min(A[0].length, B[0].length);int[][] result new int[rows][cols];for (int i 0; i rows; i) {for (int j 0; j cols; j) {result[i][j] A[i][j] - B[i][j];}}return result;}// 计算矩阵的转置public static int[][] transposeMatrix(int[][] A) {int rows A.length;int cols A[0].length;int[][] result new int[cols][rows];for (int i 0; i rows; i) {for (int j 0; j cols; j) {result[j][i] A[i][j];}}return result;}// 打印矩阵public static void printMatrix(String title, int[][] matrix) {System.out.println(title);for (int[] row : matrix) {for (int num : row) {System.out.print(num );}System.out.println();}}
}
运行结果
不规则二维数组
C语言中定义的数组必须是一个m*n的矩阵而Java语言的二维数组不一定是规则的矩形。 对二维数组声明时可以只指定第一维的大小第二维的每个元素可以指定不同的大小。
例:
String [][]cities new String[2][];
cities[0] new String[3];
cities[1] new String[2];
cities[0][0] new String(北京);
cities[0][1] new String(上海);
cities[0][2] new String(广州);
cities[1][0] new String(伦敦);
cities[1][1] new String(纽约);实战演练杨辉三角形
例: 编写程序输出杨辉三角形的前10行。
package shujia_test1;public class Csdn5_5 {public static void main(String[] args) {int i, j;int level 10;int triangle[][] new int[level][];for (i 0; i triangle.length; i)triangle[i] new int[i 1];// 为triangle数组的每个元素赋值triangle[0][0] 1;for (i 1; i triangle.length; i) {triangle[i][0] 1;for (j 1; j triangle[i].length - 1; j)triangle[i][j] triangle[i - 1][j - 1] triangle[i - 1][j];triangle[i][triangle[i].length - 1] 1;}// 打印输出triangle数组的每个元素for (i 0; i triangle.length; i) {for (j 0; j triangle[i].length; j)System.out.print(triangle[i][j] );System.out.println(); // 换行}}}
运行结果 讲解二维数组可以被看作是一个数组的数组也就是说它是一个数组的集合其中每个元素本身又是一个数组。这些内部数组通常具有相同的长度尽管在Java中这不是强制性的但在处理像杨辉三角形这样的结构时我们通常会保持每行长度递增的模式但它们各自独立分别代表二维数组中的一行。从逻辑上讲你可以将二维数组视为由多个一维数组行组成的集合每个一维数组都包含了一定数量的元素列。这种理解方式有助于我们编写代码来遍历和操作二维数组中的元素。在遍历二维数组时我们通常首先遍历外层数组即行然后对于每一行我们再遍历该行的内部数组即列。这就是在上面的杨辉三角形示例中所做的。 博主用心写读者点关注互动传真情知识不迷路