数组
package array;
public class ArrayDemo01 {
public static void main ( String[ ] args) {
int [ ] nums;
nums = new int [ 10 ] ;
nums[ 0 ] = 1 ;
nums[ 1 ] = 2 ;
nums[ 2 ] = 3 ;
nums[ 3 ] = 4 ;
nums[ 4 ] = 5 ;
nums[ 5 ] = 6 ;
nums[ 6 ] = 7 ;
nums[ 7 ] = 8 ;
nums[ 8 ] = 9 ;
nums[ 9 ] = 10 ;
int sum = 0 ;
for ( int i = 0 ; i < nums. length; i++ ) {
sum = sum + nums[ i] ;
}
System. out. println ( sum) ;
}
}
内存分析
声明一个数组的时候,只是在栈里插入了一个数组
int[] array = null;
只有在创建数据的时候,才会在堆里产生
array =new int [10];
初始化
package array;
public class ArrayDemo02 {
public static void main ( String[ ] args) {
int [ ] a= { 1 , 2 , 3 } ;
System. out. println ( a[ 0 ] ) ;
int [ ] b = new int [ 10 ] ;
b[ 0 ] = 1 ;
System. out. println ( b[ 0 ] ) ;
System. out. println ( b[ 1 ] ) ;
}
}
数组边界
数组的使用
package array;
public class ArrayDemo03 {
public static void main ( String[ ] args) {
int [ ] a= { 1 , 2 , 3 } ;
for ( int i = 0 ; i < a. length; i++ ) {
System. out. println ( a[ i] ) ;
}
System. out. println ( "==========" ) ;
int sum = 0 ;
for ( int i = 0 ; i < a. length ; i++ ) {
sum = sum + a[ i] ;
}
System. out. println ( "和等于" + sum) ;
int max = a[ 0 ] ;
for ( int i = 1 ; i < a. length; i++ ) {
if ( a[ i] > max) {
max = a[ i] ;
}
}
System. out. println ( "最大值" + max) ;
}
}
package array;
public class ArrayDemo04 {
public static void main ( String[ ] args) {
int [ ] arrays = { 1 , 2 , 3 , 4 , 5 } ;
for ( int array : arrays) {
System. out. println ( array) ;
}
}
}
多维数组
冒泡排序
package array;
import java. util. Arrays;
import java. util. concurrent. ForkJoinPool;
public class ArrayDemo05 {
public static void main ( String[ ] args) {
int [ ] a = { 1 , 5 , 6 , 7 , 8 , 10 , 65 , 14 , 2 , 2 , 66 } ;
int [ ] sort = sort ( a) ;
System. out. println ( Arrays. toString ( sort) ) ;
}
public static int [ ] sort ( int [ ] array) {
int temp = 0 ;
for ( int i = 0 ; i < array. length- 1 ; i++ ) {
for ( int j = 0 ; j < array. length- 1 - i; j++ ) {
if ( array[ j+ 1 ] < array[ j] ) {
temp = array[ j] ;
array[ j] = array[ j+ 1 ] ;
array[ j+ 1 ] = temp;
}
}
}
return array;
}
}
优化
package array;
import java. util. Arrays;
import java. util. concurrent. ForkJoinPool;
public class ArrayDemo05 {
public static void main ( String[ ] args) {
int [ ] a = { 1 , 5 , 6 , 7 , 8 , 10 , 65 , 14 , 2 , 2 , 66 } ;
int [ ] sort = sort ( a) ;
System. out. println ( Arrays. toString ( sort) ) ;
}
public static int [ ] sort ( int [ ] array) {
int temp = 0 ;
for ( int i = 0 ; i < array. length- 1 ; i++ ) {
boolean flag = false ;
for ( int j = 0 ; j < array. length- 1 - i; j++ ) {
if ( array[ j+ 1 ] < array[ j] ) {
temp = array[ j] ;
array[ j] = array[ j+ 1 ] ;
array[ j+ 1 ] = temp;
flag = true ;
}
}
if ( flag== false ) ; {
break ;
}
}
return array;
}
}
稀疏数组
package array;
public class ArrrayDemo08 {
public static void main ( String[ ] args) {
int [ ] [ ] array1 = new int [ 11 ] [ 11 ] ;
array1[ 1 ] [ 2 ] = 1 ;
array1[ 2 ] [ 3 ] = 2 ;
System. out. println ( "输出原始数组" ) ;
for ( int [ ] ints: array1) {
for ( int anInt: ints) {
System. out. print ( anInt+ "\t" ) ;
}
System. out. println ( ) ;
}
System. out. println ( "=========" ) ;
int sum = 0 ;
for ( int i = 0 ; i < 11 ; i++ ) {
for ( int j = 0 ; j < 11 ; j++ ) {
if ( array1[ i] [ j] != 0 ) {
sum++ ;
}
}
}
System. out. println ( "有效值" + sum) ;
int [ ] [ ] array2= new int [ sum+ 1 ] [ 3 ] ;
array2[ 0 ] [ 0 ] = 11 ;
array2[ 0 ] [ 1 ] = 11 ;
array2[ 0 ] [ 2 ] = sum;
int count = 0 ;
for ( int i = 0 ; i < array1. length; i++ ) {
for ( int j = 0 ; j < array1[ i] . length; j++ ) {
if ( array1[ i] [ j] != 0 ) {
count++ ;
array2[ count] [ 0 ] = i;
array2[ count] [ 1 ] = j;
array2[ count] [ 2 ] = array1[ i] [ j] ;
}
}
}
System. out. println ( "输出稀疏数组" ) ;
for ( int i = 0 ; i < array2. length; i++ ) {
System. out. println ( array2[ i] [ 0 ] + "\t" + array2[ i] [ 1 ] + "\t" + array2[ i] [ 2 ] + "\t" ) ;
}
System. out. println ( "=============" ) ;
System. out. println ( "还原" ) ;
int [ ] [ ] array3 = new int [ array2[ 0 ] [ 0 ] ] [ array2[ 0 ] [ 1 ] ] ;
for ( int i = 1 ; i < array2. length; i++ ) {
array3[ array2[ i] [ 0 ] ] [ array2[ i] [ 1 ] ] = array2[ i] [ 2 ] ;
}
System. out. println ( "输出原始数组" ) ;
for ( int [ ] ints: array1) {
for ( int anInt: ints) {
System. out. print ( anInt+ "\t" ) ;
}
System. out. println ( ) ;
}
}
}