zoukankan      html  css  js  c++  java
  • java基础--数组--练习集锦

    数组反转

    package zsc.czy.array;
    
    public class A {
    	public static void main(String[] args) {
      //最笨的方法
    		int a[] = new int[5];
    		for(int i = 0;i<a.length;i++){
    			a[i] = (int) (Math.random() * 100);
    			System.out.println(a[i]);
    		}
    		System.out.println();
    		int b[] = new int[5];
    		for (int i = 0; i < b.length; i++) {
    			b[i]= a[a.length-i-1];
    			System.out.println(b[i]);
    		}
    	}
    }
    
    
    package zsc.czy.array;
    
    public class B {
            //首尾交换
    	public static void main(String[] args) {
    		int a[] = new int[5];
    		for(int i = 0;i<a.length;i++){
    			a[i] = (int) (Math.random() * 100);
    			System.out.println(a[i]);
    		}
    		System.out.println();
    		int temp =0;
    		for (int i = 0; i < a.length/2; i++) {
    			 temp = a[i];
    			 a[i]= a[a.length-1-i];
    			 a[a.length-1-i] = temp;
    		}
    		for(int i :a){
    			System.out.println(i);
    		}
    	}
    
    }
    
    

    选择排序

    一开始我的错误写法

    package zsc.czy.arraySort;
    
    public class QuickSort {
    /**
     *我第一次写,是这样的,是错误的,但还不知道错在哪里
     * 
     * @param args
     */
    	public static void main(String[] args) {
    		int a[] = new int[5];
    		for (int i = 0; i < a.length; i++) {
    			a[i] = (int) (Math.random() * 100);
    			System.out.println(a[i]);
    		}
    		System.out.println();
    		int temp=0;
    		for (int i = 0; i < a.length-1; i++) {
    			for(int j = i;j<a.length-1;j++){
    				if(a[j]>a[j+1]){
    					temp = a[j];
    					a[j]=a[j+1];
    					a[j+1]=temp;
    				}
    			}
    		}
    		for(int i:a){
    			System.out.println(i);
    		}
    	}
    
    }
    

    正确做法

    package zsc.czy.arraySort;
    
    public class QuickSort {
    /**
     * 选择法排序的思路: 
    把第一位和其他所有的进行比较,只要比第一位小的,就换到第一个位置来 
    比较完后,第一位就是最小的 
    然后再从第二位和剩余的其他所有进行比较,只要比第二位小,就换到第二个位置来 
    比较完后,第二位就是第二小的 
    以此类推
     * 
     * @param args
     */
    	public static void main(String[] args) {
    		int a[] = new int[5];
    		for (int i = 0; i < a.length; i++) {
    			a[i] = (int) (Math.random() * 100);
    			System.out.println(a[i]);
    		}
    		System.out.println();
    		int temp=0;
    		for (int i = 0; i < a.length-1; i++) {
    			for(int j = i+1;j<a.length;j++){
    				if(a[j]<a[i]){
    					temp = a[i];
    					a[i]=a[j];
    					a[j]=temp;
    				}
    			}
    		}
    		for(int i:a){
    			System.out.println(i);
    		}
    	}
    
    }
    

    冒泡排序

    package zsc.czy.arraySort;
    
    public class BubbleSort {
    /*
     * 冒泡法排序的思路: 
     * 第一步:从第一位开始,把相邻两位进行比较 
     * 如果发现前面的比后面的大,就把大的数据交换在后面,循环比较完毕后,最后一位就是最大的
     * 第二步: 再来一次,只不过不用比较最后一位 
     * 以此类推
     */
    	public static void main(String[] args) {
    		int a[] = new int[5];
    		for (int i = 0; i < a.length; i++) {
    			a[i] = (int) (Math.random() * 100);
    			System.out.println(a[i]);
    		}
    		System.out.println();
    		for(int i=0;i<a.length-1;i++){
    			for(int j=0;j<a.length-i-1;j++){
    				if(a[j]>a[j+1]){
    					int temp = a[j];
    					a[j]=a[j+1];
    					a[j+1]=temp;
    				}
    			}
    		}
    		for(int i :a){
    			System.out.println(i);
    		}
    	}
    
    }
    
    

    合并数组

    题目

    首先准备两个数组,他俩的长度是5-10之间的随机数,并使用随机数初始化这两个数组
    然后准备第三个数组,第三个数组的长度是前两个的和
    通过System.arraycopy 把前两个数组合并到第三个数组中

    package zsc.czy.HebingArray;
    
    public class HeBing {
    	public static void main(String[] args) {
    		int a[] = new int[5];
    		int b[] = new int[5];
    		for (int i = 0; i < a.length; i++) {
    			a[i] = (int) (Math.random() * 100);
    			b[i] = (int) (Math.random() * 100);
    
    			System.out.println(a[i]);
    			
    		}
    		System.out.println();
    		for(int bb :b){
    			System.out.println(bb);
    		}
    		System.out.println();
    		int c[] = new int[a.length+b.length];
    		System.arraycopy(a, 0, c, 0, a.length);
    		System.arraycopy(b, 0, c, a.length, b.length);  ///从下表c[5]开始
    		for(int cc :c){
    			System.out.println(cc);
    		}
    	}
    }
    
    

    二维数组

    定义一个5X5的二维数组。 然后使用随机数填充该二维数组。
    找出这个二维数组里,最大的那个值,并打印出其二维坐标

    package zsc.czy.erWeiArray;
    
    public class A {
    
    	public static void main(String[] args) {
    		int a[][] = new int[5][5];
    		
    		for(int i=0;i<a.length;i++){
    			for(int j=0;j<a[i].length;j++){
    				a[i][j]= (int)(Math.random()*100);
    			}
    		}
    		
    		  // 打印这个数组的内容:
            for (int[] row : a) {
                for (int each : row) {
                    System.out.print(each + "	");
                }
                System.out.println();
            }
            
    		int max=a[0][0];
    		int temp;
    		for(int i=0;i<a.length;i++){
    			for(int j=0;j<5;j++){
    				if(a[i][j]>max){
    					max = a[i][j];
    				}
    			}
    			
    		}
    		System.out.println(max);
    	}
    
    }
    
    

    二位数组排序

    首先定义一个5X8的二维数组,然后使用随机数填充满。
    借助Arrays的方法对二维数组进行排序。

    参考思路:
    先把二维数组使用System.arraycopy进行数组复制到一个一维数组
    然后使用sort进行排序
    最后再复制回到二维数组。

    package zsc.czy.ArrayUtil;
    
    import java.util.Arrays;
    
    public class ErWeiArraySort {
    
    	public static void main(String[] args) {
    		int a[][] = new int[5][8];
    		for (int i = 0; i < a.length; i++) {
    			for (int j = 0; j < a[i].length; j++) {
    				a[i][j] = (int) (Math.random() * 100);
    			}
    		}
    		// System.out.println(Arrays.toString(a));
    		for (int aa[] : a) {
    			for (int aaa : aa) {
    				System.out.print(aaa + "	");
    			}
    			System.out.println();
    		}
    
    		int newArray[] = new int[a.length * a[0].length];
    //		for (int row[] : a) {
    //			System.arraycopy(row, 0, newArray, 0, row.length);
    //			System.out.println(Arrays.toString(newArray));
    //		}
    		for(int i=0;i<a.length;i++){
    			//包里不包外
    		System.arraycopy(a[i], 0, newArray, a[i].length*i, a[i].length);
    		}
    		System.out.println(Arrays.toString(newArray));
    		Arrays.sort(newArray);
    		System.out.println();
    		System.out.println(Arrays.toString(newArray));
    	}
    
    }
    
    
  • 相关阅读:
    普通类型(Trivial Type)和标准布局类型(Standard-layout Type)以及POD类型
    设计模式
    网络相关的学习和命令总结
    sheel命令学习和工作总结。
    Makefile的学习
    [UI基础][实现]九宫格之应用程序管理
    [嵌入式][分享][交流]发布一个消息地图的模块
    [UI基础][不会说话的汤姆猫]
    [UI基础][QQ登陆界面]
    volatile的陷阱
  • 原文地址:https://www.cnblogs.com/czy16/p/8933510.html
Copyright © 2011-2022 走看看