天天看點

Java中 數組作業的練習

 Java中 數組的練習

   結合上一篇的數組複習,本篇給出一些數組的練習

執行個體代碼一:

package cn.com.blog.array;

import java.util.Scanner;

/*
 * 1.已知一個數組,求數組中心元素。
2.已知一個數組,求所有元素和。
3.已知一個數組,輸出所有奇數下标元素。
4.已知一個數組,輸出所有元素中,值為奇數的。
5.已知一個數組,将所有元素乘二。
6.已知一個數組,将所有元素加到第一個元素中。
7.已知一個數組A,将奇數位置元素存到B數組中,偶數元素存到C數組中。
 */
public class ArrayExe01 {
   public static void main(String[] args) {
	  int array [] = {2,432,5221,235,2,5352,1,53,5,3,5364,2,2,63,3533,2,53,532,532};
	  Scanner scan = new Scanner(System.in);
	  int num = scan.nextInt();
	  if(num == 1){
		  System.out.println(ArrayExe01.getMidInfo(array));
	  }if(num == 2){
		  System.out.println(ArrayExe01.getSum(array));
	  }if(num == 3){
		  ArrayExe01.print(array);
	  }if(num == 4){
		  ArrayExe01.print1(array);
	  }if(num == 5){
		  System.out.println(ArrayExe01.getArray(array));
	  }if(num == 6){
		  System.out.println(ArrayExe01.getFirstSum(array));
	  }if(num == 7){
		  ArrayExe01.newArray(array);
	  }
   }
   
   // 1.當長度為偶數的時候取的是後面那個
   public  static int getMidInfo(int array[]){
	   int length = array.length;
	   return array[length / 2];
   }
   // 2.數組為int類型
   public static int getSum(int array[]){
	   int sum= 0;
	   for(int i =0 ;i< array.length;i++){
		   sum += array[i];
	   }
	   return sum;
   }
   // 3.循環步進為2即可
   public static void print(int array[]){
	   for (int i = 0; i < array.length; i+=2) {
	       System.out.println(array[i]);
	    }
   }
   // 4.數組為int類型
   public static void print1(int array[]){
	   for (int i = 0; i < array.length; i++) {
	       if(array[i] % 2 == 0){
	    	   System.out.println(array[i]);
	       }
	    }
   }
   
   // 5.将參數的副本作為傳回值再次回傳
   public static int[]  getArray(int array[]){
	   for(int i =0 ;i < array.length;i++){
		   array[i] = array[i] * 2;
	   }
	   return array;
   }
   // 6.傳回第一個元素的值
   public static int getFirstSum(int array[]){
	   int one = array[0];
	   for(int i = 1 ;i< array.length;i++){
		   one = one + array[i];
	   }
	   return one;
   }
   // 7.隻作為輸出
   public static void newArray(int array[]){
	   for(int i =0 ;i<array.length;i++){
		  if(array[i] % 2==0){
			  System.out.println("偶數: "+array[i]);
		  }else{
			  System.out.println("奇數: "+array[i]);
		  }
	   }
   } 
}
           

執行個體代碼二:

package cn.com.blog.array;

import java.util.Random;

/**
 * 
 * @author fcs
 * 2014年9月12日
 * ArrayExe02
 * 說明:
 * 1.把A數組的前5個元素複制到B數組中。
2.把1----36分别放入數組中,計算數組對角元素之和。6 * 6的數組
3.判斷一個數組是否對稱。
4.有一個長度是10的數組,數組内有10個不重複的數字,要求按從大到小排序。
5.有一個長度是10的數組,要求删除某一個位置的元素,後邊元素前置。
6.有一個長度是10的數組,按遞增排列,使用者輸入一個數,插入适當位置。
7.有一個長度是10的數組,數組内有10個人名,要求去掉重複的
8.把A數組的第三到第六位之間的元素删除。
9.已知A數組,B數組,定義一個數組C,要求C包含A,B數組中的資料(無重複值)。
 */
public class ArrayExe02 {
	
