java實(shí)現(xiàn)從方法返回多個(gè)值功能示例
本文實(shí)例講述了java實(shí)現(xiàn)從方法返回多個(gè)值功能。分享給大家供大家參考,具體如下:
這里介紹三個(gè)方法,使java方法返回多個(gè)值。
方法1:使用集合類
方法2:使用封裝對(duì)象
方法3:使用引用傳遞
示例代碼如下:
import java.util.HashMap; import java.util.Map; public class Test { /** * 方法1:使用集合類 (Map以外的集合類也可以隨意使用) * 目標(biāo):返回一個(gè)數(shù)組的最大值和最小值 */ public Map<String, Integer> test1(int[] arr) { Map<String, Integer> map = new HashMap<String, Integer>(); int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for (int i = 0; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } if (arr[i] < min) { min = arr[i]; } } map.put("max", max); map.put("min", min); return map; } /** * 方法2:使用封裝對(duì)象 * 目標(biāo):返回一個(gè)數(shù)組的最大值和最小值 */ public Result test2(int[] arr) { Result result = new Result(); int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for (int i = 0; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } if (arr[i] < min) { min = arr[i]; } } result.setMax(max); result.setMin(min); return result; } /** * 方法3:使用引用傳遞 (不適用基本類型及其封裝類和String類型) * 目標(biāo):返回?cái)?shù)組長(zhǎng)度,同時(shí)獲取最大值和最小值 */ public int test3(int[] arr, Result result) { int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for (int i = 0; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } if (arr[i] < min) { min = arr[i]; } } result.setMax(max); result.setMin(min); int total = arr.length; return total; } /** * 測(cè)試main */ public static void main(String[] args) { Test t = new Test(); int[] arr = { 1, 2, 3, 4, 5, 6 }; // ----------方法1測(cè)試----------- // Map<String, Integer> map = t.test1(arr); // System.out.println("max : " + map.get("max")); // System.out.println("min : " + map.get("min")); // ----------方法2測(cè)試----------- // Result result = t.test2(arr); // System.out.println("max : " + result.getMax()); // System.out.println("min : " + result.getMin()); // ----------方法3測(cè)試----------- Result result = new Result(); int total = t.test3(arr, result); System.out.println("total : " + total); System.out.println("max : " + result.getMax()); System.out.println("min : " + result.getMin()); } } class Result { int max; int min; // 構(gòu)造函數(shù) public Result() { super(); } // getters/setters(略) }
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)組操作技巧總結(jié)》、《Java字符與字符串操作技巧總結(jié)》、《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》及《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
idea注入mapper報(bào)錯(cuò)報(bào)紅的幾種解決方案
相信大家在使用idea的時(shí)候一定會(huì)遇到這樣的問題,就是在service里注入mapper的時(shí)候,明明代碼沒有問題,也可以運(yùn)行,但是idea它就是給你報(bào)個(gè)錯(cuò),有個(gè)紅色的波浪線在下面,所以本文將給大家介紹了idea注入mapper報(bào)錯(cuò)報(bào)紅的幾種解決方案,需要的朋友可以參考下2023-12-12springboot項(xiàng)目打成war包部署到tomcat遇到的一些問題
這篇文章主要介紹了springboot項(xiàng)目打成war包部署到tomcat遇到的一些問題,需要的朋友可以參考下2017-06-06java工廠實(shí)例BeanFactoryPostProcessor和BeanPostProcessor區(qū)別分析
這篇文章主要為大家介紹了BeanFactoryPostProcessor和BeanPostProcessor區(qū)別示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07簡(jiǎn)單理解java泛型的本質(zhì)(非類型擦除)
泛型在java中有很重要的地位,在面向?qū)ο缶幊碳案鞣N設(shè)計(jì)模式中有非常廣泛的應(yīng)用。泛型是參數(shù)化類型的應(yīng)用,操作的數(shù)據(jù)類型不限定于特定類型,可以根據(jù)實(shí)際需要設(shè)置不同的數(shù)據(jù)類型,以實(shí)現(xiàn)代碼復(fù)用。下面小編來簡(jiǎn)單講一講泛型2019-05-05Springboot基于maven打包分離lib及resource
這篇文章主要介紹了Springboot基于maven打包分離lib及resource,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10詳細(xì)聊一聊java語言中的package和import機(jī)制
這篇文章主要給大家介紹了關(guān)于java語言中package和import機(jī)制的相關(guān)資料,Java中的package是指將相關(guān)的類組織在一起的一種機(jī)制,它可以用來避免命名沖突,也可以方便地管理和維護(hù)代碼,需要的朋友可以參考下2024-01-01springboot 中異步任務(wù),定時(shí)任務(wù),郵件任務(wù)詳解
這篇文章主要介紹了springboot 與異步任務(wù),定時(shí)任務(wù),郵件任務(wù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09