java - 需要递归地处理数组,传递数据时遇到麻烦,java

标签 java arrays methods recursion

好吧,我在这里犯了一个真正的初学者错误,但我想不出我需要做什么。我有一个数组 permArray,我正在递归地填充可能的排列。我有一个公共(public)方法来准备好参数,然后我在私有(private)方法中处理数组,该方法调用自身来处理越来越小的部分。

我遇到的问题是如何将完成的数组传递回公共(public)方法。每次完成递归时我都会返回数组吗(在每个部分中放置最后一个元素之后,其中部分大小为 1)。

哦,还有,这是练习,不是作业。

//todo:
//need to determine what is wrong with my array of linked lists
package wordchains;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
import javax.xml.soap.Node;

/**
 *
 * @author Samuel French
 */
public class WordChains {

public static void main(String[] args) {
    //variables
    int numWords = -1; //This will hold the number of words the user is inputting
    String userInput; //holds the user input to be broken up into a string array

    //Get the user's input, 0 is the quit value
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter number of words: ");
    numWords = sc.nextInt();
    System.out.println(">");
    sc.nextLine();
    userInput = sc.nextLine();
    String[] inputWords = userInput.split("\\s+");
    int numElements = inputWords.length;
    int numOfPerms = numOfPerms(numElements);

    //We will start by checking the last letter of the first word
    char cTest;
    int wordChecking = 0;

    int[][] permArray = genPerms(numElements, numOfPerms);

    for (int col = 0; col < numOfPerms; col++) {
        System.out.println();
        for (int row = 0; row < numElements; row++) {
            System.out.print(permArray[col][row] + " ");
        }

    }

}

public static int numOfPerms(int numElements) {
    int numOfPerms = numElements;
    numElements--;
    while (numElements > 0) {
        numOfPerms = numOfPerms * numElements;
        System.out.println(numOfPerms);
        numElements--;
    }
    return numOfPerms;
}

public static int[][] genPerms(int numElements, int totPerms) {
    int permArray[][] = new int[totPerms][numElements];
    //either do it like this or create an array of head nodes
    List<LinkedList<Integer>> elementsLeftList = new ArrayList<LinkedList<Integer>>();
    LinkedList tempList = new LinkedList();
    for (int x = 0; x < numElements; x++) {
        tempList.addLast(x);
    }

    for (int x = 0; x < totPerms; x++) {
        elementsLeftList.add((LinkedList<Integer>) tempList.clone());
    }

    return privateGenPerms(permArray,elementsLeftList,totPerms,0,0,totPerms);
}

private static void privateGenPerms(int[][] permArray, List<LinkedList<Integer>> elementsLeftList, int totalPermutations, int elementPlacing, int sectionBegin, int sectionSize) {
    //variables-

//totalPermutations - the total number of permutations in the whole problem

//elementPlacing - the element currently being placed's position, corresponds to the rows of permArray
//elementPlacingIndex - the number of times the element currently being placed has been placed
//sectionSize - the size of the total working section. First time this is the # of permutations

//permCounter - this counter counts the permutation working with within the section
//sectionBegin - counts the beginning of the section working with


//2 Data structures:

//permArray - 2d the array of permutations
//elementsLeftList - list of lists of elements left, corresponds to each permutation


    int totalNumberOfElements = permArray[0].length;
    //
    int numberOfElementsLeftToPlace = totalNumberOfElements - elementPlacing;
    //

    int permCounter = sectionBegin;
    //Base case
    if (numberOfElementsLeftToPlace == 1) {
        for (int x = 0; x < totalPermutations; x++) {
            permArray[x][totalNumberOfElements - 1] = (int) elementsLeftList.get(permCounter).remove(0); //may need to be a remove 1, not sure
        }
        return; //need to decide what I am going to do here
    }

    //
    int elementPlacingIndex = 0;
    int elementCurrentlyPlacing = 0; //could be a 1, don't remember
    //
    int numberOfTimesToPlaceWithinCol = (sectionSize / numberOfElementsLeftToPlace);
    //
    //
    for (; permCounter < (sectionBegin + sectionSize); permCounter++) {
        //when we need to switch to a different partition of the section
        if (elementPlacingIndex == numberOfTimesToPlaceWithinCol) {
            elementPlacingIndex = 0;
            elementCurrentlyPlacing++;
        }
        permArray[permCounter][elementPlacing] = (int) elementsLeftList.get(permCounter).remove(elementCurrentlyPlacing);
        elementPlacingIndex++;
    }
    for (int newSectionBegin = sectionBegin; newSectionBegin < (sectionBegin + sectionSize); newSectionBegin = newSectionBegin + numberOfTimesToPlaceWithinCol) {
        privateGenPerms(permArray, elementsLeftList, totalPermutations, (elementPlacing + 1), newSectionBegin, (sectionSize / numberOfElementsLeftToPlace));
    }
}
}

最佳答案

该数组是按引用传递的,因此您在私有(private)函数中所做的任何更改都将是永久性的,您不需要再次返回修​​改后的数组。

我还没有查看你的逻辑来看看这是否是你的情况下的正确方法。

关于java - 需要递归地处理数组,传递数据时遇到麻烦,java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14843869/

相关文章:

c - 以两种不同的方式在升序数组中搜索值

python - 在 python 中获取子字符串的更快方法?

android - Android webview的loadData和loadDataWithBaseURL有什么区别

php - 多次调用一个方法,是否有更有效的方法?

java - Firebase 使用 addTextChangedListener 检查用户名是否存在

java - 在 Eclipse 中创建自定义 Android 项目模板?

Java内存管理对象与原始类型

java - 缓冲后台 InputStream 实现

python - 如何将深度为 2 的元组转换为 2D Numpy 数组?

methods - 如何获取并发方法