【黑马程序员】 学习笔记 - Java数组及排序算法

本文介绍了Java中的数组概念,包括一维和二维数组的定义、内存分配方式以及遍历方法。此外,还详细讲解了两种基本排序算法——选择排序和冒泡排序的实现原理与步骤,并提供了具体的代码示例。
----------------------android培训java培训、期待与您交流!----------------------

JAVA数组及排序算法


一  数组


1.定义

定义:有联系的一些相同的数据类型的数据放在一起定义,这就是数组。
意义:我们可以很方便的来批量操作同一个数组内部的元素。
定义数组的方法:数据类型[] 数组名 = new 数据类型 [数组长度];
或者:数据类型 []数组名 = new 数据类型 {元素1,元素2,.....};

定义数组时的内存如图:


过程:JVM先在栈中申请一个空间,叫做array[6],之后分配一个内存地址,这个地址指向堆内存的一个地址0x2000,并开辟出一个24字节的内存空间。由于数据类型是int,每个元素占4个字节位置,所以array[0]就是0x2000~0x2003,array[1]是0x2004~0x2007,以此类推,到array[5]就是0x2010~0x2013(十六进制)。

2.遍历

用循环语句对数组的每个数据进行操作就叫遍历。

length:直接获取到数组的长度,如ch[10].length就是10。

遍历的时候容易出现那些异常?

a. Array Index Out Of Bounds Exception  角标越界异常:调用了无用的角标

b. Null Pointer Exception  空指针异常:调用了空数据的指针。

建立数组array后,用System.out.println(array);会输出[I@dec7d,会显示array的属性[是数组的意思,I是int型的意思,@dec7d表示哈希值为dec7d。而不是输出整个数组,输出整个数组的元素的方法是遍历。

3.二维数组

定义方法:int [][] arr = new int[3][4];或者int [][] arr = new int[3][];或者int[][]arr = {{3,2,1},{2,3,4,2}{1}};

二维数组相当于一个行列式,有行有列,很适合复杂的数据操作。

4.数组的应用 :排序

简单排序分为选择排序和冒泡排序,其他的排序方法还有快速排序,哈希值排序等。

选择排序:将第一个元素与后面每个元素都进行一次比较,并把两者之中小的交换给第一个元素,完成一次遍历之后,从第二个元素在循环一次,以此类推,直到排完整个序列。

冒泡排序:相邻的两个元素进行比较,如果前者大于后者,交换两者的值,这样一次循环就把最大的排到最后了。循环一次,排列的数组长度就少一位。这种方法叫做冒泡排序。

附:

选择排序法和冒泡排序法的程序及结果截图

public class Sort
{
	public static void main(String[] args)
	{
		int[] arr1 = {2,9,1,7,4,6,3,5};
		int[] arr2 = {2,9,1,7,4,6,3,5};
		int cnt = 0;
		//before sorting:
		System.out.println("Before sorting:");
		printArray(arr1);
		//sorting -- selectsort:
		System.out.println("Sorting--selectsort.....");
		cnt = selectSort(arr1);
		//after sorting--selectsort:
		System.out.println("After sorting--selectsort:");
		printArray(arr1);
		System.out.println("It's sorted "+cnt+" times.");
		//before sorting:
		System.out.println("Before sorting:");
		printArray(arr2);
		//sorting -- bubblesort:
		System.out.println("Sorting--bubblesort.....");
		cnt = selectSort(arr2);
		//after sorting--bubblesort:
		System.out.println("After sorting--bubblesort:");
		printArray(arr2);
		System.out.println("It's sorted "+cnt+" times.");
	}
	//it's used to display the array's element's value
	 public static void printArray(int[] arr)
	{
		System.out.print('[');
		for(int x = 0 ; x < arr.length-1 ; x++)
		{
			System.out.print(arr[x]+",");
		}
		System.out.println(arr[arr.length-1]+"]");
	}
	 //function: selecetsort 
	public static int selectSort(int[] arr)
	{
		int cnt = 0;
		//Traversing the array
		for(int x=0; x < arr.length-1; x++)
		{
			//the sorted element is the smallest in this turn,
			//we'll begin at the next element for next turn.
			for(int y=x+1; y < arr.length; y++,cnt++)
			{
				//we use the sorting element compare the later ones
				//if the sorting is bigger,we'll exchange their values
				if(arr[x] > arr[y])
				{
					int temp = arr[x];
					arr[x] = arr[y];
					arr[y] = temp;
				}
			}
		}
		return cnt;
	}
	//function:bubblesort
	public static int bubbleSort(int[] arr)
	{
		int cnt = 0;
		int x = 0;
		//Traversing the array
		for(; x < arr.length-1; x++);
		{
			//Once the array do,the length of the not sort is shorte.
			for(int y=0; y < arr.length- x -1; y++,cnt++)
			{
				//if the element is bigger than the next one,we exchange their value.
				//thus the biggest will goto the bottom
				if(arr[y] > arr[y+1])
				{
					int temp = arr[y];
					arr[y] = arr[y+1];
					arr[y+1] = temp;
				}
			}
		}
		//receive the times we loop;
		return cnt;
	}
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值