数组

定义:

存储相同类型数据的一组数据。

数组的声明与赋值:

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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值