    public static void main(String[] args) {
    	int array [] = {2,4,1,5,6,7,3,8,9,0};
    	ArrayExe02.sortArray(array);
    	Random rand = new Random();
    	int array1 [] = new int [12];
    	for(int i = 0;i< 10;i++){
    		array1[i] = rand.nextInt(10); 
    	}
    
    	System.out.println("------------------------------------");
    	ArrayExe02.insert(array1, 10);
    	System.out.println("------------------------------------");
    	int array2 [] = new int [10];
    	for(int i =0;i< 10;i++){
    		array2[i] = rand.nextInt(10);
    	}
    	ArrayExe02.deleteRe(array2);
    	
    	ArrayExe02.getSum1(array1, array2);
    	
	}
    //1.注意數組下标越界
    public static int [] newArry(int array[]){
    	int narr [] = new int [5];
    	int length = array.length > 5 ? 5 : array.length;
        for(int i =0 ;i< 5;i++){
        	narr[i] = array[i];
        }
        return narr;
    }
    //2.for循環指派
    public static int getSum(){
    	int array [][] = new int [6][6];
    	int  num = 0;
    	for(int i =0 ;i < 6;i++){
    		for(int j = 0;j < 6;j++ ){
    			array[i][j] = num++;
    		}
    	}
    	int sum = 0;
    	for(int i =0;i < 6;i++){
    		for(int j =0 ;j< 6;j++){
    			if(i==j || i+j == 5){
    				sum += array[i][j];
    			}
    		}
    	}
    	return sum ;
    }
    //3數組的對稱問題
    public static void getMId(){
    	//一維數組
        //二維數組的對稱矩陣 
        int array [] = {1,2,3,4,4,3,2,1};
        boolean b = false;
        for(int i =0 ;i< array.length / 2;i++){
        	if(array[i] != array[array.length - i]){
        	     b = true;
        	}	
        }
        if(b){
        	System.out.println("不是對稱數組");
        }else{
        	System.out.println("是對稱數組");
        }
        boolean b1 = false;
        int array1 [] [] =  {{1,2,3,4},{2,1,4,3},{3,4,1,2},{4,3,2,1}};
        for(int i =0 ;i<4;i++){
        	for(int j = 0;j < i;j++){
        		if(array1[i][j] != array1[j][i]){
        			b1 = true;
        		}
        	}
        }
        if(b1){
        	System.out.println("不是對稱數組");
        }else{
        	System.out.println("是對稱數組");
        }
        //還有一種情況,在此不做讨論。
    }
    //4.數組的排序問題,這裡使用冒泡排序
    public static void sortArray(int array[]){
    	for(int  i =0 ;i < array.length -1;i++){
    		for(int  j = i;j < array.length;j++ ){
    			if(array[i] < array[j]){
    				int temp = array[i];
    				array[i] = array[j];
    				array[j] = temp;
    			}
    		}
    	}
    	for(int i = 0;i< array.length;i++){
    		System.out.println(array[i]);
    	}
    }
    //5.數組的删除操作
    public static void delete(int array[],int index){
    	if(array.length  <=  index){
    		System.out.println("數組下标錯誤。。。。");
    	    return;
    	}
    	int a = array[index];
    	for(int i = index+1;i<array.length;i++){
    		array[i - 1] = array[i];
    	}
    	for(int i =0 ;i< array.length;i++){
    		System.out.println(array[i]);
    	}
    }
    //6.數組的插入
    public static void insert(int array [],int value){
    	array[10] = value;
    	for(int  i =0 ;i < 11;i++){
    		for(int  j = i;j < array.length;j++ ){
    			if(array[i] > array[j]){
    				int temp = array[i];
    				array[i] = array[j];
    				array[j] = temp;
    			}
    		}
    	}
    	for(int i =0 ;i<array.length;i++){
    		System.out.print(array[i]+" , ");
    	}
    }
    
    //7.去除重複元素
    public static void deleteRe(int array [] ){
    	for(int i =0 ;i<array.length;i++){
    		System.out.print(array[i]+" , ");
    	}
    	boolean brr [] = new boolean [array.length];;
    	for(int i =0 ;i < array.length - 1 ;i++ ){
            	for(int j = i+1;j< array.length;j ++){
                    if(array[i] == array[j] && brr[j] != true){
                    	brr[j] = true;
                    }
            	}
    	   }
    	System.out.println("--------------------");
    	for(int i =0 ;i< array.length;i++){
    		if(brr[i] ==false){
    			System.out.print(array[i]+" , ");
    		}
    	}
    }
    
    //8.采用數組的指派
    public static void deleteTo(int array [] ){
    	for(int i =0 ;i<array.length;i++){
    		if(i < 2 && i > 5){
    			System.out.print(array[i]+" , ");
    		}
    		
    	}
    }
    //9.先合并再去除重複元素
    public static void   getSum1(int A [],int B[]){
    	System.out.println("9.先合并再去除重複元素");
    	int array [] = new int [A.length+B.length];
    	for(int i =0;i<array.length;i++){
    		if(i < A.length){
    			array[i] = A[i];
    		}else{
    			array[i] = B[i - A.length];
    		}
    	}
    	 deleteRe(array);
    }
}
           

其實很多不大的項目或者題目中,數組通常會很有幫助,而且簡單易用,這裡隻是列出一些比較淺顯的題目和自己的測試。