current position：Home>Design a mathutils Java class file
Design a mathutils Java class file
20220202 20:59:31 【CSDN Q & A】
(1) Design static functions to maximize getMax , Pass in 1 individual int Array type parameter ,(2) Design static function to find the minimum value getMin , Pass in 1 individual int Array type parameter , return int Type data (3) Design static function inversion reverse, Pass in 1 individual int Array type parameter , Invert the data in the array and return the array to (4) Design static functions getIndex Find the subscript position corresponding to the data in the current array , Pass in 1 individual int Array type parameters and int Type data , Return the index of the data in the array to
Refer to the answer 1：
public class MathUtils { /** * (1) Design static functions to maximize getMax Pass in 1 individual int Array type parameter , return int Type data such as [12345] return 5 (10 branch ) * @param numbers * @return */ public static int getMax(int[] numbers){ int rs = 0; if (numbers==null) return rs; for (int i = 0; i < numbers.length; i++) { if (i==0) rs= Math.max(numbers[i],numbers[i+1]); else rs= Math.max(numbers[i],rs); } return rs; } /** * (2) Design static function to find the minimum value getMin , Pass in 1 individual int Array type parameter , return int Type data , Such as [1,2,3,4,5] return 1 (10 branch ) * @param numbers * @return */ public static int getMin(int[] numbers){ int rs = 0; if (numbers==null) return rs; for (int i = 0; i < numbers.length; i++) { if (i==0) rs= Math.min(numbers[i],numbers[i+1]); else rs= Math.min(numbers[i],rs); } return rs; } /** * (3) Design static function inversion reverse, Pass in 1 individual int Array type parameter , Invert the data in the array and return the array to , Such as [1,2,3,4,5] , return [5,4,3,2,1] (10 branch ) * @param numbers numbers * @return */ public static int[] doReverse(int[] numbers){ if (numbers==null) return new int[]{}; int[] rs = new int[numbers.length]; int temp=0; for (int i = numbers.length1; i >= 0; i) {
rs[temp] = numbers[i]; temp++; } return rs; } /** * (4) Design static functions getIndex Find the subscript position corresponding to the data in the current array , Pass in 1 individual int Array type parameters and int Type data , Return the index of the data in the array to , Such as , stay [11,22,33,44,55] lookup 55 , return Subscript 4. (10 branch ) */ public static int getIndex(int[] numbers,int number){ for (int i = 0; i < numbers.length; i++) { if (numbers[i] == number){ return i; } } return 1; } /** * (5) Design static functions to exchange the contents of the array swap, Pass in 1 individual int Array type parameter , No return value , Such as incoming [88,99] , call swap The contents of the last array are [99,88] (10 branch ) */ public static void swap(int[] numbers){ int temp; for (int i = 0; i < numbers.length1; i++) { temp = numbers[i]; numbers[i] = numbers[i+1]; numbers[i+1] = temp; } } /** * (6) Design static function to count the sum of data in the array getSum, Pass in 1 individual int Array type parameter , Returns the sum of the data in the array , Such as [1,2,3,4,5] return 15 (10 branch ) */ public static int getSum(int[] numbers){ int sum = 0; for (int number : numbers) { sum += number; } return sum; } /** * (7) Design static function to count the average value of data in the array getAvg, Pass in 1 individual int Array type parameter , Returns the average value of the data in the array , Such as [1,2,3,4,5] return 3 (10 branch ) */ public static float getAvg(int[] numbers) { int sum = 0; for (int number : numbers) { sum += number; } return (float)sum/numbers.length; } /** *(8) Extended function realization : Design static function sorting sort Method , Pass in 1 individual int Array type parameter , No return value Such as [ 10,2,8,5,7] , call sort after [2,5,7,8,10] (20 branch ) */ public static int[] doSort(int[] numbers) { Arrays.sort(numbers); return numbers; } public static void main(String[] args) { int[] a = new int[]{
11,20,2,6,7}; //1 System.out.println(getMax(a)); //2 System.out.println(getMin(a)); //3 System.out.println(Arrays.toString(doReverse(a))); //4 System.out.println(getIndex(a,11)); //5 int[] b = new int[]{
11,20}; System.out.println(Arrays.toString(b)); //6 System.out.println(getSum(a)); //7 System.out.println(getAvg(a)); //8 System.out.println(Arrays.toString(doSort(a))); }}
Refer to the answer 2：
copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202022059307446.html
The sidebar is recommended
 Oracle12c invalid create index option
 If the folder of pod in k8s is mounted with data volume (persistent), the pod cannot be started. How to solve it?
 I want to ask what's wrong!
 Problems related to C language selection structure if else
 KeyError: 'ConvWS is already registered in conv layer'
 On the problem of C language: prime is a number that has no other factors except 1 and itself
 Dev C + + automatically generates an ostream after compilation?
 C language: input a character from the keyboard to judge whether it is an English letter.
 Python uses function to find factorial of 05 points
 C language wants to insert new data into an array structure with only three elements, but on the one hand, it is garbled. On the other hand, it cannot judge the value of N and return it
guess what you like

The computer disk space is lost, and the actual used space is inconsistent with the displayed space

Is there a price for debugging java code

Read in an integer m and calculate the value of the following formula

About idea, it doesn't work

C language choice sorting primary questions, the number after the first sorting exchange position becomes 0

Programming with C language to find the Y power of X

'request 'object has no attribute' type 'error

There is no prompt for idea to write Python code

On the test of Fibonacci sequence

I set div to 100% in CSS, but it is not 100%. How should I solve it
Random recommended
 On Vue select
 How to configure the post request size in the tomcat7 plugin of Maven in idea
 Java random numbers are sorted in the array
 The problem of is and memory address in Python
 Save min, ask for an answer, ask for an answer.
 New Ajax options {onsuccess} migration core in MVC
 If you have any questions, the form will automatically crawl and fill in the information of another form
 Iotdc3 drivers cannot be started. What is the problem and whether the configuration needs to be changed
 How to lay a good foundation for C language?
 Sqlstate [42s02]: base table or view not found: 1146 table 'com system_ node' doesn't exist
 The edge search function cannot be used
 Recursive program running error, is there a maximum limit for recursion?
 Why does the integer 1000 become 1 after excel & merges the cell contents,
 Openstack cloud platform APL operation and maintenance development
 Output the node of X in the binary tree
 Questions about Java: kettle related
 PHP class members, construction and encapsulation
 Vue develops Gaode map, clicks the map to obtain the longitude and latitude, but fails to obtain the information with inverse geocoding, and returns invalid_ USER_ SCODE
 *PTR is a value and a is a pointer. How can they be equal? Shouldn't it be * PTR = & A?
 How to solve this problem?
 Specify local time to run code?
 How is the notification message implemented in Android studio app?
 Why is a < B short circuited?
 Usually in face recognition, what operations do we need to do (such as division)? How many times does each operation need to be done? What is the data specification of the operation?
 Firefox browser of win10 system is not connected to the Internet
 Must JNA load DLL be an absolute path? The relative path will report an error every time it is loaded lang.UnsatisfiedLinkError
 Server error in '/ CMS parameters' application.
 The MAC installs windows virtual machine and QT reports an error
 On string array as function parameter
 How to figure out HTML5
 What is the specific idea to realize the comment function
 The number of occurrences of certain fields in SQL query
 Programming with eclipse Java
 Get function usage, how to use, high adoption rate
 MNIST dataset with KNN algorithm
 The socalled palindrome number is the same number from left to right and from right to left. For example, 121 is a palindrome number. Write a program to find the number of palindromes that can be divided by 3 in the range of 100m. (can anyone see where
 Solve the problem of binding this point in anti chattering throttling
 Find the factorial of 05 by function
 JSP get the current URL parameter
 Why did you output more