黑马程序员——数组

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

 

    直接上图:

 

由于图片太大,且宽度受限,只能拉长便于阅读。

 

例一:

class ArrayDemo {

	public static void main(String[] args) {
		// 元素类型[] 数组名 = new 元素类型[元素个数或数组长度];

		{// 局部代码块。限定局部变量的生命周期。
			int age = 3;
			System.out.println(age);
		}

		int[] arr = new int[3];

		// arr[0] = 89;
		System.out.println(arr[0]);
		for (int x = 0; x < 4; x++) {
		}

	}
}

/*
 * 内存的划分: 1,寄存器。 2,本地方法区。 3,方法区。 4,栈内存。 存储的都是局部变量。 而且变量所属的作用域一旦结束,该变量就自动释放。
 * 
 * 
 * 5,堆内存。 存储是数组和对象(其实数组就是对象) 凡是new建立在堆中。 特点: 1,每一个实体都有首地址值。
 * 2,堆内存中的每一个变量都有默认初始化值,根据类型的不同而不同。整数是0,小数0.0或者0.0f,boolean false char '\u0000'
 * 3,垃圾回收机制。
 */

 

例二:

class ArrayDemo2 {
	public static void main(String[] args) {
		int[] arr = new int[3];
		// System.out.println(arr[3]);//ArrayIndexOutOfBoundsException:
		// 当访问到数组中不存在的角标时,就会发生该异常。

		// arr = null;
		// System.out.println(arr[0]);//NullPointerException
		// 空指针异常:当引用型变量没有任何实体指向时,还在用其操作实体。就会发生该异常。

		// System.out.println(arr);// [I@c17164 对应是 数组 int型 @ 16进制地址 @左边是实体的类型
		// @右边是实体的哈希值

	}
}

 

例三:

class ArrayDemo3 {
	public static void main(String[] args) {
		// 格式1
		/*
		 * 需要一个容器,但是不明确容器的具体数据。
		 */
		// int[] arr = new int[3];

		/*
		 * 需要一个容器,存储已知的具体数据。
		 */
		// 元素类型[] 数组名 = new 元素类型[]{元素,元素,……};
		// int[] arr = new int[]{89,34,270,17};
		int[] arr = { 89, 34, 270, 17 };

		/*
		 * 对数组操作最基本的动作就是存和取。 核心思想:就是对角标的操作。
		 */

		System.out.println("length:" + arr.length);
		for (int x = 0; x < arr.length; x++) {
			System.out.println("arr[" + x + "] = " + arr[x] + ";");// arr[0] =
																	// 89;
		}

		for (int x = arr.length - 1; x >= 0; x--) {
			System.out.println("arr[" + x + "] = " + arr[x] + ";");// arr[0] =
																	// 89;
		}
		// System.out.println(arr[1]);
		// System.out.println(arr[2]);
		// System.out.println(arr[3]);

	}
}

 

例四:

class ArrayDemo4 {

	// 遍历数组的功能。
	public static void printArray(int[] arr) {
		System.out.print("[");
		for (int x = 0; x < arr.length; x++) {
			if (x != arr.length - 1)
				System.out.print(arr[x] + ", ");
			else
				System.out.println(arr[x] + "]");
		}
	}

	public static void main(String[] args) {
		int[] arr = { 34, 19, 11, 109, 3, 56 };

		// int max = getMax_2(arr);
		// System.out.println("max="+max);

		printArray(arr);

		// selectSort(arr);
		// bubbleSort(arr);
		// Arrays.sort(arr);
		selectSort_2(arr);

		printArray(arr);
	}

	public static void swap(int[] arr, int a, int b) {
		int temp = arr[a];
		arr[a] = arr[b];
		arr[b] = temp;
	}

	/*
	 * 冒泡排序。
	 */
	public static void bubbleSort(int[] arr) {
		for (int x = 0; x < arr.length - 1; x++) {
			for (int y = 0; y < arr.length - 1 - x; y++) {
				if (arr[y] > arr[y + 1]) {
					swap(arr, y, y + 1);
					/*
					 * int temp = arr[y]; arr[y] = arr[y+1]; arr[y+1] = temp;
					 */
				}
			}
		}
	}

	/*
	 * 选择排序。
	 */
	public static void selectSort(int[] arr) {
		for (int x = 0; x < arr.length - 1; x++) {
			for (int y = x + 1; y < arr.length; y++) {
				if (arr[x] > arr[y]) {
					swap(arr, x, y);
					/*
					 * int temp = arr[x]; arr[x] = arr[y]; arr[y] = temp;
					 */
				}
			}
		}
	}

	public static void selectSort_2(int[] arr) {
		for (int x = 0; x < arr.length - 1; x++) {
			int num = arr[x];
			int index = x;
			for (int y = x + 1; y < arr.length; y++) {
				if (num > arr[y]) {
					num = arr[y];
					index = y;
				}
			}
			if (index != x)
				swap(arr, x, index);
		}
	}

	/*
	 * 获取数组中的最大值。 思路: 1,需要进行比较。并定义变量记录住每次比较后较大的值。 2,对数组中的元素进行遍历取出,和变量中记录的元素进行比较。
	 * 如果遍历到的元素大于变量中记录的元素,就用变量记录住该大的值。 3,遍历结果,该变量记录就是最大值。
	 * 
	 * 定义一个功能来是实现。 明确一,结果。 是数组中的元素。int . 明确二,未知内容。 数组.
	 */
	public static int getMax(int[] arr) {
		// 定义变量记录较大的值。
		int maxElement = arr[0];// 初始化为数组中的任意一个元素。
		for (int x = 1; x < arr.length; x++) {
			if (arr[x] > maxElement)
				maxElement = arr[x];
		}
		return maxElement;
	}

	public static int getMax_2(int[] arr) {
		// 定义变量记录较大的值。
		int maxIndex = 0;// 初始化为数组中任意一个角标。
		for (int x = 1; x < arr.length; x++) {
			if (arr[x] > arr[maxIndex])
				maxIndex = x;
		}
		return arr[maxIndex];
	}
}


练习一:

/*
 给定一个数组,对其进行反转。

 {3,1,6,5,8,2} --> 
 {2,8,5,6,1,3};

 其实就是头尾元素的位置置换。

 */

class ArrayTest {
	public static void printArray(int[] arr) {
		System.out.print("[");
		for (int x = 0; x < arr.length; x++) {
			if (x != arr.length - 1)
				System.out.print(arr[x] + ", ");
			else
				System.out.println(arr[x] + "]");
		}
	}

	public static void main(String[] args) {
		int[] arr = { 4, 1, 8, 7, 3, 8, 2 };
		printArray(arr);
		reverseArray(arr);
		printArray(arr);
	}

	public static void reverseArray(int[] arr) {
		for (int start = 0, end = arr.length - 1; start < end; start++, end--) {
			swap(arr, start, end);
		}
	}

	public static void swap(int[] arr, int a, int b) {
		int temp = arr[a];
		arr[a] = arr[b];
		arr[b] = temp;
	}
}


练习二:

 

/*
 获取一个整数的16进制表现形式。
 */

class ArrayTest2 {
	public static void main(String[] args) {
		toHex_2(0);
	}

	// 0,1,2,3,4,5,6,7,8,9,A, B, C, D, E, F
	// 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

	/*
	 * 什么时候使用数组呢? 如果数据出现了对应关系,而且对应关系的一方是有序的数字编号。并作为角标使用。 这时就必须要想到数组的使用。
	 * 
	 * 就可以将这些数据存储到数组中。 根据运算的结果作为角标直接去查数组中对应的元素即可。
	 * 
	 * 这种方式:称为查表法。
	 */

	public static void toHex_2(int num) {

		if (num == 0) {
			System.out.println("0");
			return;
		}
		// 定义一个对应关系表。
		char[] chs = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
				'B', 'C', 'D', 'E', 'F' };
		/*
		 * 一会查表会查到比较多的数据。 数据一多,就先存储起来,在进行操作。 所以定义一个数组。 临时容器。
		 */
		char[] arr = new char[8];
		int pos = arr.length;

		while (num != 0) {
			int temp = num & 15;
			arr[--pos] = chs[temp];
			num = num >>> 4;
		}

		System.out.println("pos=" + pos);
		for (int x = pos; x < arr.length; x++) {
			System.out.print(arr[x]);
		}

	}

	public static void toHex_1(int num) {
		// 定义一个对应关系表。
		char[] chs = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
				'B', 'C', 'D', 'E', 'F' };

		for (int x = 0; x < 8; x++) {
			int temp = num & 15;
			System.out.print(chs[temp]);
			num = num >>> 4;
		}

	}

	public static void toHex(int num) {

		for (int x = 0; x < 8; x++) {
			int temp = num & 15;
			if (temp > 9)
				System.out.print((char) (temp - 10 + 'A'));
			else
				System.out.print(temp);
			num = num >>> 4;
		}
		/*
		 * int n1 = num & 15; System.out.println("n1="+n1);
		 * 
		 * num = num >>> 4; int n2 = num & 15; System.out.println("n2="+n2);
		 */
	}
}


练习三:

 

class ArrayTest3 {
	public static void main(String[] args) {
		// toHex(26);
		toBinary(-6);
		// toOctal(26);
		System.out.println(Integer.toBinaryString(-6));
	}

	// 十进制-->十六进制。
	public static void toHex(int num) {
		trans(num, 15, 4);
	}

	// 十进制-->二进制。
	public static void toBinary(int num) {
		trans(num, 1, 1);
	}

	// 十进制-->八进制。
	public static void toOctal(int num) {
		trans(num, 7, 3);
	}

	public static void trans(int num, int base, int offset) {

		if (num == 0) {
			System.out.println("0");
			return;
		}
		// 定义一个对应关系表。
		char[] chs = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
				'B', 'C', 'D', 'E', 'F' };
		/*
		 * 一会查表会查到比较的数据。 数据一多,就先存储起来,在进行操作。 所以定义一个数组。 临时容器。
		 */
		char[] arr = new char[32];
		int pos = arr.length;

		while (num != 0) {
			int temp = num & base;
			arr[--pos] = chs[temp];
			num = num >>> offset;
		}

		for (int x = pos; x < arr.length; x++) {
			System.out.print(arr[x]);
		}
		System.out.println();

	}

}


练习四:使用查表法输出对应的星期。

 

class ArrayTest4 {
	public static void main(String[] args) {
		String week = getWeek(71);
		System.out.println(week);
	}

	/*
	 * 使用查表法。 星期。 String s = "abc"; int x = 4;
	 */
	public static String getWeek(int num) {

		if (num > 7 || num < 1) {
			return "错误的星期";
		}
		String[] weeks = { "", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };

		return weeks[num];
	}

}


一道面试题(查找):

/*
 面试题:
 给定一个有序的数组,如果往该数组中存储一个元素,并保证这个数组还是有序的,
 那么个元素的存储的角标为如何获取。
 {13,15,19,28,33,45,78,106};
 */
class ArrayDemo5 {
	public static void main(String[] args) {
		//
		// int[] arr = {4,1,8,7,3,8,2};
		int[] arr = { 13, 15, 19, 28, 33, 45, 78, 106 };
		int index = halfSearch_2(arr, 5);
		System.out.println("index=" + index);

		int index1 = Arrays.binarySearch(arr, 5);// 如果存在返回的具体的角标位置,不存在返回的是
													// -插入点-1
		System.out.println("index1=" + index1);
	}

	/*
	 * 二分查找法。
	 */
	public static int halfSearch(int[] arr, int key) {
		int max, min, mid;
		min = 0;
		max = arr.length - 1;
		mid = (max + min) / 2;

		while (arr[mid] != key) {
			if (key > arr[mid])
				min = mid + 1;
			else if (key < arr[mid])
				max = mid - 1;

			if (max < min)
				return -1;

			mid = (max + min) / 2;
		}
		return mid;

	}

