数组的定义
public class TestArr1 {
public static void main ( String [ ] args) {
int [ ] arr;
arr = new int [ 5 ] ;
int len = arr. length;
System . out. println ( len) ;
System . out. println ( arr) ;
}
}
数组默认值
public class TestArr2 {
public static void main ( String [ ] args) {
int [ ] arr = new int [ 5 ] ;
int a1 = arr[ 0 ] ;
int a2 = arr[ 0 ] ;
int a3 = arr[ 0 ] ;
int a4 = arr[ 0 ] ;
int a5 = arr[ 0 ] ;
System . out. println ( a1) ;
System . out. println ( a2) ;
System . out. println ( a3) ;
System . out. println ( a4) ;
System . out. println ( a5) ;
float [ ] arr2 = new float [ 2 ] ;
System . out. println ( arr2[ 0 ] ) ;
String [ ] arr3 = new String [ 2 ] ;
System . out. println ( arr3[ 0 ] ) ;
double [ ] arr4 = new double [ 2 ] ;
System . out. println ( arr4[ 0 ] ) ;
boolean [ ] arr5 = new boolean [ 2 ] ;
System . out. println ( arr5[ 0 ] ) ;
}
}
数组遍历
public class TestArr3 {
public static void main ( String [ ] args) {
XuanZeSort xuanZeSort = new XuanZeSort ( ) ;
int [ ] arr;
arr= new int [ 2 ] ;
int [ ] arr2 = new int [ 2 ] ;
int [ ] arr3 = new int [ ] { 1 , 2 , 3 , 4 } ;
int [ ] arr4 = { 5 , 3 , 7 , 1 } ;
for ( int i = 0 ; i< arr4. length; i++ ) {
System . out. print ( arr4[ i] + " " ) ;
}
System . out. println ( ) ;
xuanZeSort. selectsort ( arr4) ;
}
}
获取数组平均值
public class Demo1 {
public static void main ( String [ ] args) {
int [ ] arr = { 1 , 2 , 3 , 4 } ;
avg ( arr) ;
}
private static void avg ( int [ ] arr) {
double sum = 0 ;
for ( int i = 0 ; i < arr. length; i++ ) {
int temp = arr[ i] ;
sum = sum+ temp;
}
System . out. println ( sum/ arr. length) ;
}
}
冒泡排序,选择排序,插入排序
public class Demo2 {
public static void main ( String [ ] args) {
int [ ] arr = { 2 , 0 , 4 , 7 , 8 , 5 , 3 } ;
int [ ] arr2 = { 2 , 3 , 4 , 6 , 8 , 5 , 9 , 1 } ;
insertSort ( arr) ;
}
private static void insertSort ( int [ ] arr) {
for ( int i = 1 ; i < arr. length; i++ ) {
int shouldIndex = i;
int currentV = arr[ i] ;
for ( int j = i- 1 ; j>= 0 ; j-- ) {
if ( currentV< arr[ j] ) {
arr[ j+ 1 ] = arr[ j] ;
shouldIndex= j;
} else {
break ;
}
}
if ( shouldIndex!= i) {
arr[ shouldIndex] = currentV;
}
}
System . out. println ( Arrays . toString ( arr) ) ;
}
private static void selectSort ( int [ ] arr) {
for ( int i = 0 ; i < arr. length- 1 ; i++ ) {
int min = i;
for ( int j = i+ 1 ; j< arr. length; j++ ) {
if ( arr[ min] > arr[ j] ) {
min= j;
}
}
if ( i!= min) {
int temp = arr[ min] ;
arr[ min] = arr[ i] ;
arr[ i] = temp;
}
System . out. println ( "第" + i+ "轮的结果:" + Arrays . toString ( arr) ) ;
}
System . out. println ( Arrays . toString ( arr) ) ;
}
private static void bubberSort ( int [ ] arr) {
for ( int i = 0 ; i < arr. length- 1 ; i++ ) {
for ( int j= 0 ; j< arr. length- 1 - i; j++ ) {
if ( arr[ j] > arr[ j+ 1 ] ) {
int temp = arr[ j] ;
arr[ j] = arr[ j+ 1 ] ;
arr[ j+ 1 ] = temp;
}
System . out. println ( "第" + i+ "轮第" + j+ "次的结果" + Arrays . toString ( arr) ) ;
}
System . out. println ( "第" + i+ "轮的结果" + Arrays . toString ( arr) ) ;
}
for ( int i = 0 ; i < arr. length; i++ ) {
System . out. print ( arr[ i] + " " ) ;
}
}
}
数组拷贝
int [ ] arr = { 5 , 8 , 0 , 2 , 3 , 1 } ;
System . out. println ( Arrays . toString ( arr) ) ;
arr = Arrays . copyOf ( arr, arr. length<< 1 ) ;
System . out. println ( Arrays . toString ( arr) ) ;