java学习笔记

1、java.util包下有一些工具类,比如Collections、Arrays等。

2、注意一下,File的getName方法

public class FileDemo {
	public static void main(String[] args)
	{
		listRootsMethod();
	}
	 public static void listRootsMethod(){
		 
		 File f = new File("F:\\");//打印输出为空
         System.out.println(f.getName());
         
         File f1 = new File("F:\\软件\\Adobe\\");
         System.out.println(f1.getName());//打印输出为adobe      
 }

}

3、经典选择排序代码:

public static void selectSortA(int[] arr)
	{
		for( int i = 0; i < arr.length; i++ )
		{
			for( int j = i + 1; j < arr.length; j++ )
			{
				if( arr[i] > arr[j] )
				{
					int temp = arr[i];
					arr[i] = arr[j];
					arr[j] = temp;
				}
			}
		}
	}

优化后的选择排序代码:

public static void selectSort(int[] arr)
	{
		for( int i = 0; i < arr.length; i++ )
		{
			int temp = i;
			for( int j = i+1; j < arr.length; j++ )
			{
				if( arr[temp] > arr[j] )
				{
					temp = j;
				}
			}
			
			if( temp != i )
			{
				arr[temp] = arr[temp] ^ arr[i];
				arr[i] = arr[temp] ^ arr[i];
				arr[temp] = arr[temp] ^ arr[i];
			}
			
		}
		
	}
该优化代码因为降减少了各元素之间的交换次数,所以提高了效率!

4、经典冒泡排序的代码:

public static void bubbleSortA(int[] arr)
	{
		for( int i = 0; i < arr.length - 1; i++ )
		{
			for( int j = 0; j < arr.length - 1; j++ )
			{
				if( arr[j] > arr[j+1] )
				{
					int temp = arr[j];
					arr[j] = arr[j+1];
					arr[j+1] = temp;
				}
			}
		}
	}
冒泡排序的优化:

public static void bubbleSort(int[] arr)
	{
		for( int i = 0; i < arr.length -1; i++ )
		{
			int temp = 0;
			int j;
			for( j = 1; j < arr.length - i; j++ )
			{
	
				if( arr[temp] < arr[j] )
				{
					temp = j;
				}
			}
			
			if( temp != j-1 )
			{
				arr[temp] = arr[temp]^arr[j-1];
				arr[j-1] = arr[temp]^arr[j-1];
				arr[temp] = arr[temp]^arr[j-1];
				
			}
		}
		
	}

5、另外,找到一个异或符号交换两数数值的弊端,当要交换的两数是为同一片内存位置上的数时,异或的结果就变为了0,如下:

int a = 9;
		
		a = a ^ a;
		System.out.println(a);//打印结果为0
		a = a ^ a;
		System.out.println(a);//打印结果为0
		a = a ^ a;
		System.out.println(a);//打印结果为0

6、BufferedReader的readLine方法在读取数据时,注意要该数据的结尾要有回车符!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值