首页 > 解决方案 > 从java中的方法返回一个数组?

问题描述

为什么我的输出屏幕上没有任何输出?应该有一个反向数组。

public class Main {
    static int[] reverse(int[] array)
    {
        int[] result =new int[array.length];
        for(int i=0,j=array.length-1;i<array.length;i++,j--)
        {
            result[j]=array[i];
        }
        return result;
    }


    public static void main(String[] args) {

        int[] newArray = {1,5,9,7,8,0,3,2};
        reverse(newArray);
    }
}

标签: javaarraysmethods

解决方案


如果您想在控制台中查看输出,则需要将其发送到系统的输出流(在这种情况下适用)或错误流。

int[] newArray = {1,5,9,7,8,0,3,2};
newArray = reverse(newArray);     // <- reuse the variable to save memory
System.out.println(Arrays.toString(newArray));

推荐阅读