Exercises for array jobs in Java __java

Source: Internet
Author: User
Tags rand

Exercises for arrays in Java

In conjunction with the previous array review, this article gives some examples of array exercises

Instance code one:

Package Cn.com.blog.array;

Import Java.util.Scanner;
* * 1. An array is known, and the center element of the array is evaluated.
2. An array is known, and all elements and.
3. An array is known to output all odd subscript elements.
4. An array is known to output all elements with an odd value.
5. An array is known to multiply all elements by two.
6. An array is known to add all elements to the first element.
 7. Given an array a, the odd position element is stored in the B array, and even elements are stored in the C array. */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. When the length is even, take the following public static int getmidinfo (int array[]) {int length = Array.Length; Return ARRAY[LENGTH/2];
	   //2. Array is int type public static int getsum (int array[]) {int sum= 0;
	   for (int i =0;i< array.length;i++) {sum = Array[i];
   return sum; //3. Circular stepping is 2 to public static void print (int array[]) {for (int i = 0; i < Array.Length; i+=2) {Syst
	    Em.out.println (Array[i]);
	       }//4. Array is int type 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 to return a copy of the parameter as the returned value again public static int[] GetArray (int array[]) {for (int i =0; i < Array.le
	   ngth;i++) {Array[i] = array[i] * 2;
   } return array;
	   }//6. Returns the value of the first element 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. Only as output public static void NewArray (int array[]) {for (int i =0; i<array.length;i++) {if (Array[i]% 2==0) {System.out.println ("even:" +array[i]);
		  }else{System.out.println ("Odd:" +array[i]);
 }
	   }
   } 
}

Instance code two:

Package Cn.com.blog.array;

Import Java.util.Random;
/** * * @author FCS * September 12, 2014 * ARRAYEXE02 * Description: * 1. Copy the first 5 elements of a array into the B array. 2. Put 1----36 into the array separately, and compute the sum of the diagonal elements of the array.
6 * 6 of the array 3. Determine whether an array is symmetric.
4. There is an array with a length of 10, and there are 10 distinct digits in the array, which require sorting from large to small.
5. There is an array of length 10, which requires the deletion of elements from a certain position, and the elements that are placed behind them.
6. There is an array of length 10, in ascending order, the user enters a number and inserts the appropriate position.
7. There is an array with a length of 10, with 10 names in the array, requiring that the duplicate 8 be removed. Deletes the element between the third and sixth digits of a array.
 9. Given an array of arrays, b arrays, define an array C, which requires C to contain the data in the A,B array (no duplicate values).
    	* * 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. Note that the array subscript is out of bounds 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 circular Assignment 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++;
    	an 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; Symmetric problems with//3 arrays public static void Getmid () {//one-dimensional array///two-dimensional array of symmetric matrices 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 ("Not a symmetric array");
        }else{System.out.println ("is a symmetric array");
        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 ("Not a symmetric array");
        }else{System.out.println ("is a symmetric array");
    //There is a situation where there is no discussion. 
    		//4. Array sorting problem, where you use bubble sort 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. Array Delete operations public Static void Delete (int array[],int index) {if (Array.Length <= index) {System.out.println ("Array subscript error ....)
    	    ");
    	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]);
    	Insert public static void insert (int array [],int value) {array[10] = value//6. Array.
    				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. Remove the repeating element 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] &A
                    mp;& 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. Using the assignment of an array 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. Merge and then remove the repeating element public static void getSum1 (int A [],int b[]) {System.out.println ("9.)
    	And then remove the repeating element ");
    	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);
 }
}

In fact, a lot of small projects or topics, the array is usually helpful, and easy to use, here is just a list of some of the more obvious topics and their own tests.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.