2013-02-12 37 views
0

好吧,我在这里有一个真正的初学者错误,但我想不出我需要做什么。我有一个数组permArray,我递归填充可能的排列。我有一个公共方法来获取参数,然后我使用private方法处理数组,这个方法调用它自己来处理更小或更小的部分。需要对数组递归地工作,传递数据时遇到问题,java

我遇到的问题是如何将完成的数组传递回公共方法。每当我完成递归时(在将每个节中的最后一个元素放置在节大小为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)); 
    } 
} 
} 
+1

您能否以示例的形式向我们展示您的预期输入和您的预期输出? – 2013-02-12 23:34:35

回答

2

的数组传递按引用,让你在私有函数所做的任何更改将是永久性的,你不需要再返回修改后的数组。

我没有看过你的逻辑,看看这是否是正确的方法来做你的情况。