数组练习
数组的交换、填充、拷贝及二分查找
import java. util. Arrays;
public class Course0718
{
public static void swap ( int [ ] a, int i, int j)
{
int t = a[ i] ;
a[ i] = a[ j] ;
a[ j] = t;
}
public static int indexOf ( int [ ] a, int v)
{
for ( int i = 0 ; i < a. length; i++ )
{
if ( a[ i] == v)
{
return i;
}
}
return - 1 ;
}
public static void fill ( int [ ] a, int v)
{
for ( int i = 0 ; i < a. length; i++ )
{
a[ i] = v;
}
}
public static int [ ] copyOf ( int [ ] original, int newLength)
{
int [ ] dest = new int [ newLength] ;
int length = original. length <= newLength ? original. length : newLength;
for ( int i = 0 ; i < length; i++ )
{
dest[ i] = original[ i] ;
}
return dest;
}
public static int binarySearch ( int [ ] a, int v)
{
int left = 0 ;
int right = a. length;
while ( left < right)
{
int mid = ( left + right) / 2 ;
if ( v == a[ mid] )
{
return mid;
} else if ( v < a[ mid] )
{
right = mid;
} else
{
left = mid + 1 ;
}
}
return - 1 ;
}
public static int binarySearch2 ( int [ ] a, int v)
{
int left = 0 ;
int right = a. length - 1 ;
while ( left <= right)
{
int mid = ( left + right) / 2 ;
if ( v == a[ mid] )
{
return mid;
} else if ( v < a[ mid] )
{
right = mid - 1 ;
} else
{
left = mid + 1 ;
}
}
return - 1 ;
}
public static void main ( String[ ] args)
{
int [ ] a = { 1 , 2 , 3 , 4 , 5 } ;
System. out. println ( a) ;
System. out. println ( Arrays. toString ( a) ) ;
copyOf ( a, 10 ) ;
System. out. println ( Arrays. toString ( Arrays. copyOf ( a, 10 ) ) ) ;
int [ ] b = new int [ 10 ] ;
System. arraycopy ( a, 0 , b, 5 , 5 ) ;
System. out. println ( Arrays. toString ( b) ) ;
}
}
补充说明:
将指定源数组中的数组元素从指定位置复制到目标数组的指定位置
public static void arraycopy ( Object src, int srcPos, Object dest, int destPos, int length)
参数:
src——源数组
srcPos—源数组中的起始位置( 从源数组中要复制的起始位置) ;
dest—— 目标数组
destPos——目标数组中的起始位置(复制到目标数组的起始位置);
length——复制数组元素的的长度
会出现的异常情况:
1. 如果src ( 源数组) 是null,则抛出NullPointerException并且目标数组不会被修改
2. 如果dest ( 目标数组) 是null,则抛出NullPointerException
冒泡排序
import java. util. Arrays;
public class bubbleSort
{
public static void bubbleSort ( int [ ] a)
{
int n= a. length;
for ( int i= 0 ; i< n; i++ )
{
for ( int j= 1 ; j< n- i; j++ )
{
if ( a[ j- 1 ] > a[ j] )
{
swap ( a, j- 1 , j) ;
}
}
}
}
public static void bubbleSort1 ( int [ ] a)
{
for ( int i= 0 ; i< a. length; i++ )
{
for ( int j= a. length - 1 ; j> i; j-- )
if ( a[ j] < a[ j- 1 ] )
{
swap ( a, j, j- 1 ) ;
}
}
}
public static void swap ( int [ ] a, int n, int m)
{
int t= a[ n] ;
a[ n] = a[ m] ;
a[ m] = t;
}
public static void testbubbleSort ( int [ ] a)
{
System. out . println ( "排序前" ) ;
System. out. println ( Arrays. toString ( a) ) ;
bubbleSort1 ( a) ;
System. out . println ( "排序后" ) ;
System. out. println ( Arrays. toString ( a) ) ;
}
public static void main ( String [ ] args)
{
int [ ] a = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ;
int [ ] b = { 7 , 6 , 5 , 4 , 3 , 2 , 1 } ;
int [ ] c = { 3 , 5 , 2 , 4 , 1 , 7 , 6 } ;
int [ ] d = { 3 , 3 , 3 , 3 , 3 , 3 , 3 } ;
testbubbleSort ( a) ;
testbubbleSort ( b) ;
testbubbleSort ( c) ;
testbubbleSort ( d) ;
}
}
去掉一个最大值和最小值的平均值
public class Averge
{
public static void average ( int [ ] a)
{
int sum = 0 ;
int max = a[ 0 ] ;
int min = a[ 0 ] ;
for ( int i= 0 ; i< a. length; i++ )
{
sum+= a[ i] ;
if ( a[ i] > max)
{
max= a[ i] ;
}
if ( a[ i] < min)
{
min= a[ i] ;
}
}
double ave = ( double ) ( sum- max- min) / ( a. length- 2 ) ;
System. out. println ( "最大值为:" + max) ;
System. out. println ( "最小值为:" + min) ;
System. out. println ( "总和为:" + sum) ;
System. out. println ( "平均值为:" + ave) ;
}
public static void main ( String [ ] args)
{
int [ ] a = { 1 , 2 , 3 , 4 , 5 } ;
average ( a) ;
}
}
数组右旋转
public class Arraysrotate
{
public static int [ ] rotate ( int [ ] nums, int k)
{
k= k% nums. length;
int [ ] rightPart= Arrays. copyOfRange ( nums, nums. length- k, nums. length) ;
System. arraycopy ( nums, 0 , nums, k, nums. length- k) ;
System. arraycopy ( rightPart, 0 , nums, 0 , k) ;
return nums;
}
public static void main ( String [ ] args)
{
int [ ] nums = { 1 , 2 , 3 , 4 , 5 } ;
System. out . println ( Arrays. toString ( rotate ( nums, 3 ) ) ) ;
}
}
使得一个数组,偶数在前,奇数在后
public class partition
{
public static int [ ] partition1 ( int [ ] nums)
{
int begin = 0 ;
int end = nums. length- 1 ;
while ( begin< end)
{
while ( begin< end&& nums[ begin] % 2 == 0 )
{
begin ++ ;
}
while ( begin< end&& nums[ end] % 2 != 0 )
{
end -- ;
}
swap ( nums, begin, end) ;
}
return nums;
}
public static int [ ] partition2 ( int [ ] nums)
{
int d= 0 ;
for ( int i= 0 ; i< nums. length; i++ )
{
if ( nums[ i] % 2 == 0 )
{
swap ( nums, i, d) ;
d++ ;
}
}
return nums;
}
public static void main ( String [ ] args)
{
int [ ] nums = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ;
System. out. println ( Arrays. toString ( partition2 ( nums) ) ) ;
}
}
打印100~200之间的素数
判断1000~2000年的所有闰年
public class Test
{
public static void printprime ( int i)
{
int j= 0 ;
for ( j= 2 ; j<= i; j++ )
{
if ( i% j== 0 )
{
break ;
}
}
if ( i== j)
{
System. out. print ( i+ " " )
}
}
public static void printleapYear ( int year)
{
if ( ( ( year % 4 == 0 ) && ( year % 100 != 0 ) ) || ( year % 400 == 0 ) )
{
System. out. print ( year+ " " ) ;
}
}
public static void main ( String [ ] args)
{
for ( int i= 100 ; i< 201 ; i++ )
{
printprime ( i) ;
}
for ( int year= 1000 ; year< 2001 ; year++ )
{
printleapYear ( year) ;
}
}
}