黑马程序员————学习日记【4】 【Java基础编程3】

------- android培训java培训、期待与您交流! ----------

 

 一、数组

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

		//需求:想定义一个可以存储3个整数的容器
		int[] x= new int[3];

		//打印数组中角标为0 的元素的值。
		System.out.println(x[0]);


	}
}


 

 

class ArrayDemo
{
	public static void main(String[] args)
	{
		//数组的操作
		//获取数组中的元素.通常会用到遍历。

//		int[] arr = new int[3];
		int[] arr = {3,6,5,1,8,9,67};

		//数组中有一个属性可以直接获取到数组元素个数。length
		//使用方式:数组名称.length = 
		System.out.println("length:"+arr.length);

		for(int x=0; x<arr.length; x++)//角标的最大值就是长度-1
		{
			System.out.println("arr["+x+"]="+arr[x]+";");//arr[0]=0;
		}
		//但凡有数组的情况下,通常会用到for循环
		//因为要把里面的数据取出来用,取是最常见的。

	}
}


 

class ArrayDemo
{
	public static void main(String[] args)
	{
		//数组的操作
		//获取数组中的元素.通常会用到遍历。

//		int[] arr = new int[3];
		int[] arr = {3,6,5,1,8,9,67};

		//数组中有一个属性可以直接获取到数组元素个数。length
		//使用方式:数组名称.length = 
		System.out.println("length:"+arr.length);

		for(int x=0; x<arr.length; x++)//角标的最大值就是长度-1
		{
			System.out.println("arr["+x+"]="+arr[x]+";");//arr[0]=0;
		}
		//但凡有数组的情况下,通常会用到for循环
		//因为要把里面的数据取出来用,取是最常见的。

	}
}


class ArrayDemo
{
	public static void main(String[] args)
	{
		int[] arr = {3,5,5,2,8,9,1};

		System.out.println("length:"+arr.length);

		//求和:这是累加,即为变量+循环
		int sum = 0;
		for(int x=0; x<arr.length; x++)//角标的最大值就是长度-1
		{
			sum +=arr[x];
		}
		System.out.println(sum);
	}
}


class ArrayDemo
{
	public static void main(String[] args)
	{
		int[] arr = {3,5,5,2,8,9,1};
		printArray(arr);
		
	}
	//定义功能,用于打印数组中的元素,元素间用逗号隔开。
	public static void printArray(int[] arr)
	{
		for(int x=0; x<arr.length; x++)
		{
			if(x!=arr.length-1)
				System.out.print(arr[x]+",");
			else
				System.out.println(arr[x]);
		}
	}
}


 

 

class ArrayDemo
{
	public static void main(String[] args)
	{
		int[] arr = {3,5,5,2,8,9,1};

//		printArray(arr);
		System.out.println(arr);
		
	}
	//定义功能,用于打印数组中的元素,元素间用逗号隔开。
	public static void printArray(int[] arr)
	{
		for(int x=0; x<arr.length; x++)
		{
			if(x!=arr.length-1)
				System.out.print(arr[x]+",");
			else
				System.out.println(arr[x]);
		}
	}
}


结果为: 

class ArrayDemo
{
	public static void main(String[] args)
	{
		int[] arr = {3,5,5,2,8,9,1};

		printArray(arr);
//		System.out.println(arr);
		
	}
	//定义功能,用于打印数组中的元素,元素间用逗号隔开。
	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]+"]");
		}
	}
}


这样打印更美观: 

 

class ArrayTest
{
	/*
	获取最大值

	思路:
	1、获取最值需要比较,每次比较都会有一个较大值,因为该值不确定,
	   通过一个变量进行临时存储。
	2、让数组中的每一个元素都和这个变量中的值进行比较。
	   如果大于了变量中的值,就用该变量记录较大值
	3、当所有元素都比较完成,那么该变量中存储的就是数组中的最大值

	步骤:
	1、定义变量。初始化为数组中任意一个元素即可
	2、通过循环语句对数组进行遍历
	3、在变量过程中定义判断条件,如果遍历到的元素比变量中的元素大,就
	   赋值给该变量 

    需要定义一个功能来完成,以便提高复用性
	1、明确结果,数组中的最大元素 int
	2、未知内容:一个数组 int[]
	*/
	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;
	}
	public static void main(String[] args)
	{
		int[] arr ={5,1,6,4,2,8,9};

		int max = getMax(arr);
		System.out.println("max="+max);
	}
}


