黑马程序员---数组

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

一、概念

数组是具有相同数据类型的一组数据的集合。

二、一维数组的格式

数组作为队形允许使用new关键字进行内存分配。

声明方式:

数组元素类型 数组名字[]

数组元素类型[] 数组名字

初始化数组:

1.数组名字 = new 数组元素类型[数组元素的个数]

2.数组元素类型 数组名[] = new 数组元素类型[数组元素的个数]

3.数组元素类型 数组名[] = {数组元素1,数组元素2,.......}

数组也被称为引用数据类型。

注:数组在堆内存开辟空间后,就有默认的初始化值。如:int默认0boolean默认false

内存小知识:

系统的内存大概可以分为3个区域:系统区(OS),程序去(Program),数据区(Data)

        Java程序在运行时,需要在内存中的分配空间。为了提高运算效率,有对空间进行了不同区域的划分,因为每一片区域都有特定的处理数据方式和内存管理方式。

程序区:程序代码会存储到程序区内

栈内存:用于存储局部变量,当数据使用完,所占空间会自动释放。

堆内存:

1、数组和对象,通过new建立的实例都存放在堆内存中。

2、每一个实体都有内存地址值。

3、实体中的变量都有默认初始化值。

4、实体不在被使用,会在不确定的时间内被垃圾回收器回收。

、二维数组

声明方式:

数组元素类型 数组名字[][]

数组元素类型[][] 数组名字

初始化数组:

1.数组名字 = new 数组元素类型[数组元素的个数][数组元素的个数]

2.数组元素类型 数组名[] = new 数组元素类型[数组元素的个数][数组元素的个数]

3.数组元素类型 数组名[] = {{数组元素1,数组元素2},{数组元素3,数组元素4}.......}

遍历二维数组:

package array;
public class TwoArray {
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		int[][] a = {{2,3},{23,45},{64,24},{234,37}};
		for(int[] x:a)
			for (int i :x) 
			{
				System.out.print(i+",");
			}			
	}
}


、数组基本操作

静态方法:

Arrays.fill()填充替换数组元素

Arrays.sort(obj[])对数组进行排序(String类型为字典顺序)

Arrays.copyOf(arr,int newlength) ,Arrays.copyOfRange(arr,int formIndex,int endIndex) 复制数组

Arrays.binarySearch(Object[].Object key)数组查询

Arrays.equals(obj[],obj[])比较字符串是否相等

Arrays.hashCode(obj[])返回字符串的hashcode值

五、数组操作常见问题

1、数组脚标越界异常(ArrayIndexOutOfBoundsException)。例:

int[] arr = new int[2];
System.out.println(arr[3]);
访问到了数组中的不存在的脚标时发生。
2、空指针异常 (NullPointerException)。例:          
int[]arr = null;
System.out.println(arr[0]);
arr引用没有指向实体,却在操作实体中的元素时。

六、数组常见操作

1. 数组排序:

常见的排序方式:冒泡排序,选择排序和快速排序。

        冒泡程序:

package exam;
public class BubbleSort {
	//冒泡排序的方法实现
	public void sort(int[] array)
	{
		int i,j;
		int length = array.length;
		for(i = length - 1;i > 0; i--)
			for(j = 0 ; j < i ; j++)
			{
				if(array[j] >= array[j+1]) 
				{
					int temp = array[j];
					array[j] = array[j+1];
					array[j+1] = temp;
				}
			}
		for(i = 0;i < length ; i++)
		{
			System.out.print(array[i]+",");
		}
	}
	public static void main(String[] args)
	{
		int demoSort[] = {25,34,6,37,5,7};
		BubbleSort bubbleSort = new BubbleSort();
		bubbleSort.sort(demoSort);
	}
}
选择排序:

package exam;
public class SelectSort {
	//选择排序的方法实现
	public void sort(int[] array)
	{
		int i,j;
		int length = array.length;
		int min;
		for(i = 0;i<length; i++)
		{
				min = i;
				for(j=i+1;j<length;j++)
				{
					if(array[min]>array[j])
						min = j;
				}
				if(min != i)
				{
					int temp = array[min];
					array[min] = array[i];
					array[i] = temp;
				}
		}
		for(i = 0;i < length ; i++)
		{
			System.out.print(array[i]+",");
		}
	}
	public static void main(String[] args)
	{
		int demoSort[] = {25,34,6,37,5,7};
		SelectSort selectSort = new SelectSort();
		selectSort.sort(demoSort);
	}
}

快速排序:

package arrayExam;
public class quickSortDemo {
	private static int[] array1;
	public static void quickSort(int[] array,int low,int high)
	{
		int l=low;
		int h=high;
		int povit=array[low];
		while(l<h)
		{
			while(l<h&&array[h]>=povit)//执行完毕后,array[h]<povit<arrya[low]
				h--;
			if(l<h){
				int temp=array[h];
				array[h]=array[l];
				array[l]=temp;
				l++;
			}
			while(l<h&&array[l]<=povit)//执行完毕后,array[l]>povit>array[low]
				l++;
			if(l<h){
				int temp=array[h];
				array[h]=array[l];
				array[l]=temp;
				h--;
			}
		}
		if(l>low)quickSort(array,low,l-1);
		if(h<high)quickSort(array,l+1,high);
		array1 = array;
	}
	public static void show(int[] array)
	{
		for(int i = 0; i<array.length;i++)
		{
			System.out.print(array[i]+" ");
		}
	}
	public static void main(String[] args) {
		int[] array = {34,32,23,35,18,90,345,37,24};
		quickSort(array, 0, 8);
		show(array1);
	}
}

2.数组特定数据查找:

遍历查找:

package com.itheima;
public class Sereach {
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		int[] array = {3,4,6,12,34,5,29,48,32};
		int index = ergodicSereach(array,32);
		System.out.println(index);
	}
	public static int ergodicSereach(int array[],int key)
	{
		for(int i = 0;i<array.length;i++)
		{
			if(key==array[i])
				return i;
		}
		return -1;
	}
}
折半查找:
package array;
public class Bisearch {
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		int[] array = {1,3,5,7,9,11,14,15,16,17,20};
		int index = getIndex(array, 20);
		System.out.println(index);
	}
	public static int getIndex(int[] array,int num)
	{
		int low = 0;
		int high = array.length;
		while(low<=high)
		{
			int half = (low+high)/2;
			if(num>array[half])
			{
				low = half+1;
			}
			else if(num<array[half])
			{
				high = half-1;
			}
			else {
				return half;
			}
		}
		return -1;
	}
}
 

3.进制转换:

进制转换:
package array;
public class Conversion {
//查表法进行二进制,十六进制转换
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		int num = 25;
		System.out.println("num的二进制表现形式为:"+toBinary(num));
		System.out.println("num的十六进制表现形式为:"+toHex(num));
	}
	public static String toHex(int num)
	{
		StringBuilder sBuilder = new StringBuilder();
		char chs[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
		while(num>0)
		{
			sBuilder.append(chs[num&15]);
			num=num>>4;
		}
		return sBuilder.reverse().toString();
	}
	public static String toBinary(int num)
	{
		StringBuilder sBuilder = new StringBuilder();
		char chs[] = {'0','1'};
		while(num>0)
		{
			sBuilder.append(chs[num&1]);
			num=num>>1;
		}
		return sBuilder.reverse().toString();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值