如何在不循环的情况下反转数组或创建新数组

class ReverseArrayElements1
{
  public static void main ( String[] args )
  {
    int[] values = {10,20,30,40}; 
    int temp;

     System.out.println( "Original Array: " + values[0] + "\n" + values[1] +
                         "\n" + values[2] + "\n" + values[3]   );

    // reverse the order of the numbers in the array



    System.out.println( "Reversed Array: " + values[0] + "\n" + values[1] + "\n"
                         + values[2] + "\n" + values[3] );
   }
}

任务 我需要完成程序,以便数组中的数字以相反的顺序出现。这并不意味着我只需要以相反的顺序显示元素。我实际上将数组中的最后一个元素移到数组的第一个元素中,依此类推。我不能使用循环或创建新数组。

输出应为

Original Array: 10 20 30 40 
Reversed Array: 40 30 20 10 
hzthzjln 回答:如何在不循环的情况下反转数组或创建新数组

如果您使用的是Java 8:

import java.util.stream.IntStream;

// stuff

int[] reversed = IntStream.range(0,values.length).map(i -> values[values.length-i-1]).toArray();
本文链接:https://www.f2er.com/2983417.html

大家都在问