内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

java如何return兩個值

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

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:主機(jī)域名、網(wǎng)站空間、營銷軟件、網(wǎng)站建設(shè)、武都網(wǎng)站維護(hù)、網(wǎng)站推廣。

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

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

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

方法1:使用集合類方法2:使用封裝對象方法3:使用引用傳遞。

請看示例:

import java.util.HashMap;
import java.util.Map;
public class Test {

    /**
     * 方法1:使用集合類 (Map以外的集合類也可以隨意使用)
     * 目標(biāo):返回一個數(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):返回一個數(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):返回數(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兩個值的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)站欄目:java如何return兩個值
網(wǎng)站URL:http://www.rwnh.cn/article14/jippde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作網(wǎng)站改版、企業(yè)建站微信公眾號App設(shè)計、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
专栏| 南通市| 扎囊县| 赞皇县| 克拉玛依市| 柘荣县| 洛川县| 龙胜| 苏尼特左旗| 安福县| 巢湖市| 乌审旗| 东源县| 栾川县| 顺昌县| 玉门市| 南靖县| 平和县| 宁陵县| 越西县| 清原| 泽普县| 文昌市| 遵化市| 邢台市| 尉氏县| 班戈县| 剑川县| 和田县| 加查县| 巴里| 崇礼县| 天柱县| 洛阳市| 南乐县| 合阳县| 元阳县| 高州市| 台山市| 定西市| 凯里市|