/*
给定一个数组{5,1,6,4,2,8,9}

*/

class ArrayTest
{
	/*
	获取最小值

	*/
	public static int getMin(int[] arr)
	{
		int min = arr[0];
		for(int x=1; x<arr.length; x++)
		{
			if(arr[x]<min)
				min = arr[x];
		}
		return min;
	}
	public static void main(String[] args)
	{
		int[] arr ={5,1,6,4,2,8,9};

		int min = getMin(arr);
		System.out.println("min="+min);
	}
}


/*
给定一个数组{5,1,6,4,2,8,9}

*/

class ArrayTest
{
	/*
	获取最大值--另一种方式

	能否将临时变量初始化为0呢?
	可以,这种方式其实是在初始化为数组中任意一个角标

	*/
	public static int getMax(int[] arr)
	{
		int max = 0;
		for(int x=1; x<arr.length; x++)
		{
			if(arr[x]>arr[max])
				max = x;
		}
		return arr[max];
	}
	public static void main(String[] args)
	{
		int[] arr ={5,1,6,4,2,8,9};

		int max = getMax(arr);
		System.out.println("max="+max);
	}
}


 

/*
给定一个数组{5,1,6,4,2,8,9}

*/

class ArrayDemo
{
	/*
	获取最小值---另一种方式

	*/
	public static int getMin(int[] arr)
	{
		int min = 0;
		for(int x=1; x<arr.length; x++)
		{
			if(arr[x]<arr[min])
				min = x;
		}
		return arr[min];
	}
	public static void main(String[] args)
	{
		int[] arr ={5,1,6,4,2,8,9};

		int min = getMin(arr);
		System.out.println("min="+min);
	}
}


//获取double类型数组的最大值,因为功能一致,所以定义相同函数名称,以重载形式存在
	/*
	public static double getMax(double[] arr)
	{
	
	}
	*/


 

public static void main(String[] args)
	{
		boolean[] ar = new boolean[3];
		System.out.println(ar[1]);
	}
	//最值只适用于数值型数据,没有boolean。


结果: 

 

 

/*
对给定数组进行排序
{5,1,6,4,2,8,9}
*/
class ArrayTest 
{
	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])
				{
					int temp = arr[x];
					arr[x] = arr[y];
					arr[y] = temp;
				}
			}
		}
	}

	public static void main(String[] args)
	{
		int[] arr = {5,1,6,4,2,8,9};
		//排序前
		printArray(arr);
		//排序
		selectSort(arr);
		//排序后
		printArray(arr);
	}

	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]+"]");  
        }  
    }  

}


结果: 这是从小到大升序,如想从大到小倒序,将if(arr[x]>arr[y])改为<即可。一般都升序排列。

 

 

 

/*
对给定数组进行排序
{5,1,6,4,2,8,9}
*/
class ArrayTest 
{
	/*
	冒泡排序
	*/
	public static void bubbleSort(int[] arr)
	{
		for(int x=0; x<arr.length-1; x++)
		{
			for(int y=0; y<arr.length-x-1; y++)//-x:让每一次比较的元素减少   -1:避免角标越界
			{
				if(arr[y]>arr[y+1])
				{
					int temp = arr[y];
					arr[y] = arr[y+1];
					arr[y+1] = temp;
				}
			}
		}
	}

	public static void main(String[] args)
	{
		int[] arr = {5,1,6,4,2,8,9};
		//排序前
		printArray(arr);
		//排序
		bubbleSort(arr);
		//排序后
		printArray(arr);
	}

	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]+"]");  
        }  
    }  

}


 下面的开发时候用:

 

/*
对给定数组进行排序
{5,1,6,4,2,8,9}
*/

import java.util.*;
class ArrayTest 
{
	public static void main(String[] args)
	{
		/*
		冒泡排序
		*/
		int[] arr = {5,1,6,4,2,8,9};

		//排序前
		printArray(arr);
		

		Arrays.sort(arr);//java中已经定义好的一种排序方式,开发时使用。


		//排序后
		printArray(arr);
	}

	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]+"]");  
        }  
    }  

}


 

排序位置置换功能抽取:

/*
对给定数组进行排序
{5,1,6,4,2,8,9}
*/


