Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 反向数组顺序_Java_Arrays_Reverse - Fatal编程技术网

Java 反向数组顺序

Java 反向数组顺序,java,arrays,reverse,Java,Arrays,Reverse,我试图在java中反转数组的顺序。 在O(n)中使用最少内存量的最有效方法是什么。 不需要用代码回答,伪代码就可以了。 以下是我的思考过程: create a new temp array //I think this is a waste of memory, //but I am not sure if there's a better way grab elements from the end of the original a

我试图在java中反转数组的顺序。
在O(n)中使用最少内存量的最有效方法是什么。
不需要用代码回答,伪代码就可以了。
以下是我的思考过程:

  create a new temp array //I think this is a waste of memory, 
                          //but I am not sure if there's a better way
 grab elements from the end of the original array -decrement this variable
 insert element in beginning of temp array -increment this variable
then make the original array point to the temp array? //I am not sure 
            //if I can do this in java; so let's say the 
            //original array is Object[] arr; and the temp array is 
            //Object[] temp. Can I do temp = arr; ?
有没有更好更有效的方法来实现这一点,而不必使用临时数组? 最后,假设数组中没有空值,所以一切都可以工作。 多谢各位


编辑:不,这不是家庭作业。

使用单个临时元素

int array[SIZE];
int temp;

for (int i = 0; i < SIZE/2; i++)
  {
     temp = array[i];
     array[i] = array[SIZE-1 - i];
     array[SIZE-1 - i] = temp;
  }
int数组[SIZE];
内部温度;
对于(int i=0;i
您无需临时阵列即可完成此操作

  • 循环从数组的开始(或结束不重要)到中间
  • 将元素与位于(最后一个元素-索引)的元素交换(so 0和
    size-1
    ,1和
    size-2
    等)
  • 您将执行以下操作来交换:
温度=a[i]; a[i]=a[end-i]; a[end-i]=温度;
  • 重复

    • 您不需要使用临时数组;只需从开始到中间逐步遍历数组,将
      i
      处的元素替换为
      array.length-i-1
      处的元素。确保正确处理中间元素(不难做到,但一定要做到。)

      伪代码,假设基于0的索引数组:

      for i in range(0, len(array)/2):
           swap(array[i], array[(len(array)-1)-i])
      
      这里有两个解决方案:

          loop to N/2
            swap each element at i with element at N - i
      
      另一种解决方案是(根据您的情况)通过索引来反转阵列:

          GetValueAt(int i){return array[N - i];}
      

      如果是一个对象数组,那么<代码>集合.Read(数组.aslist(数组))将执行具有常量内存和线性时间的任务——不需要临时数组。

      让我们考虑数组是整数数组,那么我们也可以寻找这样的解决方案

      arr-整数数组

      for(int i=0,int J<arr.length-1 ; i<j ; i++,j--)
      {
          temp =a[i];
          a[i]=a[j];
          a[j]=temp;
       }
      

      for(inti=0,intj您只需两个步骤即可完成此操作

      ArrayList<Element> YourTempElement= new ArrayList<Element>(mElements);
      Collections.reverse(YourTempElement);
      
      ArrayList YourTempElement=新的ArrayList(mElements);
      Collections.reverse(yourtemp元素);
      
      这是一个作业吗?如果是,请标记为SUCH。考虑先将最后一个项目和第二个和第二个最后项目进行互换,直到达到列表的一半……您只需要一个临时变量,仍然会超过列表一次。您可以使用java LIB?<代码>集合吗?
      只需以相反的顺序遍历原始数组,并创建一个新容器来包含新的订单插入。这是O(n)+1,因为OP现在说这不是家庭作业,这是一个很好的答案。喜欢这个解决方案。刚刚确认不需要临时数组。请参见:…单击“克隆”,然后单击“运行”至少在Java 1.6中不起作用:System.out.println(X[0]+”到“+X[X.length-1]);Collections.reverse(Arrays.asList(X));System.out.println(X[0]+”到“+X[X.length-1]);打印:2272.6270739116至186.704625250768 2272.6270739116至186。704625250768@Jean-Yves:X的类型是什么?
      ?我强烈怀疑它不是一个对象数组,我在回答中指定了这个数组是必要的。这看起来不像Java。使用与公认答案相同的方法,只是不那么优雅,解释也不多不需要解释,兄弟,这是两个步骤,我不是解释者。堆栈溢出的好答案可以解释事情。被接受的答案可以解释。如果已经有一个好答案,你会说同样的话,或者如果根本没有办法写一个好答案,那么给这个问题添加一个答案就没有真正的意义了:那只会增加噪音。
      ArrayList<Element> YourTempElement= new ArrayList<Element>(mElements);
      Collections.reverse(YourTempElement);