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方法在读取数据时,注意要该数据的结尾要有回车符!