Results 1 to 5 of 5
  1. #1
    sparklezilla is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default Help with Creating Array List of permutations

    Hello:

    I have created this code to generate all the permutations of a string that is passed into the method.
    Each result I would like to add to an arraylist, this is where I am having difficulty placing the loop that I need.
    Any help?
    Here is what I have, but it's not producing the result I would like,

    Java Code:
    	public static void permutation(String prefix, String s)
    	{
    		int n=s.length();
    		ArrayList<String> candidates= new ArrayList<String>();
    		
    		
    			if(n==0)
    			{
    				candidates.add(prefix);
    				
    			}
    			else
    			{
    			
    			permutation(prefix + s.charAt(i), s.substring(0,i) + s.substring(i+1, n));
    			}
    		}
    		
    		System.out.println(candidates);
    		
    	}
    Thanks

  2. #2
    sparklezilla is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    sorry i forgot to add the for loop i have right now

    Java Code:
    public static void permutation(String prefix, String s)
    	{
    		int n=s.length();
    		ArrayList<String> candidates= new ArrayList<String>();
    		
    		
    		
    			if(n==0)
    			{
    				candidates.add(prefix);
    				
    			}
    			else
    			{
    			for(int i=0; i<n; i++)
    			{
    			
    			permutation(prefix + s.charAt(i), s.substring(0,i) + s.substring(i+1, n));
    			}
    			}
    		
    		
    		System.out.println(candidates);
    		
    	}

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    You are creating a new ArrayList with each iteration of the method. That's not what you want. Either make your ArrayList an instance variable, or pass it as another parameter.

    -Gary-

  4. #4
    sparklezilla is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    I tried creating it outside the method, but got a null pointer exception

    Java Code:
    public static ArrayList<String> candidates;
    	public static void permutation(String prefix, String s)
    	{
    		int n=s.length();
    		
    		
    		
    		
    			if(n==0)
    			{
    				candidates.add(prefix);
    				
    			}
    			else
    			{
    			for(int i=0; i<n; i++)
    			{
    			
    			permutation(prefix + s.charAt(i), s.substring(0,i) + s.substring(i+1, n));
    			}
    			}
    		
    		
    		
    		
    	}

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    OK, you've declared it as a static class variable, but you haven't initialized it. You need to initialize it at some point before you call your permutation() method.

    It's not great style to make your variables and methods static like that, although I understand it's confusing when you're learning. This is a better approach:
    Java Code:
    public class MyClass {
            // declare your instance variables -- you may also initialize them here,
            // but I prefer to do it explicitly in a constructor. I just think it's clearer.
            private ArrayList<String> myArrayList;
    
            // initialize your instance variables in a constructor
            public MyClass() {
                    myArrayList = new ArrayList<String>();
            }
    
            // use verbs, not nouns, for method names
            public void permute(String prefix, String s) {
                    ...
            }
    
            // use a non-static starting point to do all the stuff you're
            // making main() do now
            private void run() {
                    // TODO: get a String from the user
                    permute("", userString);
            }
    
            // since the rest of our methods are not static now, we need an
            // instance of MyClass -- here's a simple way to do it
            public static void main(String[] args) {
                    new MyClass().run();
            }
    }
    If you really don't want to use this approach, it's probably better to declare your ArrayList within main(), and pass it as another parameter in your method.

    -Gary-

Similar Threads

  1. array permutations
    By mtK in forum Advanced Java
    Replies: 4
    Last Post: 04-08-2010, 06:57 AM
  2. Permutations
    By m00nchile in forum New To Java
    Replies: 3
    Last Post: 03-25-2010, 09:58 PM
  3. Find all permutations of a number
    By matzahboy in forum New To Java
    Replies: 6
    Last Post: 12-02-2008, 04:59 AM
  4. Creating an Iterator for Linked List
    By spanky in forum New To Java
    Replies: 3
    Last Post: 10-05-2008, 08:16 PM
  5. creating a deck of cards using a linked list
    By boomba88 in forum New To Java
    Replies: 2
    Last Post: 09-11-2008, 04:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •