Normal method:
public class Max { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5,10,11,15,100,-1,-4.5}; //Define one-dimensional array double num = myList[0]; //0 is the first array subscript for (int i = 0; i < myList.length; i++) { //Start looping for one-dimensional array if (myList[i] > num) { //Loop to judge array elements num = myList[i]; } //Assign to num, and then loop again} System.out.println("Maximum value is" + num); //Break out of the loop and output the result} }Triple operator:
public class Max { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5,10,11,15,1,-1,-4.2}; //Define one-dimensional array double num = myList[0]; //0 is the first array subscript for (int i = 0; i < myList.length; i++){ //Start loop one-dimensional array num=(myList[i] < num?num: myList[i]); //Trial operator, see the comments for details} System.out.println("Maximum value is" + num); //Break out of the loop and output the result} }Note: Is the syntax of the ternary operator condition? Result 1: Result 2; Advantages are concise in code, but disadvantages are poor readability
Example: int a,b,c;
a=2;b=3;
c=a>b?100:200;
Semantics: If a>b,c=100;a<b,c=200
General functions/methods:
public class Max { double[] myList = {1.9, 2.9, 3.4, 100,3.5,10,11,12,13,-1}; double num = myList[0]; void getValue(){ //Create general method for (int i = 0; i < myList.length; i++) { num=(myList[i] < num?num: myList[i]);//Trial operator} System.out.println("Maximum value is" + num); } public static void main(String args[]){ Max max=new Max(); //Create the object max.getValue(); //Calendar general methods through the object}}Note: Method 3 requires the use of object-oriented ideas