Java 數(shù)組獲取最大和最小值的實例實現(xiàn)
以下實例演示了如何通過 Collections 類的 Collections.max() 和 Collections.min() 方法來查找數(shù)組中的最大和最小值:
Main.java 文件:
import java.util.Arrays;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};
int min = (int) Collections.min(Arrays.asList(numbers));
int max = (int) Collections.max(Arrays.asList(numbers));
System.out.println("最小值: " + min);
System.out.println("最大值: " + max);
}
}
以上代碼運行輸出結果為:
最小值: 1
最大值: 9
java求數(shù)組中元素最大值最小值及其下標
功能需求:遍歷數(shù)組,并求出數(shù)組中元素的最大元素,最小元素,及其相應的索引等問題,要求用方法完成.
思路:分別創(chuàng)建不同的方法,然后再調(diào)用方法.
代碼展示:
public class Array{
public static void main(String[] args){
int[] arr={13,45,7,3,9,468,4589,76,4}; //聲明數(shù)組并賦值
//調(diào)用遍歷的方法
print(arr); //在同一個類中,類名可以省略
//調(diào)用獲取最大值的方法
System.out.println("最大元素為:"+max(arr));
//調(diào)用獲取最大值索引的方法
System.out.println("最大元素的索引為:"+maxIndex(arr));
//調(diào)用獲取最小值的方法
System.out.println("最小元素為:"+min(arr));
//調(diào)用獲取最小值索引的方法
System.out.println("最小元素的索引為:"+minIndex(arr));
//調(diào)用查找元素是否存在的方法
System.out.println("查找元素的狀態(tài)為:"+search(arr,9));
//調(diào)用查找元素是否存在并返回索引方法
System.out.println("查找元素的索引為:"+searchIndex(arr,9));
}
//遍歷數(shù)組
public static void print(int[] arr){
/*for(int i:arr){ //使用加強for循環(huán)遍歷
System.out.print(arr[i]+"\t");
}
System.out.println; */
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+"\t");
}
System.out.println();
}
//獲取最大值
public static int max(int[] arr){
int max=arr[0];
for(int i=0;i<arr.length;i++){
if(arr[i]>max){
max=arr[i];
}
}
return max;
}
//獲取最大值索引
public static int maxIndex(int[] arr){
int maxIndex=0;;
for(int i=0;i<arr.length;i++){
if(arr[i]>arr[maxIndex]){
maxIndex=i;
}
}
return maxIndex;
}
//獲取最小值
public static int min(int[] arr){
int min=arr[0];
for(int i=0;i<arr.length;i++){
if(arr[i]<min){
min=arr[i];
}
}
return min;
}
//獲取最小值索引
public static int minIndex(int[] arr){
int minIndex=0;;
for(int i=0;i<arr.length;i++){
if(arr[i]<arr[minIndex]){
minIndex=i;
}
}
return minIndex;
}
//在數(shù)組中查找指定元素是否存在 ,如是存在返回true,不存在返回false
public static boolean search(int[] arr,int number){
for(int i=0;i<arr.length;i++){
if(number==arr[i]){
return true;
}
}
return false;
}
//在數(shù)組中查找指定元素是否存在 ,如是存在返回索引,不存在返回-1
public static int searchIndex(int[] arr,int number){
for(int i=0;i<arr.length;i++){
if(number==arr[i]){
return i; //返回索引
}
}
return -1;
}
}
效果截圖:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
淺談Mybatis Plus的BaseMapper的方法是如何注入的
我們在用的時候經(jīng)常就是生產(chǎn)自定義的Mapper繼承自BaseMapper,那么BaseMapper怎么被注入到mybatis里的,本文就詳細的介紹一下,感興趣的可以了解一下2021-09-09
Java集合中獲取數(shù)據(jù)前驅和后繼元素的實現(xiàn)
使用一致性hash時,如何找到一個hash值對應的臨近節(jié)點,可以使用集合中獲取數(shù)據(jù)的前驅和后繼元素實現(xiàn),所以本文給大家介紹了Java集合中獲取數(shù)據(jù)前驅和后繼元素的實現(xiàn),文中有相關的代碼示例供大家參考,需要的朋友可以參考下2024-05-05
使用Spring Boot輕松實現(xiàn)流式AI輸出的步驟
本文介紹了如何使用Spring Boot和WebFlux實現(xiàn)流式AI輸出,通過非阻塞I/O、反應式編程和函數(shù)式路由等技術,優(yōu)化了AI應用的響應速度,提升了用戶體驗,感興趣的朋友一起看看吧2025-02-02
MyBatisPlus使用${ew.customSqlSegment}別名問題解決
在使用MyBatisPlus進行連表查詢時,可能遇到因${ew.customSqlSegment}無法加別名的問題,本文就來介紹一下如何解決,感興趣的可以了解一下2024-10-10
如何使用Sentry 監(jiān)控你的Spring Boot應用
這篇文章主要介紹了如何使用Sentry 監(jiān)控你的Spring Boot應用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11

