0

I'm currently desperatly trying to get an ArrayList that I return from a function into a new ArrayList in my main function...

Here are the code snippets:

public static ArrayList<String> permute(String begin, String end) {
ArrayList<String> al=new ArrayList<String>();
//filling bla
return al;
}

and here's where I call the function in the main function:

ArrayList<String> arr =permute("","abc");

arr unfortunately is empty, and I have no idea how to get it to work :(

Thanks in advance

EDIT: Here's the full code:

import java.util.*;
class Problem24 {

    public static ArrayList<String> permute(String begin, String end) {
        ArrayList<String> al=new ArrayList<String>();
        if (end.length() <= 1) {
            String s=begin+end;
            al.add(s);
        } else {
            for (int i = 0; i < end.length(); i++) {
                try {
                    String newString = end.substring(0, i) + end.substring(i + 1);
                    permute(begin + end.charAt(i), newString);
                } catch (StringIndexOutOfBoundsException exception) {
                    exception.printStackTrace();
                }
            }
     }
     return al;
  }
    public static void main (String[] args)
    {
        ArrayList<String> arr =permute("","abc");
            System.out.println(arr.get(0));
    }
}
9
  • 9
    probably the //filling bla part is wrong. no chance to tell what's exactly wrong if you don't give the code Commented Feb 28, 2013 at 12:52
  • are you filling anything in al? Commented Feb 28, 2013 at 12:52
  • It looks like "filling bla" doesn't fill the al ArrayList. You should look for the problem there, rest of the code looks fine. Commented Feb 28, 2013 at 12:53
  • Problem is definitely not in the portion of code you posted. Post how you are filling the list. Commented Feb 28, 2013 at 12:53
  • What gefei said. Whatever code you have there is not putting anything into the list. Please post the rest of your code. Commented Feb 28, 2013 at 12:54

6 Answers 6

2

Your ArrayList is empty but not null which means that the returning part worked. In oder to use the values from the method you need to fill the ArrayList inside the method.

ps: You should use List list = new ArrayList() or List list = permute("", "abc") which is a simple version of dependency injection and a better design of your program.

Sign up to request clarification or add additional context in comments.

Comments

1

You're not adding the items from the recursive calls.

Try adding the al.addAll to your permute call:

al.addAll(permute(begin + end.charAt(i), newString));

Comments

1

before returning the value make sure you are filling the al List

     al.add(begin);
     al.add(end);
     al.add("any other string");

     return al;

Comments

1

Obviously something in wrong with the // filling bla part.

I'd start with replacing your code in // filling bla with al.add("TEST"); and see if you even get something out.

Also your method is static and the source array isn't passed in, which suggest that either your code is supposed to permute those strings somehow. Are you possibly acting upon a static array (i.e. permute all elements between begin and end), and the source array is empty?

Comments

0
import java.util.*;
class Problem24
{

    public static ArrayList<String> permute(String begin, String end)
    {
        ArrayList<String> al=new ArrayList<String>();
        if (endingString.length() <= 1)
        {
            String s=begin+end;
            al.add(s);
        }
        else
        {
            for (int i = 0; i < end.length(); i++)
            {
                try
                {
                    String newString = end.substring(0, i) + end.substring(i + 1);
                    al.add(permute(begin + end.charAt(i), newString));
                }
                catch (StringIndexOutOfBoundsException exception)
                {
                    exception.printStackTrace();
                }
            }
        }
        return al;
    }

    public static void main (String[] args)
    {
            ArrayList<String> arr = permute("","abc");
            System.out.println(arr.get(0));
    }
}

hope i corrected it the right way.

Comments

0

You call your method permute recurrently --- but you make no use of what it returns when it returns. In other words, you create a new array each time you call permute, but everything you get in for-loop is lost, as you are not passing it to next permute() calls --- eventually you return an empty array in which you didn't put anything.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.