当前位置:文档之家› java中数组的学习完整版二

java中数组的学习完整版二

1.知识点A.数组的复制B.经典算法a)冒泡排序b)选择排序c)插入排序C.System类D.Arrays类2.讲解数组的复制:就是指从一个已知的数组中获取部分或全部的值,放入另外一个数组中。

方法一、采用循环的办法来做Eg1:class ArrayCopy{public static void main(String[] args){System.out.println("程序开始");int[] arrSrc={1,2,3,6,7,9};int[] arrDest=new int[arrSrc.length*2];System.out.println("目地数组原来的值的情况:");for(int i=0;i<arrDest.length;i++){System.out.print(arrDest[i]+" ");}System.out.print("\n");for(int i=0;i<arrSrc.length;i++){arrDest[i]=arrSrc[i];}System.out.println("目地数组现在的值的情况:");for(int i=0;i<arrDest.length;i++){System.out.print(arrDest[i]+" ");}System.out.print("\n");System.out.println("程序结束");}}Eg2: class ArrayCopy1{public static void main(String[] args){System.out.println("程序开始");int[] arrSrc={1,2,3,6,7,9};int[] arrDest=new int[arrSrc.length*2];System.out.println("目地数组原来的值的情况:");for(int i=0;i<arrDest.length;i++){System.out.print(arrDest[i]+" ");}System.out.print("\n");for(int i=0,j=2;i<arrSrc.length;i++,j++){arrDest[j]=arrSrc[i];}System.out.println("目地数组现在的值的情况:");for(int i=0;i<arrDest.length;i++){System.out.print(arrDest[i]+" ");}System.out.print("\n");System.out.println("程序结束");}}Eg3:import java.util.Scanner;class ArrayCopy2{public static void main(String[] args){System.out.println("程序开始");int[] arrSrc={1,2,3,6,7,9};int[] arrDest=new int[arrSrc.length*2];int posSrc=0;int posDest=0;Scanner sc=new Scanner(System.in);System.out.println("请输入要复制的起始位置:");posSrc=sc.nextInt();//这里要进行异常捕获,在这里暂时不写了System.out.println("请输入目的数组的起始位置:");posDest=sc.nextInt();System.out.println("目地数组原来的值的情况:");for(int i=0;i<arrDest.length;i++){System.out.print(arrDest[i]+" ");}System.out.print("\n");//开始复制if((arrSrc.length-posSrc)<(arrDest.length-posDest)){System.out.println("复制开始:...");for(int i=posSrc,j=posDest;i<arrSrc.length;i++,j++){arrDest[j]=arrSrc[i];}System.out.println("目地数组现在的值的情况:");for(int i=0;i<arrDest.length;i++){System.out.print(arrDest[i]+" ");}System.out.print("\n");}else{System.out.println("复制失败...");}System.out.println("程序结束");}}Eg4:import java.util.Scanner;class ArrayCopy3{public static void main(String[] args){System.out.println("程序开始");int[] arrSrc={1,2,3,6,7,9};int[] arrDest=new int[arrSrc.length*2];int posSrc=0;int posDest=0;int copyLength=0;Scanner sc=new Scanner(System.in);System.out.println("请输入要复制的起始位置:");posSrc=sc.nextInt();//这里要进行异常捕获,在这里暂时不写了System.out.println("请输入目的数组的起始位置:");posDest=sc.nextInt();//这里要进行异常捕获,在这里暂时不写了System.out.println("请输入数组要复制的长度:");copyLength=sc.nextInt();//这里要进行异常捕获,在这里暂时不写了System.out.println("目地数组原来的值的情况:");for(int i=0;i<arrDest.length;i++){System.out.print(arrDest[i]+" ");}System.out.print("\n");//开始复制if(copyLength<(arrSrc.length-posSrc)&&copyLength<(arrDest.length-posDest)){ System.out.println("复制开始:...");for(int i=posSrc,j=posDest;i<=copyLength;i++,j++){arrDest[j]=arrSrc[i];}System.out.println("目地数组现在的值的情况:");for(int i=0;i<arrDest.length;i++){System.out.print(arrDest[i]+" ");}System.out.print("\n");}else{System.out.println("复制失败...");}System.out.println("程序结束");}}Eg5:import java.util.Scanner;class ArrayCopy4{public static void main(String[] args){System.out.println("程序开始");int[] arrSrc={1,2,3,6,7,9};int[] arrDest=new int[arrSrc.length*2];int posSrc=0;int posDest=0;int copyLength=0;Scanner sc=new Scanner(System.in);System.out.println("请输入要复制的起始位置:");posSrc=sc.nextInt();//这里要进行异常捕获,在这里暂时不写了System.out.println("请输入目的数组的起始位置:");posDest=sc.nextInt();//这里要进行异常捕获,在这里暂时不写了System.out.println("请输入数组要复制的长度:");copyLength=sc.nextInt();//这里要进行异常捕获,在这里暂时不写了System.out.println("目地数组原来的值的情况:");for(int i=0;i<arrDest.length;i++){System.out.print(arrDest[i]+" ");}System.out.print("\n");//开始复制if(arrayCopy(arrSrc,posSrc,arrDest,posDest,copyLength)){System.out.println("目地数组现在的值的情况:");for(int i=0;i<arrDest.length;i++){System.out.print(arrDest[i]+" ");}System.out.print("\n");}System.out.println("程序结束");}public static boolean arrayCopy(int arraySrc[],int posSrc,int arrayDest[],int posDest,int copyLength){boolean flag=false;if(copyLength<=(arraySrc.length-posSrc)&&copyLength<=(arrayDest.length-posDest)){ System.out.println("复制开始:...");for(int i=posSrc,j=posDest;i<=copyLength;i++,j++){arrayDest[j]=arraySrc[i];}flag=true;}else{System.out.println("复制失败...");}return flag;}}方法二、采用系统提供的类来做(Systempublic static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)eg:class ArrayCopy5{public static void main(String[] args){System.out.println("程序开始");int[] arrSrc={1,2,3,4,5};int[] arrDest=new int[arrSrc.length*2];try{System.arraycopy(arrSrc,2,arrDest,8,arrSrc.length-2);for(int i=0;i<arrDest.length;i++){System.out.print( arrDest[i]+" ");}//运行有问题,因为越界了}catch(IndexOutOfBoundsException e0){e0.printStackTrace();}catch(ArrayStoreException e1){e1.printStackTrace();}catch(NullPointerException e2){e2.printStackTrace();}System.out.println("");System.out.println("程序结束");}}经典算法冒泡排序原理Eg:class SortBubbled{public static void main(String[] args){System.out.println("程序开始");int[] arr={6,5,4,3,2,1};//数组的原样输出System.out.println("排序前的数组:");for(int i=0;i<arr.length;i++){System.out.print(arr[i]+" ");}System.out.println();//排序开始for(int ciShu=0;ciShu<arr.length-1;ciShu++){//这个循环控制排序的次数,6个数,我要排5次完成有序for(int j=0;j<arr.length-1-ciShu;j++){//这个循环,进行具体的每一次的排序。

相关主题