整数数组查找java_Java 查找整数数组的最小值。

本文提供了一个Java程序,用于查找不同类型数组(整数、双精度浮点数、长整数、单精度浮点数和字节)及其二维矩阵的最小值。通过定义通用的方法来处理不同类型的数值数组,展示了如何遍历数组比较每个元素以找到最小值。
摘要由CSDN通过智能技术生成

//package com.nowjava;/*from NowJava.com - 时代Java*/

public class Main {

public static void main(String[] argv) throws Exception {

int[] array = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };

System.out.println(min(array));

}

/**

* Finds the minimum of an array of integers.

*

* @param array Array of integers

* @return Minimum value from the array

*/

public static int min(int[] array) {

int minimum = Integer.MAX_VALUE;

for (int i = 0; i < array.length; i++) {

if (minimum > array[i]) {

minimum = array[i];/* from nowjava*/

}

}

return minimum;

}

/**

* Finds the minimum of an array of doubles.

*

* @param array Array of doubles

* @return Minimum value from the array

*/

public static double min(double[] array) {

double minimum = Integer.MAX_VALUE;

for (int i = 0; i < array.length; i++) {

if (minimum > array[i]) {

minimum = array[i];

}

}

return minimum;

}

/**

* Finds the minimum of an array of longs.

*

* @param array Array of longs

* @return Minimum value from the array

*/

public static long min(long[] array) {

long minimum = Long.MAX_VALUE;

for (int i = 0; i < array.length; i++) {

if (minimum > array[i]) {

minimum = array[i];

}

}

return minimum;

}

/**

* Finds the minimum of an array of floats.

*

* @param array Array of floats

* @return Minimum value from the array

*/

public static float min(float[] array) {

float minimum = Float.MAX_VALUE;

for (int i = 0; i < array.length; i++) {

if (minimum > array[i]) {

minimum = array[i];

}

}

return minimum;

}

/**

* Finds the minimum of an array of bytes.

*

* @param array Array of bytes

* @return Minimum value from the array

*/

public static byte min(byte[] array) {

byte minimum = Byte.MAX_VALUE;

for (int i = 0; i < array.length; i++) {

if (minimum < array[i]) {

minimum = array[i];

}

}

return minimum;

}

/**

* Finds the minimum value of a matrix of integers.

*

* @param matrix Matrix of integers.

* @return Minimum value in the matrix.

*/

public static int min(int[][] matrix) {

int minimum = Integer.MAX_VALUE;

for (int[] column : matrix) {

for (int j = 0; j < matrix[0].length; j++) {

if (minimum > column[j]) {

minimum = column[j];

}

}

}

return minimum;

}

/**

* Finds the minimum value of a matrix of doubles.

*

* @param matrix Matrix of doubles.

* @return Minimum value in the matrix.

*/

public static double min(double[][] matrix) {

double minimum = Double.MAX_VALUE;

for (double[] column : matrix) {

for (int j = 0; j < matrix[0].length; j++) {

if (minimum > column[j]) {

minimum = column[j];

}

}

}

return minimum;

}

/**

* Finds the minimum value of a matrix of longs.

*

* @param matrix Matrix of longs.

* @return Minimum value in the matrix.

*/

public static long min(long[][] matrix) {

long minimum = Long.MAX_VALUE;

for (long[] column : matrix) {

for (int j = 0; j < matrix[0].length; j++) {

if (minimum > column[j]) {

minimum = column[j];

}

}

}

return minimum;

}

/**

* Finds the minimum value of a matrix of floats.

*

* @param matrix Matrix of floats.

* @return Minimum value in the matrix.

*/

public static float min(float[][] matrix) {

float minimum = Float.MAX_VALUE;

/**代码未完, 请加载全部代码(NowJava.com).**/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值