class ArrayDemo
{
	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])
				{
					/*
					int temp = arr[x];
					arr[x] = arr[y];
					arr[y] = temp;
					*/
					swap(arr,x,y);
				}
			}
		}
	}
	/*
	冒泡排序
	*/
	public static void bubbleSort(int[] arr)
	{
		for(int x=0; x<arr.length-1; x++)
		{
			for(int y=0; y<arr.length-x-1; y++)//-x:让每一次比较的元素减少   -1:避免角标越界
			{
				if(arr[y]>arr[y+1])
				{
					/*
					int temp = arr[y];
					arr[y] = arr[y+1];
					arr[y+1] = temp;
					*/
					swap(arr,y,y+1);
				}
			}
		}
	}

	/*
	发现无论什么排序,都需要对满足条件的元素进行位置置换
	所以可以把这部分相同的代码提取出来,单独封装成一个函数
	*/
	public static void swap(int[] arr,int a,int b)
	{
		int temp = arr[a];
		arr[a] = arr[b];
		arr[b] = temp;
	}

	public static void main(String[] args)
	{
		int[] arr = {5,1,6,4,2,8,9};
		//排序前
		printArray(arr);
		//排序
		bubbleSort(arr);
		//排序后
		printArray(arr);
	}

	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]+"]");  
        }  
    }  

}

 

import java.util.*;
/*
对给定数组进行排序
{5,1,6,4,2,8,9}
*/
class ArrayTest2 
{
	/*
	选择排序
	内循环结束一次,最值出现头角标位置上
	*/
	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])
				//if(arr[x]<arr[y])
				{
					/*
					int temp = arr[x];
					arr[x] = arr[y];
					arr[y] = temp;
					*/
					swap(arr,x,y);
				}
			}
		}
	}
	
	/*
	冒泡排序
	*/

	public static void bubbleSort(int[] arr)
	{
		for(int x=0; x<arr.length-1; x++)//for(int x=arr.length-1; x>0; x--)
		{								//for(int y=0; y<x; y++)
			for(int y=0; y<arr.length-x-1; y++)
			// -x:让每一次比较的元素减少; -1:避免角标越界
			{
				if(arr[y]<arr[y+1])
				{
					/*
					int temp = arr[y];
					arr[y] = arr[y+1];
					arr[y+1] = temp;
					*/
					swap(arr,y,y+1);
				}
			}

		}
	}

	/*
	发现无论什么排序,都需要对满足条件的元素进行位置置换。
	所以可以把这部分相同的代码提取出来,单独封装成一个函数。
	*/
	public static void swap(int[] arr, int a, int b)
	{
		int temp = arr[a];
		arr[a] = arr[b];
		arr[b]= temp;
	}

	public static void main(String[] args)
	{
		int[] arr = {5,1,6,4,2,8,9};
		//排序前
		printArray(arr);

		//排序
		//selectSort(arr);
		
		//冒泡排序
		bubbleSort(arr);

		//Arrays.sort(arr);
		//java中已经定义好的一种排序方式,开发中,对数组排序
		//要使用该句代码。

		//排序后
		printArray(arr);
	}

	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]+"]");
		}
	}
}


 


 

/*
数组的查找操作

练习:有一个有序的数组,想要将一个元素插入到该数组中,插入后还要
保证该数组是有序的,如何获取该元素在数组中的位置。
*/
class ArrayTest
{
	public static void main(String[] args)
	{		
		int[] arr = {2,4,5,7,19,32,45};
		int index = getIndex(arr,35);
		System.out.println("index="+index);
	}

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

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


 

class ArrayTest7   
{
	public static void main(String[] args) 
	{
//		toBin(6);
//		toBin(-6);
//		toHex(60);
//		toHex(-60);
//		toBa(60);
		toBa(-60);
	}

	/*
	十进制-->二进制
	*/
	public static void toBin(int num)
	{
		trans(num,1,1);
	}
	/*
	十进制-->八进制
	*/
	public static void toBa(int num)
	{
		trans(num,7,3);
	}
	/*
	十进制-->十六进制
	*/
	public static void toHex(int num)
	{
		trans(num,15,4);
	}
	public static String trans(int num, int base, int offset)
	{
		//如果向里面传元素0,下面的循环就不运行了,
		//所以要做以下判断:
		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]);
		}
	}
}





 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值