数组 获取最值(最大值 最小值)选择排序 冒泡排序 快速排序 半查找(二分查找) 十进制转其他进制法

数组

 

元素类型[] 数组名 = new 元素类型[元素个数或数组长度];

int [] arr = new int [4];

//需要一个容器,但是不明确容器的具体数据。

int [] arr = new int [] {3,1,4,5,}

//需要一个容器,存储已知的具体数据。

int [] arr = {12,4,4,3,3}

 

对数组操作最基本的动作就是存和取。

核心思想:就是对角标的操作。

 

length 长度判断

arr.length   arr数组的长度 

 

数组的遍历

 

class ArrayDemo2
{
	public static void main (String [] args)
	{
		int [] arr = {13,4,1,4,1};
			for (int x=0;x<arr.length ; x++)
			{
		    	System.out.println("arr["+x+"]="+arr[x]);
			}
	}
}

 

 

获取最值(最大值 最小值)

 

/*
	获取数组中的最大值。
	思路:
	1,需要进行比较。并定义变量记录住每次比较后较大的值。
	2,对数组中的元素进行遍历取出,和变量中记录的元素进行比较。
		如果遍历到的元素大于变量中记录的元素,就用变量记录住该大的值。 
	3,遍历结果,该变量记录就是最大值。

	定义一个功能来是实现。
	明确一,结果。
			是数组中的元素。int .
	明确二,未知内容。
			数组.
*/
class ArrayDemo2
{
	public static void main (String [] args)
	{
		int [] arr = {12,3213,4,5,64,667,6};
		int max = getMax (arr);
			System.out.println("max="+max);
	}
	public static int getMax (int [] arr)
	{
		int max =arr[0];
		for (int x=1;x<arr.length ;x++ )
		{
			if(arr[x]>max)
				max = arr[x];
		}
		return max;
	}
}

 

通过脚标求最大值

 

class ArrayDemo2
{
	public static void main (String [] args)
	{
		int [] arr = {12,3213,4,5,64,667,6};
		int max = getMax (arr);
			System.out.println("max="+max);
	}
	public static int getMax (int [] arr)
	{
		int maxIndex =0;
		for (int x=1;x<arr.length ;x++ )
		{
			if(arr[x]>arr[maxIndex])
				maxIndex = x;
		}
		return arr[maxIndex];
	}
}


排序(选择排序 冒泡排序 高效)

Arrays.sort(arr) 

选择排序 

 

//排序法 选择
public class Test1 {

	public static void main(String[] args) {

		int[] arr = { 3, 4, 1, 23, 4, 11, 42, 5, };
		arrSort(arr);
	}

	public static void arrSort(int[] arr) {
		for (int x = 0; x < arr.length - 1; x++) {
			for (int y = x + 1; y < arr.length - 1; y++) {
				if (arr[x] > arr[y])
					swap(arr, x, y);
			}
			System.out.print(arr[x] + " ");
		}
		System.out.println();
	}

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

 

冒泡排序

 

//排序法 冒泡
public class Test1 {

	public static void main(String[] args) {

		int[] arr = { 3, 4, 1, 23, 4, 11, 42, 5, };
		arrPoob(arr);
	}

	public static void arrPoob(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);
			}
			System.out.print(arr[arr.length - 1 - x] + " ");
		}
		System.out.println();
	}

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

高效方法

 

 

//排序法 高效
public class Test1 {

	public static void main(String[] args) {

		int[] arr = { 3, 4, 1, 23, 4, 11, 42, 5, };
		arrhh(arr);
	}

	public static void arrhh(int[] arr) {
		int index = 0;
		for (int x = 0; x < arr.length - 1; x++) {
			index = 0;
			for (int y = 0; y < arr.length - x; y++) {
				if (arr[index] < arr[y])
					index = y;
			}
			swap(arr, index, arr.length - 1 - x);
		}
		for (int z = 0; z < arr.length; z++) {
			System.out.print(arr[z] + " ");
		}
	}

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

 

折半查找 (二分查找)

查找法

 

//二分筛选法
//强调,一定是有序数列
//1,判断while(min <max),返回min;
//2,判断while(arr[mid]!=key),返回mid;
public class Test1 {

	public static void main(String[] args) {
		int[] arr = { 1, 3, 5, 6, 8, 9 };
		System.out.println(halfSearch(arr, 4));
		int[] arr2 = { 11, 13, 15, 16, 18, 19 };
		System.out.println(halfSearch2(arr2, 14));
	}

	public static int halfSearch(int[] arr, int key) {
		int max = arr.length - 1;
		int min = 0;
		int mid = (min + max) / 2;
		while (min < max) {
			if (key > arr[mid])
				min = mid + 1;
			else if (key < arr[mid])
				max = mid - 1;
			else
				return mid;
			mid = (min + max) / 2;
		}
		return min;
	}

	public static int halfSearch2(int[] arr, int key) {
		int min, mid, max;
		min = 0;
		max = arr.length - 1;
		mid = (min + max) / 2;
		while (arr[mid] != key) {
			if (key > arr[mid])
				min = mid + 1;
			else if (key < arr[mid])
				max = mid - 1;
			if (max < min)
				return min;
			mid = (min + max) / 2;
		}
		return mid;
	}
}

例题

 

 
/*
面试题:
给定一个有序的数组,如果往该数组中存储一个元素,并保证这个数组还是有序的,
那么个元素的存储的角标为如何获取。
{13,15,19,28,33,45,78,106};
*/
class Zz
{
	public static void main (String [] args)
	{
		int []arr ={13,15,19,28,33,45,78,106};
		int Index = halfSearch(arr,50);
		System.out.println(Index);
	}	
	public static int halfSearch(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;
	}
}

int index1 = Arrays.binarySearch(arr,5);

如果存在返回的具体的角标位置

不存在返回的是  (-插入点-1)

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];
	}

}

十进制 转换为 二进制 和 十六进制

 

十进制 转换为 二进制

 

//十进制转二进制
//数组方法,和查表法。
public class Test1 {
	public static void main(String[] args) {
		int x = -60;
		toBin(x);
		System.out.println();
		toBinSearch(x);
	}

	public static void toBin(int x) {
		int[] arr = new int[32];
		int index = 0;
		while (x != 0) {
			arr[index++] = (x & 1);
			x = x >>> 1;
		}
		for (int z = index - 1; z >= 0; z--) {
			System.out.print(arr[z]);
		}
	}

	public static void toBinSearch(int x) {
		char[] chs = { '0', '1' };
		char[] arr = new char[32];
		int index = 0;
		while (x != 0) {
			arr[index++] = chs[(x & 1)];
			x = x >>> 1;
		}
		for (int z = index - 1; z >= 0; z--) {
			System.out.print(arr[z]);
		}
	}

 

十进制 转换为 十六进制

//十进制转十六进制
//数组方法,和查表法。
public class Test1 {
	public static void main(String[] args) {
		int x = -60;
		toHex(x);
		System.out.println();
		toHexSearch(x);
	}

	public static void toHex(int x) {
		char[] arr = new char[8];
		int index = 0;
		while (x != 0) {
			if ((x & 15) > 10)
				arr[index++] = (char) ((x & 15) - 10 + 'A');
			else
				arr[index++] = (char) ((x & 15) + '0');
			x = x >>> 4;
		}
		for (int z = index - 1; z >= 0; z--) {
			System.out.print(arr[z]);
		}
	}

	public static void toHexSearch(int x) {
		char[] chs = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
				'B', 'C', 'D', 'E', 'F' };
		char[] arr = new char[8];
		int index = 0;
		while (x != 0) {
			arr[index++] = chs[(x & 15)];
			x = x >>> 4;
		}
		for (int z = index - 1; z >= 0; z--) {
			System.out.print(arr[z]);
		}
	}
}

十进制转其他进制 合并法

 

//十进制转其他进制合并法
public class Test1 {
	public static void main(String[] args) {
		int x =0;
		toBin(x);
		System.out.println();
		toBa(x);
		System.out.println();
		toHex(x);
	}
	public static void toBin(int x){
		train(x,1,1);
	}
	public static void toBa(int x){
		train(x,7,3);
	}
	public static void toHex(int x){
		train(x,15,4);
	}
	public static void train(int x,int n,int m){
		if(x==0)
		{
			System.out.print(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 index =0;
		while(x!=0){
			arr[index++] = chs[x&n];
			x=x>>>m;
		}
		for(int z = index-1;z>=0;z--){
			System.out.print(arr[z]+" ");
		}
	}
}

 

二维数组

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

int [][] arr= { { ,,}{,,}{,,,} };

分别对二维数组中的每一个小数组进行初始化。

arr[0]=new int[2];

arr[1]=new int[1];

arr[2]=new int[3];

 

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一维数组的长度。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值