package day4_array; import java.util.Arrays; import java.util.Scanner; public class ArrayExam { public static void main(String[] args) { int[]array=new int[]{34,5,22,-98,6,-76,0,-3}; //3.复制上述数组,得到一个新的数组 int[] arr=new int[array.length]; for(int i=0;i<array.length;i++) { arr[i]=array[i]; }System.out.println(Arrays.toString(arr)); /* 使用冒泡排序,实现如下数组从小到大排序 int[]arr=new int[]{34,5,22,-98,6,-76,0,-3} */ for(int i=0;i<array.length-1;i++) { for(int j=0;j<array.length-1-i;j++) { if(array[j]>array[j+1]) { int temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } } }System.out.println(Arrays.toString(array)); //快速排序法 Arrays.sort(arr); System.out.println(Arrays.toString(arr)); //2.反转上述数组 for(int i=0;i<array.length/2;i++) { int temp=array[i]; array[i]=array[array.length-1-i]; array[array.length-1-i]=temp; }System.out.println(Arrays.toString(array)); //4.使用线性、二分查找,从上述数组中查找22是否存在,存在,则返回索引,否则输出提示信息 //线性 int index=22; boolean isExit=false; for(int i=0;i<arr.length;i++) { if(arr[i]==index) { System.out.println("存在,位置是:"+i); isExit=true; break; } }if(!isExit) { System.out.println("没找到该数字"); } //二分法 int head=0; int end=arr.length; while(head<=end) { int middle=(head+end)/2; if(index>arr[middle]) { head=middle+1; }else if(index<arr[middle]) { end=middle-1; }else { System.out.println("找到了,位置是:"+middle); break; } }if(head>end) { System.out.println("没找到该数字"); } //二分法查找Arrays库 int num=Arrays.binarySearch(arr, index); if(num>=0) { System.out.println("找到了,位置是:"+num); }else { System.out.println("没找到该数字"); } /* *第2题!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!太难了做不出来了!!!!!!!!!!!!!!!!!!!!!!!! 如果控制台输入一个3,打印如下矩阵: 1 2 3 8 9 4 7 6 5 如果输入的是5,打印如下矩阵: 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 */ Scanner s=new Scanner(System.in); System.out.println("请输入您要大小"); int i=s.nextInt(); int [][] arr1=new int[i][i]; int x=0; int y=0; int number=1; while (arr1[x][y]!=(i*i)){ arr1[x][y]=number; //定位 if(y!=i-1&&arr1[x][y+1]==0){ if(x>0&&arr1[x-1][y]==0){ x--; }else{y++;} } else if(x!=i-1&&arr1[x+1][y]==0){ x++; } else if(y>0&&arr1[x][y-1]==0){ y--; } else if(x>0&&arr1[x-1][y]==0){ x--; } number++; } //打印 for(int i1=0;i1<arr1.length;i1++){ for(int i2=0;i2<arr1.length;i2++){ System.out.print(arr1[i1][i2]+"\t"); } System.out.println(); }//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! } }
标签:Java,--,day4,System,int,arr1,println,array,out From: https://www.cnblogs.com/NGZS/p/16861896.html