在Java中生成所有排列

编程入门 行业动态 更新时间:2024-10-15 12:28:50
本文介绍了在Java中生成所有排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能重复: 生成给定字符串的所有排列

我在Java中有一个任意长度的数组,我想生成它们的所有可能的排列。对固定长度执行此操作的简单方法是一系列嵌套for循环,但由于数组长度未知,因此这不是一个选项。有没有一种直接的方法在Java中实现这一点?

I have an array of arbitrary length in Java, and I would like to generate all possible permutations of them. The easy way to do this for a fixed length would be a series of nested for loops, but because the array is of unknown length, that is not an option here. Is there a straightforward way to accomplish this in Java?

推荐答案

使用递归函数而不是循环。每次调用方法时,都应该在数组的较小部分上,并在length = 0时停止。这个链接应该可以帮助您设计功能。

Use a recursive function, instead of loops. Each time you call the method should be on a smaller portion of the array and stop when length = 0. This link should help you design your function.

更多推荐

在Java中生成所有排列

本文发布于:2023-11-30 00:19:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648088.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:排列   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!