亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

大神求解,初學java,

大神求解,初學java,

慕的地8271018 2018-07-05 18:22:26
在java中創建一個方法,調用方法之后需要從方法中返回兩個數組,怎樣才能辦到
查看完整描述

2 回答

?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

方式有很多種,
1.二維數組的方式
2.Vector(不定長數組)嵌套的方式
3.Vector內套數組
3.List>的方式
4.List>的方式
……
下面我用兩種方式給你一個簡單的demo:
1.二維數組

 // 創建定長二維數組
    public static int[][] getArrays(){
        // 創建二維數組,2 -- 表示外層數組的長度,3表示里層數組的長度
        int[][] a = new int[2][3];
        int[] b = {1, 2, 3};
        int[] c = {4, 5, 6};
        a[0] = b;
        a[1] = c;
        return a;
    }

    // 二維數組
        int[][] a = getArrays();
        for(int i = 0; i < a.length; i++){
            int[] b = a[i];
            for(int j = 0; j < b.length; j++){
                System.out.print(b[j] + " ");
            }
            System.out.println(" ");
        }

        System.out.println("\n\n");

2.Vector內套數組的方式

 // 外層使用不定長數組
    public static Vector<int[]> getVectors(){
        Vector<int[]> vector = new Vector<>();
        int[] b = {1, 2, 3};
        int[] c = {4, 5, 6};
        vector.add(b);
        vector.add(c);
        return vector;
    }

    // vector
        Vector<int[]> vector = getVectors();
        // 獲取迭代器
        Iterator<int[]> iterator = vector.iterator();
        while (iterator.hasNext()) {
            int[] b = iterator.next();
            for(int c : b){
                System.out.print(c + " ");
            }
            System.out.println(" ");
        }


查看完整回答
反對 回復 2018-07-16
?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

方法返回值只能返回一個對象,如果你想返回兩個數組(其實就是兩個對象了),可以將兩個數組封裝到一個類中返回這個類即可,也可以通過返回list來解決


查看完整回答
反對 回復 2018-07-16
  • 2 回答
  • 0 關注
  • 375 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號