Arrays in Java (Exercise 4)

preview_player
Показать описание
Java Programming: Programming Question on Arrays in Java Programming
Topics Discussed:
1. Writing a program that displays the maximum and minimum elements of an array.

Music:
Axol x Alex Skrindo - You [NCS Release]

#JavaByNeso #JavaProgramming #ArraysInJava
Рекомендации по теме
Комментарии
Автор

we can sort the array and immediately set the minimum to the first element and the maximum as the last element.
it is much simpler that way.

vedroxplayz
Автор

Every day, I thank myself for discovering you! You're the best!

a.human.
Автор

yayyy I solved this problem but used the ff from your past lesson:

Arrays.sort(numbers);
System.out.println("Min = " + numbers[0] );
System.out.println("Max = " + numbers[numbers.length - 1]);

I started this course from the first video and didn't miss a single episode since then. I'm completing it this week. xD

smiley-wukn
Автор

Sir you are such an asset!
But we need more videos on
-Creating objects
-How do we access the properties from different classes
-Execptions
Etc

laxmi_miskin
Автор

Thanks much Neso academy your making programing so easy for us

shadiahlawharts
Автор

public static void main(String[] args) {
int max, min;
int[] array = {1, 1, 2, 2, 3, 3, 8, 8, 7, 7, 7};
max = array[0];
min = array[0];
for (int n = 1; n < array.length; n++){
if (max < array[n])
max = array[n];
if(min > array[n])
max = array[n];
}
System.out.println(max + " is max");
System.out.println(min + " is min");



alternatively with sort()

public static void main(String[] args) {
int max, min;
int[] array = {1, 1, 2, 2, 3, 3, 8, 8, 7, 7, 7};

Arrays.sort(array);

System.out.println("max is " + array[array.length -1]);
System.out.println("min is " + array[0]);
}

jejewite
Автор

Thanks for the video sir.
Waiting for the next video.
#LoveFromChandigarh

rishurana
Автор

import java.util.Arrays;

public class Main {

public static void main(String[] args) {

int[] nums = {8, 3, 7, 5};

//via loop + comparison

int min_1 = nums[0];
int max_1 = nums[0];

for(int i: nums) {
if(i<min_1)
min_1 = i;
if(i>max_1)
max_1 = i;
}

System.out.println("Min: " + min_1 + "; Max: " + max_1);

//via sort method

int[] nums2 = {3, -2, 5, 1, -1, 9};
Arrays.sort(nums2);

int min_2 = nums2[0];
int max_2 = nums2[nums.length -1];

System.out.println("Min: " + min_2 + "; Max: " + max_2);

}

}

alterjrorbino
Автор

public static void main(String[] args) {

int[] numbers = {3, 2, 1, 5, 4, 54, -2};
System.out.println("max: " + max(numbers));
System.out.println("min: " + min(numbers));
}
public static int max(int[] numbers){
Arrays.sort(numbers);
return numbers[numbers.length-1];
}
public static int min(int[] numbers){
Arrays.sort(numbers);
return numbers[0];
}

marcinnalborczyk
Автор

public class methods {
public static void main(String[ ] args) {

Scanner num=new Scanner(System.in);
int n=num.nextInt();

int[ ] array= new int [n];
int min=array[0];
int max=array[0];
for (int i=0;i<array.length;i++)
{
array[i]=num.nextInt();
if (array[i] > max)
{
max=array[i];
}
if(array[i]< min)
{
min=array[i];
}
}
"+"min"+'='+min);


}
}

thivagarmurugan
Автор

public static void main(String[] args) {
int[] minmax = {3, 2, 1, 5, 4};

int max = minmax[0];
int min = minmax[0];

for(int i = 0; i < minmax.length; i++){
if(minmax[i]>max)
max = minmax[i];
else if (minmax[i]<min)
min = minmax[i];
}
System.out.println("min = "+min);
System.out.println("max = "+max);

}

acunit
Автор

This is the shortcut way:




int[]a= {1, 2, 2, 3, 2, 5, 6, 7};
Arrays.parallelSort(a);
System.out.print("mininmum= "+a[0]+" maximum= "+a[7]);

ishanpatni
Автор

public static void main(String[] args) {

int[] arr = {3, 2, 1, 5, 4};
int maxValue = arr[0];
int minValue = arr[0];

for (int i = 1; i < arr.length -1; i++) {
if (arr[i] > maxValue) {
maxValue = arr[i];
}
if (arr[i] < minValue) {
minValue = arr[i];
}
}

System.out.println("min = " + minValue + " max = " + maxValue);
}

kevinbanan
Автор

import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int[] numbers={2, 5, 4, 7, 6, 9};
Arrays.sort(numbers);

System.out.println(" min = "+numbers[0]+" max
}
}

srisaisrinivasvemula
Автор

import java.util.Arrays;
public class MaxandMin {
public static void main(String[] args) {
int[] numbers = {3, 2, 1, 5, 4};
Arrays.sort(numbers);
int min, max;
for(int i=0; i<numbers.length; i++){
min =numbers[i=0];
max =numbers[i=numbers.length-1];

System.out.println("min= " + min + " max= " + max);
}

}
}

GAMBITisLive
Автор

What if Numbers are equal.
This code is not universal.

mjain
Автор

import java.util.Arrays;
import java.util.Scanner;

public class DanceLoop {

public static void main(String[] args) {
int[] numbers = { 1, 2, -3, 2, 7 };


int max = getMax(numbers);
System.out.println("max = " + max);

int min = getMin(numbers);
System.out.println("min = " + min);



}

private static int getMin(int[] numbers) {
int min = numbers[0];
for(int i=0; i < numbers.length; i++)
if( min > numbers[i])
min = numbers[i];

return min;
}

public static int getMax(int[] numbers) {
int max = numbers[0];
for(int i=0; i < numbers.length; i++)
if( max < numbers[i])
max = numbers[i];

return max;
}
}

nayanasisil
Автор

import java.util.*;

class Check {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = input.nextInt();

int[] numbers = new int[size];
fillArrayOfIntegers(numbers);


System.out.println("Maximum of the array is: " + maxOfAnArray(numbers));
System.out.println("Minimum of the array is: " + minOfAnArray(numbers));
}

private static void fillArrayOfIntegers(int[] numbers) {
Scanner input = new Scanner(System.in);

for(int i = 0; i < numbers.length; i++) {
System.out.print("Enter an integer: ");
numbers[i] = input.nextInt();
}
}

private static int maxOfAnArray(int[] numbers) {
int max = 0;
for(int i = 0; i < numbers.length; i++)
if(numbers[i] > max)
max = numbers[i];

return max;
}

private static int minOfAnArray(int[] numbers) {
int min = maxOfAnArray(numbers);
for(int i = 0; i < numbers.length; i++)
if(numbers[i] < min)
min = numbers[i];

return min;
}
}

suswithcherry