代码改变世界

数组学习案例

2019-10-12 14:35  进击的菜鸟123  阅读(105)  评论(0)    收藏  举报

案例1:使用冒泡排序对数组进行排序

public static void sort(int temp[]){
  for(int i = 1;i <temp.length;i++){
    for(int j = 0;j < temp.length;j++){
      if(temp[i] <temp[j]){
        int x = temp[j];
        temp[j] = temp[i];
        temp[i] = x;
      }
    }
  }
}

与  java.util.Array.sort(temp)效果一直

 

案例2:数组的复制

//参数定义:源数组名称,源数组开始点,目标数组名称,目标数组开始点,复制长度
public static void copy(int s[],int s1,int o[],int s2,int len){
  for(int i=0;i < len;i++){
    o[s1+i] = s[s1+i];
  }
}

与System.arraycopy(s,s1,o,s2,len)一致