走进Java(七):数组

数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同。

Java 语言中提供的数组是用来存储固定大小的同类型元素。一旦定义好,不可以改变长度,在内存中占用大小是固定的

声明数组变量:

dataType[] arrayRefVar;   // 首选的方法
 
或
 
dataType arrayRefVar[];  // 效果相同,但不是首选方法

注意: 建议使用 dataType[] arrayRefVar 的声明风格声明数组变量。 dataType arrayRefVar[] 风格是来自 C/C++ 语言 ,在Java中采用是为了让 C/C++ 程序员能够快速理解java语言。

double[] myList;         // 首选的方法
 
或
 
double myList[];         //  效果相同,但不是首选方法

创建数组:

dataType[] arrayRefVar = new dataType[arraySize];
dataType[] arrayRefVar = {value0, value1, ..., valuek};

数组的常用操作:

  • 打印数组长度  .length
system.out.println(array.length);
  • 遍历数组:打印的是数组的每个下标
package com.xdl.java;

public class TestArrayFor {
	public static void main(String[] args) {
		//数组遍历
		int[] list={1,2,3,4,5};
		//遍历
		for (int i = 0; i < list.length; i++) {
			System.out.println(list[i]);
		}
	}
}

数组的内存原理:

  • 数组是一个引用数据类型,数组变量是一个引用变量
  • 变量通常保存到栈(stack)中,数组对象被存储在堆(heap)空间
package com.xdl.java;

public class TestSort {
	public static void main(String[] args) {
		// 数组排序
		// 冒泡排序
		// 定义个数组
		int[] score = { 88, 62, 12, 100, 28 };
		// 两层循环
		// 外层循环控制排序轮数
		// 内存循环控制元素两两比较的次数

		//时间复杂度是O(n^2)
		//没有循环的话,复杂度是O(n)
		//通常考虑最坏时间复杂度
		
		for (int i = 0; i < score.length - 1; i++) {
			// 每循環沉底一个元素,沉底元素不再参加比较
			for (int j = 0; j < score.length - 1 - i; j++) {
				// 比较相邻元素
				if (score[j] > score[j + 1]) {
					// 交互
					int tmp = score[j];
					score[j] = score[j + 1];
					score[j + 1] = tmp;
				}
			}
			System.out.println("第" + (i + 1) + "轮排序");
			for (int j = 0; j < score.length; j++) {
				System.out.print(score[j] + "\t");
			}
			System.out.println();
		}
		System.out.println("最终排序:");
		for (int i = 0; i < score.length; i++) {
			System.out.print(score[i] + "\t");
		}
		System.out.println();
	}
}
package com.xdl.java;

public class TestValue {
	public static void main(String[] args) {
		// 数组最大值和最小值
		// 定义一个数组
		int[] score = { 88, 62, 12, 100, 28 };
		// 定义最大值
		int max = 0;
		// 定义最小值
		int min = 0;
		max = min = score[0];
		for (int i = 1; i < score.length; i++) {
			if (score[i] > max) {
				max = score[i];
			}
			if (score[i] < min) {
				min = score[i];
			}
		}
		System.out.println("最大值:" + max);
		System.out.println("最小值:" + min);
	}
}

走进Java(七):数组

 二维数组:

  • 直接为每一维分配空间,格式如下:
type arrayName = new type[arraylenght1][arraylenght2];

type 可以为基本数据类型和复合数据类型,arraylenght1 和 arraylenght2 必须为正整数,arraylenght1 为行数,arraylenght2 为列数。

int a[][] = new int[2][3];

二维数组 a 可以看成一个两行三列的数组。

  • 初始化
package com.xdl.java;

public class TestArrTwo {
	public static void main(String[] args) {
		//定义个数组
		int[][] array;
		//初始化
		array = new int[][]{
				{1},
				{2,3},
				{4}
		};
	}
}
package com.xdl.java;

public class TestArrTwo2 {
	public static void main(String[] args) {
		// 定义3*3的二维数组
		int[][] array = new int[3][3];
		// 初始化
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array[i].length; j++) {
				// 取i行,j列
				array[i][j] = 3 * i + j + 1;
			}
		}
		// 打印二维数组
		//遍历二维数组
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array[i].length; j++) {
				System.out.print(array[i][j] + "\t");
			}
			System.out.println();
		}
	}
}
package com.xdl.java;

public class TestArrTwo3 {
	public static void main(String[] args) {
		// 定义和初始化一个数组
		int[][] array = { { 1, 2, 3, 4, 5 }, { 2, 3, 4, 5 }, { 3, 4, 5 },
				{ 4, 5 }, { 5 } };
		// 初始化
		// 定义5行,不确定的列
		int[][] x = new int[5][];
		x[0] = new int[5];
		x[1] = new int[4];
		x[2] = new int[3];
		x[3] = new int[2];
		x[4] = new int[1];

		// 数组赋值
		for (int i = 0; i < x.length; i++) {
			for (int j = 0; j < x[i].length; j++) {
				x[i][j] = array[i][j];
			}
		}

		// 打印数组x
		for (int i = 0; i < x.length; i++) {
			for (int j = 0; j < x[i].length; j++) {
				System.out.print(x[i][j] + "\t");
			}
			System.out.println();
		}

	}
}