user2119109
user2119109

Reputation: 7

Using a returned ArrayList

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));
    }
}

Upvotes: 0

Views: 156

Answers (6)

Cromax
Cromax

Reputation: 2042

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.

Upvotes: 0

Ariel Pinchover
Ariel Pinchover

Reputation: 654

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.

Upvotes: 0

scott
scott

Reputation: 974

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));

Upvotes: 1

Lukas Eichler
Lukas Eichler

Reputation: 5903

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.

Upvotes: 2

mprivat
mprivat

Reputation: 21902

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?

Upvotes: 1

nsgulliver
nsgulliver

Reputation: 12671

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;

Upvotes: 1

Related Questions