	public static int halfSearch_2(int[] arr, int key) {
		int max, min, mid;
		min = 0;
		max = arr.length - 1;

		while (min <= max) {
			mid = (max + min) >> 1;

			if (key > arr[mid])
				min = mid + 1;
			else if (key < arr[mid])
				max = mid - 1;
			else
				return mid;
		}
		return -min - 1;
	}

	/*
	 * 数组常见功能:查找。
	 */
	public static int getIndex(int[] arr, int key) {
		for (int x = 0; x < arr.length; x++) {
			if (arr[x] == key)
				return x;
		}
		return -1;
	}
}


二维数组是怎样定义的:

 

/*
 二维数组定义的格式。
 */

class Array2Demo {
	public static void main(String[] args) {

		// int[] arr = new int[3];
		// System.out.println(arr);//[I@1fb8ee3 @左边是实体的类型。 @右边是实体的哈希值。

		// int[][] arr = new int[3][2];//创建一个二维数组,该数组中有3个一维数组,每一个一维数组中有2个元素。
		// System.out.println(arr);//直接打印二维数组。 [[I@c17164
		// System.out.println(arr[0]);//直接打印二维数组中的角标0的一维数组。 [I@1fb8ee3
		// System.out.println(arr[0][0]);//直接打印二维数组中的角标0的一维数组中角标为0的元素。 0

		// int[][] arr = new int[3][];
		// System.out.println(arr);//直接打印二维数组。 [[I@c17164
		// System.out.println(arr[0]);//直接打印二维数组中的角标0的一维数组。null
		// System.out.println(arr[0][0]);//直接打印二维数组中的角标0的一维数组中角标为0的元素。
		// NullPointerException

		// int[][] arr = new int[3][2];
		// System.out.println(arr.length);//打印二维数组的长度。其实就是一维数组的个数。
		// System.out.println(arr[1].length);//打印二维数组中角标为1一维数组的长度。

		int sum = 0;
		int[][] arr = { { 3, 1, 7 }, { 5, 8, 2, 9 }, { 4, 1 } };

		for (int x = 0; x < arr.length; x++) {
			for (int y = 0; y < arr[x].length; y++) {
				// System.out.print(arr[x][y]+",");
				sum += arr[x][y];

			}
		}
		System.out.println("sum=" + sum);

		// 甲:30 59 28 17
		// 乙;37 60 22 19
		// int[] arr = {{30,59,28,17},{37,60,22,19}};

		int[][][] arr1 = new int[3][2][4];
	}
}

/*
 * int[] x,y[]; int[] x; int[] y[];
 * 
 * 
 * a x = y;
 * 
 * b x = y[0];
 * 
 * c x[0] = y[0];
 * 
 * d x[0] = y[0][0];
 * 
 * e x[0] = y;
 */


 

以上是关于数组的大纲和知识点。知识点牢记、代码多敲就ok了。

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
黑马程序员的tb_brand是指在JavaWeb基础教程中创建的一个表。这个表是用来存储品牌信息的,具体的表结构和数据类型需要和JavaBean类中的成员变量保持一致。\[1\]在这个教程中,使用了Maven来构建项目,并且使用了MyBatis作为持久层框架,通过配置pom.xml文件来引入相关依赖。\[2\] Maven是一个用于管理和构建Java项目的工具,它提供了一套标准化的项目结构、构建流程和依赖管理机制。\[3\] #### 引用[.reference_title] - *1* [【JAVAWEB开发】黑马程序员java web案例资料(含Element的删除与修改)](https://blog.csdn.net/aasd23/article/details/126940147)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [黑马程序员-MyBatis 框架-最全入门笔记、阿伟看了都得说真大、真细、真全!!!](https://blog.csdn.net/qq_57383364/article/details/128103058)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [JavaWeb——黑马程序员课程笔记](https://blog.csdn.net/King_ZACC/article/details/128573804)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值