定义:
存储相同类型数据的一组数据。
数组的声明与赋值:
int []score; score=new int[3];
int score[]j; score=new int[]{23,4,23,43,2};
int age=new int[5];
int []age={0,34,4,23,3};
int []age=new int[]{23,34,2,34,4};
常见错误:
一:没有明确数组的大小。 int []score = new int[];
二:数组越界。 int age[2]; age[2]=23;
三:数组初始化错误。 int age[]; age={23,34,23,2};
四:没有给数组元素赋值。 int score[3]; score[0]=2; system.out.println(score[1]);
二重循环:
for(i=0,i<10,i++){
for(j=0,j<10,j++)
...........}
流程控制:
continue:结束本次循环,进行下次循环;
break:结束整个循环。
数组排序:
1 public static int[] bubbleSort(int[] args){//冒泡排序算法 2 for(int i=0;i<args.length-1;i++){ 3 for(int j=i+1;j<args.length;j++){ 4 if (args[i]>args[j]){ 5 int temp=args[i]; 6 args[i]=args[j]; 7 args[j]=temp; 8 } 9 } 10 } 11 return args; 12 }
1 public static int[] selectSort(int[] args){//选择排序算法 2 for (int i=0;i<args.length-1 ;i++ ){ 3 int min=i; 4 for (int j=i+1;j<args.length ;j++ ){ 5 if (args[min]>args[j]){ 6 min=j; 7 } 8 } 9 if (min!=i){ 10 int temp=args[i]; 11 args[i]=args[min]; 12 args[min]=temp; 13 } 14 } 15 return args; 16 }
1 import java.util.Arrays; 2 public class Test2{ 3 public static void main(String[] args){ 4 int[] a={5,4,2,4,9,1}; 5 Arrays.sort(a); //函数排序 6 for(int i: a){ 7 System.out.print(i); 8 } 9 } 10 }