JAVA数组操作练习
Array数组高级操作
关键字 简介 copyOfRange 数组复制 toString() 转换为字符串 sort 排序 equals 判断是否相同 fill 填充
数组操作练习源码
package j2se_test;
public class miniNum {
public static void main ( String[ ] arg) {
int [ ] a = new int [ 6 ] ;
for ( int i = 0 ; i < a. length; i++ )
a[ i] = ( int ) ( Math. random ( ) * 100 ) ;
System. out. println ( "数组中的随机数是:" ) ;
for ( int i = 0 ; i < a. length; i++ )
System. out. println ( a[ i] ) ;
int temp = a[ 0 ] ;
for ( int i = 1 ; i < a. length; i++ ) {
if ( a[ i] < temp)
temp = a[ i] ;
}
System. out. println ( "最小数为:" + temp) ;
for ( int i = 0 ; i < a. length/ 2 ; i++ ) {
int j = ( a. length - i- 1 ) ;
temp = a[ i] ;
a[ i] = a[ j] ;
a[ j] = temp;
}
System. out. println ( "数组翻转为:" ) ;
for ( int i = 0 ; i < a. length; i++ )
System. out. println ( a[ i] ) ;
for ( int i = 0 ; i < a. length - 1 ; i++ ) {
for ( int j = i+ 1 ; j < a. length; j++ ) {
if ( a[ j] < a[ i] ) {
temp = a[ j] ;
a[ j] = a[ i] ;
a[ i] = temp;
}
}
}
System. out. println ( "选择正排序为:" ) ;
for ( int i = 0 ; i < a. length; i++ )
System. out. println ( a[ i] ) ;
for ( int i = 0 ; i < a. length- 1 ; i++ ) {
for ( int j = 0 ; j < ( a. length- i- 1 ) ; j++ ) {
if ( a[ j] < a[ j+ 1 ] ) {
temp = a[ j] ;
a[ j] = a[ j+ 1 ] ;
a[ j+ 1 ] = temp;
}
}
}
System. out. println ( "冒泡逆排序为:" ) ;
for ( int i = 0 ; i < a. length; i++ )
System. out. println ( a[ i] ) ;
System. out. println ( "增强型for循环遍历:" ) ;
for ( int each : a) {
System. out. print ( each + " " ) ;
}
System. out. println ( "\n数组复制:src, srcPos, dest, destPos, length" ) ;
int b[ ] = new int [ 3 ] ;
System. arraycopy ( a, 3 , b, 0 , 3 ) ;
for ( int i = 0 ; i < b. length; i++ ) {
System. out. print ( b[ i] + " " ) ;
}
System. out. println ( "数组合并:" ) ;
int arrayALength = ( int ) ( ( ( Math. random ( ) * 100 ) % 5 ) + 5 ) ;
int arrayBLength = ( int ) ( ( ( Math. random ( ) * 100 ) % 5 ) + 5 ) ;
int arrayA [ ] = new int [ arrayALength] ;
int arrayB [ ] = new int [ arrayBLength] ;
System. out. println ( "A数组随机的长度为:" + arrayALength + "\n" + "B数组随机的长度为:" + arrayBLength) ;
for ( int i = 0 ; i < arrayALength; i++ )
arrayA[ i] = ( int ) ( Math. random ( ) * 100 ) ;
System. out. println ( "数组A的值" ) ;
for ( int each : arrayA)
System. out. print ( each + " " ) ;
for ( int i = 0 ; i < arrayBLength; i++ )
arrayB[ i] = ( int ) ( Math. random ( ) * 100 ) ;
System. out. println ( "\n数组B的值" ) ;
for ( int each : arrayB)
System. out. print ( each + " " ) ;
int arrayC [ ] = new int [ arrayALength + arrayBLength] ;
System. arraycopy ( arrayA, 0 , arrayC, 0 , arrayALength) ;
System. arraycopy ( arrayB, 0 , arrayC, arrayALength, arrayBLength) ;
System. out. println ( "\n数组C的值" ) ;
for ( int each : arrayC)
System. out. print ( each + " " ) ;
System. out. println ( "\n二维数组" ) ;
int twoDArray[ ] [ ] = new int [ 5 ] [ 5 ] ;
for ( int i = 0 ; i < twoDArray. length; i++ )
for ( int j = 0 ; j < twoDArray[ 0 ] . length; j++ )
twoDArray[ i] [ j] = ( int ) ( Math. random ( ) * 100 ) ;
for ( int [ ] each : twoDArray) {
for ( int k : each)
System. out. print ( k + "\t" ) ;
System. out. println ( ) ;
}
}
}
最后再感谢大海同学(手动滑稽)