Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8707787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:57:19+00:00 2026-06-13T03:57:19+00:00

I was trying to write a simple max and min method, as I wrote

  • 0

I was trying to write a simple max and min method, as I wrote it I just cant help feeling it shouldn’t be this complicated….maybe Im wrong?
My maximum code works like this, excuse my poor pseudo code:

Fill an array with 10 random numbers.
Create a max variable initialised to 0, because 0 is the lowest max.
Compare each element against the max
If the element is greater then max, replace the value of max with the element in question

I don’t like the fact I have to initialise max to 0, I feel there might be a better way then this?

My min code works similar except I:
Compare my min is lower then the array element.
If the element is lower replace min.

What I really don’t like about this is I have to initialise my min to the maximum random number, in this case 50.

My questions are:
Is there a better way to do this?
Is there a more efficient way to write this code?

import java.util.Random;

public class Main {

public static void main(String[] args) {

    //Declare min and max
    int max=0;
    int min;
    //Array of 10 spaces
    int[] ar=new int[10];
    //fill an array with random numbers between 0 and 50

    for(int i=0;i<10;i++)
    {
        ar[i]=new Random().nextInt(50);
    }

    //Test max algorithm
    //loop trough elements in array
    for(int i=0;i<10;i++)
    {
        //max is set to 0, there should always be a maximum of 0
        //If there isnt 0 will be the maximum

        //If element is greater then max
        //replace max with that element
        if(ar[i]>max)
        {
            max=ar[i];
        }
    }
    System.out.println("The max is "+ max);

    //Test min
    //Initialising min to maximum Random number possible?
    min=50;
    for(int i=0;i<10;i++)
    {
        if(ar[i]<min){
            min=ar[i];
        }
    }
    System.out.println("The min is "+min);


}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:57:20+00:00Added an answer on June 13, 2026 at 3:57 am

    Ok, while others were already posting answers, I have taken the time to edit your code into something I think would be more usable.

    1. Make static methods. Those can be reused.
    2. Use an ellipsis (…) because you then can either call the methods on array arguments like in your code, but also with a variable number of arguments as min(5,3,8,4,1).
    3. Initialize with the smallest/biggest possible number the data type provides
    4. To check that your code works, you have to print out the items in the array first, since when you don’t know what’s in it, there’s no way to tell the result is correct.
    5. Base your code on the existing methods in the standard library because these are known to be thoroughly tested and work efficiently (I know, min/max looks like a too trivial example).
    6. I wouldn’t bother too much about performance unless you really can show there is a performance problem in your code. Priority should be more like 1st correctness, 2nd readability/maintainability, 3rd performance.

    Most of this has been already mentioned by others, but anyway, here’s the code:

    import java.util.Random;
    
    public class MinMax {
    
        public static int min(int... args) {
            int m = Integer.MAX_VALUE;
            for (int a : args) {
                m = Math.min(m, a);
            }
            return m;
        }
    
        public static int max(int... args) {
            int m = Integer.MIN_VALUE;
            for (int a : args) {
                m = Math.max(m, a);
            }
            return m;
        }
    
        public static void main(String[] args) {
    
            // fill an array with random numbers between 0 and 50
            int[] ar = new int[10];
            for (int i = 0; i < 10; i++)
            {
                ar[i] = new Random().nextInt(50);
                System.out.println(ar[i]);
            }
    
            int maxValue = max(ar);
            int minValue = min(ar);
    
            System.out.println("The max is " + maxValue);
            System.out.println("The min is " + minValue);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple program that will receive a string of max
I'm trying to write a simple high-low program that will output like this: $
I'm trying to write a simple geometry shader what just passes through vertices before
I'm trying to write simple proxy server for some purpose. In it I use
I am trying to write header file. I can write simple headers like add(int
I trying to write a simple function to call unmanaged code from managed code.
I am trying to write some simple code which will read a text file
I am trying to write a simple application to interact with NFC tags, but
I'm trying to write a simple game based on selecting the correct answer (A
I'm trying to write a simple code in Java to connect to a memcache

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.