public class ArrayRefDemo01{ public static void main(String args[]){ int temp[] = {1,3,5} ; // 利用静态初始化方式定义数组 fun(temp) ; // 传递数组 for(int i=0;i<temp.length;i++){ System.out.print(temp[i] + "、") ; } } public static void fun(int x[]){ // 接收整型数组的引用 x[0] = 6 ; // 修改第一个元素 } };
public class ArrayRefDemo02{ public static void main(String args[]){ int temp[] = fun() ; // 通过方法实例化数组 print(temp) ; // 打印数组内容 } public static void print(int x[]){ for(int i=0;i<x.length;i++){ System.out.print(x[i] + "、") ; } } public static int[] fun(){ // 返回一个数组 int ss[] = {1,3,5,7,9} ; // 定义一个数组 return ss ; } };
public class ArrayRefDemo03{ public static void main(String args[]){ int score[] = {67,89,87,69,90,100,75,90} ; // 定义整型数组 int age[] = {31,30,18,17,8,9,1,39} ; // 定义整型数组 sort(score) ; // 数组排序 print(score) ; // 数组打印 System.out.println(" ---------------------------") ; sort(age) ; // 数组排序 print(age) ; // 数组打印 } 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[i] ; temp[i] = temp[j] ; temp[j] = x ; } } } } public static void print(int temp[]){ // 输出数组内容 for(int i=0;i<temp.length;i++){ System.out.print(temp[i] + " ") ; } } };
public class ArrayRefDemo04{ public static void main(String args[]){ int score[] = {67,89,87,69,90,100,75,90} ; // 定义整型数组 int age[] = {31,30,18,17,8,9,1,39} ; // 定义整型数组 java.util.Arrays.sort(score) ; // 数组排序 print(score) ; // 数组打印 System.out.println(" ---------------------------") ; java.util.Arrays.sort(age) ; // 数组排序 print(age) ; // 数组打印 } public static void print(int temp[]){ // 输出数组内容 for(int i=0;i<temp.length;i++){ System.out.print(temp[i] + " ") ; } } };
public class ArrayRefDemo05{ public static void main(String args[]){ int i1[] = {1,2,3,4,5,6,7,8,9} ; // 源数组 int i2[] = {11,22,33,44,55,66,77,88,99} ;// 目标数组 copy(i1,3,i2,1,3) ; // 调用拷贝方法 print(i2) ; } // 源数组名称,源数组开始点,目标数组名称,目标数组开始点,拷贝长度 public static void copy(int s[],int s1,int o[],int s2,int len){ for(int i=0;i<len;i++){ o[s2+i] = s[s1+i] ; // 进行拷贝操作 } } public static void print(int temp[]){ // 输出数组内容 for(int i=0;i<temp.length;i++){ System.out.print(temp[i] + " ") ; } } };
public class ArrayRefDemo06{ public static void main(String args[]){ int i1[] = {1,2,3,4,5,6,7,8,9} ; // 源数组 int i2[] = {11,22,33,44,55,66,77,88,99} ;// 目标数组 System.arraycopy(i1,3,i2,1,3) ; // 调用Java中对数组支持的拷贝方法 print(i2) ; } public static void print(int temp[]){ // 输出数组内容 for(int i=0;i<temp.length;i++){ System.out.print(temp[i] + " ") ; } } };