数组的排列需要大小+;1导致输出为空,Java

数组的排列需要大小+;1导致输出为空,Java,java,arrays,recursion,Java,Arrays,Recursion,首先,在做其他事情之前。。。是的,我知道使用ArrayList比使用数组容易得多。我明白了,但是对于这个项目,我需要学习如何使用一个使用递归的数组 好的,在我下面的代码中,我输出数组的所有排列,但是除非我将数组的大小设为用户输入的大小+1,否则我会得到一个ArrayIndexOutOfBounds异常。这会导致数组有一个null值,该值作为空格输出,从而导致我的输出出现问题 为什么要求我将尺寸设置为输入的尺寸加上一?我怎样才能解决这个问题?先谢谢你 package permutearray;

首先,在做其他事情之前。。。是的,我知道使用ArrayList比使用数组容易得多。我明白了,但是对于这个项目,我需要学习如何使用一个使用递归的数组

好的,在我下面的代码中,我输出数组的所有排列,但是除非我将数组的大小设为用户输入的大小+1,否则我会得到一个ArrayIndexOutOfBounds异常。这会导致数组有一个null值,该值作为空格输出,从而导致我的输出出现问题

为什么要求我将尺寸设置为输入的尺寸加上一?我怎样才能解决这个问题?先谢谢你

package permutearray;

import java.util.Scanner;

/**
*
* @author Ra'kiir
*/
public class PermuteArray {

public static Scanner keyboard = new Scanner(System.in);

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    System.out.print("How many characters in the permute array:  ");
    int p = keyboard.nextInt();
    char[] permuteArray = new char[p+1];
    System.out.print("Enter a line of " + p + " characters:  ");
    String charString = keyboard.next();
    for (int i = 0; i < p; i++) {
        permuteArray[i] = charString.charAt(i);
    }
    permutation(permuteArray, 0, p);

} //End main method

private static void permutation(char[] permuteArray, int firstIndex, int lastIndex) {

    if (lastIndex - firstIndex == 1) {
        print(permuteArray);
        swap(firstIndex, lastIndex, permuteArray);
        print(permuteArray);
        //restore the order in string array
        swap(firstIndex, lastIndex, permuteArray);

    } else {
        //swap String at firstindex with all the next Strings in the array
        for (int i = firstIndex, j = 0; i <= lastIndex; i++, j++) {
            swap(firstIndex, firstIndex + j, permuteArray);
            //With current initial String(s) find combinations for rest of the strings 
            permutation(permuteArray, firstIndex + 1, lastIndex);
            //restore the order in string array
            swap(firstIndex, firstIndex + j, permuteArray);
        }
    }

} //End swap method

private static void swap(int firstIndex, int lastIndex, char[] permuteArray) {
    char temp = permuteArray[lastIndex];
    permuteArray[lastIndex] = permuteArray[firstIndex];
    permuteArray[firstIndex] = temp;
}

private static void print(char[] permuteArray) {
    for (int i = 0; i < permuteArray.length; i++) {
        System.out.print(permuteArray[i]);
    }
    System.out.println();
} //End print method

} //End of Main Class
package permuterray;
导入java.util.Scanner;
/**
*
*@作者拉基尔
*/
公共类Permuterray{
公共静态扫描仪键盘=新扫描仪(System.in);
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
System.out.print(“排列数组中的字符数:”);
int p=keyboard.nextInt();
char[]permuterarray=新字符[p+1];
System.out.print(“输入一行“+p+”字符:”);
String charString=keyboard.next();
对于(int i=0;i对于(int i=firstIndex,j=0;i
我想我知道为什么数组的大小必须是字符数加一。最后一个索引是为空字符保留的。当你遍历数组时,你将在CHAR!=NULL时这样做,而不是N次,其中N是数组的大小。@Jon你说得对,这是一个issue,但它仍然不能解决问题。您是对的,但这是一个错误。我是粗心的type-o。@hfontanez问题是您无法检查char类型数组中的条目是否为null,因为它会给您一个错误,告诉您char和null是不可比较的类型。您可能认为他们会允许您这样做,但显然不会。我不过这是个好主意。@Ra'kiir,您显然做错了什么,因为原语
char
被初始化为空字符'\u0000'(unicode 0)。因此,您确实可以比较
char
变量的当前值,以查看它是否等于此值。您可能将其与
null
进行比较,后者是用于对象引用的保留关键字,而不是原始数据类型。