Java中,递归扭转数组 [英] Java, recursively reverse an array

查看:214
本文介绍了Java中,递归扭转数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有发现与我的函数的具体需求什么要做到这一点,是的,它是为家庭作业。

I haven't found anything with the specific needs of my function to do this, yes, it is for homework.

所以我有:

public void reverseArray(int[] x) {

}

precondition:x.length> 0

Precondition: x.length > 0

这是我不能有函数返回任何东西,唯一的参数是一个数组将离开我难住了这个事实。

The fact that I can't have the function return anything, and the only argument is an array is leaving me stumped.

我使用的递归循环一起尝试过,但一切我试过似乎与正在进行的功能无限实例结束。

I've tried using loops along with the recursion, but everything I've tried seems to end up with infinite instances of the function being made.

我已经得到了一个想法/建议使用其他的功能与这一个一起,但是,如何使用原来的递归是超越我的时刻。

I've gotten an idea/suggestion to use another function along with this one, but, how to use the original recursively is beyond me at the moment.

任何帮助是AP preciated。

Any help is appreciated.

推荐答案

如果我被编码这一点,我会创建一个临时数组(也许用一个元素去掉?)的递归调用并复制元素放回原阵列之前从函数返回。您还需要找到一个基本情况终止递归。

If I were coding this, I would create a temporary array (maybe with one element removed?) for the recursive call and copy elements back into the original array before returning from the function. You will also need to find a base case to terminate the recursion.

这篇关于Java中,递归扭转数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