溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java如何return兩個(gè)值

發(fā)布時(shí)間:2020-09-17 09:27:40 來源:億速云 閱讀:377 作者:小新 欄目:編程語言

小編給大家分享一下java如何return兩個(gè)值,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

想返回多個(gè)值,可以首先創(chuàng)建一個(gè)類,這個(gè)類有兩個(gè)成員變量,使這個(gè)類作為返回的對象。

public class Result {    
int max;    
    int min;    
    // 構(gòu)造函數(shù)
    public Result() {    
        super();
    }    
    // getters/setters(略)
}

這里使Java return(返回)兩個(gè)值的方法:

方法1:使用集合類方法2:使用封裝對象方法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:使用封裝對象
     * 目標(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ù)組長度,同時(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;
    }
    
    /**
     * 測試main
     */
    public static void main(String[] args) {
        Test t = new Test();

        int[] arr = { 1, 2, 3, 4, 5, 6 };
        
        // ----------方法1測試-----------
        // Map<String, Integer> map = t.test1(arr);
        // System.out.println("max : " + map.get("max"));
        // System.out.println("min : " + map.get("min"));

        // ----------方法2測試-----------
        // Result result = t.test2(arr);
        // System.out.println("max : " + result.getMax());
        // System.out.println("min : " + result.getMin());

        // ----------方法3測試-----------
        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());

    }

}

以上是java如何return兩個(gè)值的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI