📌  相关文章
📜  如何从Java中的向量中找到最小或最大元素?

📅  最后修改于: 2022-05-13 01:54:53.641000             🧑  作者: Mango

如何从Java中的向量中找到最小或最大元素?

Collection是Java提供的框架,它提供了一种架构来存储一组对象。一个这样的集合是 Vector()。有很多方法可以找到 Vector 中的最小和最大元素。这些方法讨论如下:

方法:

  1. 使用Collection.min()Collection.max()方法。
  2. 使用迭代向量元素设置最小值-最大值。

方法一:使用 Collections.min() 和 Collections.max() 方法

Collections 包提供了一种在集合中查找最小值和最大值的静态方法。这些方法是 Collections.max() 和 Collections.min()。

  • Collections.max() 查找最大元素
  • Collections.min() 查找集合中的最小元素。

例子

Java
// Java Program to Find minimum and maximum elements
// from a Vector
 
// Importing all classes of
// java.util package
import java.util.*;
 
// Class to find min and max
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating(defining) a  Vector
        Vector V = new Vector();
 
        // Add elements in Vector
        // Custom inouts
        V.add(1);
        V.add(2);
        V.add(3);
        V.add(4);
        V.add(5);
 
        // Printing all elements of vector
        System.out.println("Vector elements : " + V);
        // Using inbuilt function to
 
        // find minimum number
        // using Collection.min() method
        int minNumber = Collections.min(V);
 
        // find maximum number
        // using Collection.max() method
        int maxNumber = Collections.max(V);
 
        // Print max element of the vector
        System.out.println("Maximum Number in Vector is : "
                           + maxNumber);
 
        // Print min element of th vector
        System.out.println("Minimum Number in Vector is : "
                           + minNumber);
    }
}


Java
// Java Program to Find minimum and maximum elements
// from a Vector
 
// Importing all classes of
// java.util package
import java.util.*;
// Importing Integer Wrapper class for
// primitive data type
import java.lang.Integer;
 
// Clas
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Assign maximum value to minValue Variable
        int minValue = Integer.MAX_VALUE;
 
        // Assign minimum value to maxValue Variable
        int maxValue = Integer.MIN_VALUE;
 
        // Creating a Vector
        Vector V = new Vector();
 
        // Adding elements into Vector
        // Custom inputs
        V.add(100);
        V.add(30);
        V.add(7);
        V.add(24);
        V.add(13);
 
      System.out.println("Vector elements : "+V);
        // For-each loop to traverse through vector
 
        // If element is present in vector
        for (Integer i : V) {
 
            // if greater than maxValue, then update
            if (i > maxValue) {
                maxValue = i;
            }
 
            // if less than minValue, then update
            if (i < minValue) {
                minValue = i;
            }
        }
 
        // Print maximum element in the Vector
        System.out.println("Maximum Element in Vector : "
                           + maxValue);
 
        // Print minimum element in the vector
        System.out.println("Minimum Element in Vector : "
                           + minValue);
    }
}


输出
Vector elements : [1, 2, 3, 4, 5]
Maximum Number in Vector is : 5
Minimum Number in Vector is : 1

方法 2:使用迭代向量元素设置 min-max。

方法:

  • 取一个变量说 minNumber,然后初始化为最大值以进行比较和更新。
  • 取一个变量说 maxNumber,稍后初始化为最小值以比较更新。
  • 遍历 Vector 并将每个元素与 以上两个变量 -
    • 最小数量
    • 最大数量
  • 如果数字小于 minNumber,则更新 minNumber 的值。
  • 如果数量大于 maxNumber,则更新 maxNumber 的值。

例子

Java

// Java Program to Find minimum and maximum elements
// from a Vector
 
// Importing all classes of
// java.util package
import java.util.*;
// Importing Integer Wrapper class for
// primitive data type
import java.lang.Integer;
 
// Clas
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Assign maximum value to minValue Variable
        int minValue = Integer.MAX_VALUE;
 
        // Assign minimum value to maxValue Variable
        int maxValue = Integer.MIN_VALUE;
 
        // Creating a Vector
        Vector V = new Vector();
 
        // Adding elements into Vector
        // Custom inputs
        V.add(100);
        V.add(30);
        V.add(7);
        V.add(24);
        V.add(13);
 
      System.out.println("Vector elements : "+V);
        // For-each loop to traverse through vector
 
        // If element is present in vector
        for (Integer i : V) {
 
            // if greater than maxValue, then update
            if (i > maxValue) {
                maxValue = i;
            }
 
            // if less than minValue, then update
            if (i < minValue) {
                minValue = i;
            }
        }
 
        // Print maximum element in the Vector
        System.out.println("Maximum Element in Vector : "
                           + maxValue);
 
        // Print minimum element in the vector
        System.out.println("Minimum Element in Vector : "
                           + minValue);
    }
}
输出
Vector elements : [100, 30, 7, 24, 13]
Maximum Element in Vector : 100
Minimum Element in Vector : 7