Java Array Exercises Summary

Source: Internet
Author: User

2015/07/07//java Array Small exercises/*3. Write a function that calculates the average of an array of integers 4. Customizes an integer array A, reads an integer n, and if n is present in the array, outputs the subscript of n, or 1 if it does not exist. 5. Given an array, the maximum value and the minimum value of 6 in the output array. * Given an array, the order of all elements in this array is reversed. 7. * Complete the bubble sorting algorithm for the array: given an array: int[] a = {1,3,2,7,5}, using bubble sort to sort them from small to large, and then output the results. 8. * Sort the array using the second algorithm note: Each question is encapsulated by a function */package Chp5;public class Arraytest {//three computes the average of an array public void Average (int[] array) {System.out.println ("question 3rd"); int len = array.length;int average = 0;int sum = 0;for (int i = 0;i<len;i++) {sum+=array[i];} Average = Sum/len; System.out.println ("The average of the array is:" +average);} Question fourth: Customize an integer array A, read into an integer npublic void Isexit (int[] array,int num) {System.out.println ("4th"); int len = Array.Length; Boolean BL = false;for (int i = 0;i<len;i++) {if (array[i] = = num) {System.out.println ("The number exists in the array, subscript is:" +i); bl = True;}} if (bl = = False) {System.out.println (-1);}} 5 questions: Find the maximum and minimum values in the array public void Getmaxmin (int[] array) {SYSTEM.OUT.PRINTLN ("question 5th"); int len = array.length;int temp;for (int i = 0;i<len;i++) {for (int j = 0;j<len-i-1;j++) {if (array[j]>array[j+1]) {temp = ARRAY[J];ARRAY[J] = array[j+1];array[j+1] = temp;}}} System.out.println ("The maximum value of the array is:" +array[len-1]); System.out.println ("The minimum value of the array is:" +array[0]);} Question 6th: Given an array, the order of all elements in this array is reversed. public void Updownlocation (int[] array) {System.out.println ("question 6th"); int len = Array.length;int temp = 0;for (int i = 0;i<l en;i++) {temp = array[len-1];array[len-1] = array[i];array[i] = temp;len--;}} public void PrintArray (int[] array) {int len = array.length;for (int i = 0;i<len;i++) { System.out.print (array[i]+ "");} System.out.println ();} Question 7th: Sort from small to large, then output public void Bubblesort (int[] array) {System.out.println ("7th");        int len = Array.length;int temp; for (int i = 0;i<len;i++) {for (int j = 0;j<len-i-1;j++) {if (array[j]&gt                    ; array[j+1]) {temp = Array[j];                    ARRAY[J] = array[j+1];                ARRAY[J+1] = temp; }}}}//question 8th: with the secondThe algorithm sorts the array public void Mychoicesort (int array[]) {System.out.println ("question 8th"); int len = Array.Length;        int temp; for (int i = 0;i<len;i++) {for (int j = i+1;j<len;j++) {if (array[i]>a                    RRAY[J]) {temp = Array[j];                    ARRAY[J] = array[j+1];                ARRAY[J+1] = temp; }}}}public static void Main (string[] args) {arraytest Myarray = new Arraytest ();////int[] Array ={ 8,4,6,2,2,6,4,8}; Myarray.average (array);////myarray.isexit (array, 5);///myarray.getmaxmin (array);///myarray.updownlocation (array ); Myarray.printarray (array);///myarray.bubblesort (array); Myarray.printarray (array);///myarray.mychoicesort (array); Myarray.printarray (array);}}

  

Java Array Exercises Summary

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.