-4

Having trouble figuring out how to change this recursive method into an iterative method, anyone have any helpful pointers? I get how the recursive method works but not quite sure the steps to transform to iterative, thanks.

private static void printFriends(Friend friend, int distance) {
        if (distance == 0) {
            System.out.println(friend);
        } else {
            for (Friend f : friend.getFriends()) {
                System.out.println(f + "0");
                printFriends(f, distance - 1);
            }
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        List<Friend> friendList = new ArrayList<Friend>();

        Friend reuben = new Friend("Reuben");
        Friend soumitra = new Friend("Soumitra");
        Friend ken = new Friend("Ken");
        Friend elisa = new Friend("Elisa");
        Friend isaac = new Friend("Isaac");

        friendList.add(reuben);
        friendList.add(soumitra);
        friendList.add(ken);
        friendList.add(elisa);
        friendList.add(isaac);

        reuben.addFriend(soumitra);
        reuben.addFriend(ken);

        soumitra.addFriend(reuben);

        ken.addFriend(reuben);
        ken.addFriend(elisa);

        elisa.addFriend(ken);
        elisa.addFriend(isaac);

        isaac.addFriend(elisa);

        BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
        String inputName = "";
        String inputValue = "";
        Friend inputFriend = null;
        int inputInt = 0;

        System.out.println("Blank entry at any prompt exits program.");

        while (true) {
            System.out.print("Enter a person's name: ");
            try {
                inputName = inputReader.readLine();
            } catch (IOException e) {
                System.err.println("Error reading from console.");
            }

            if (inputName.equals("")) {
                System.out.println("Run complete.");
                System.exit(0);
            }

            inputFriend = new Friend(inputName);

            if (friendList.contains(inputFriend)) {
                inputFriend = friendList.get(friendList.indexOf(inputFriend));
                System.out.print("Enter a non-negative integer: ");
                try {
                    inputValue = inputReader.readLine();
                } catch (IOException e) {
                    System.err.println("Error reading from console.");
                }

                if (inputValue.equals("")) {
                    System.out.println("Run complete.");
                    System.exit(0);
                }

                try {
                    inputInt = Integer.valueOf(inputValue);
                } catch (NumberFormatException e) {
                    System.err.println("Must enter integer.");
                    continue;
                }

                if (inputInt < 0) {
                    System.out.println("Must be non-negative");
                } else {
                    printFriends(inputFriend, inputInt);
                }
            } else {
                System.out.println(inputFriend + " not found.");
            }
        }
    }
}
snipshow7
  • 87
  • 1
  • 8

1 Answers1

1

Looking at your code, it seems that you are using the 'distance' parameter as the counter for the number of friends to print and hence the number times to recursively call the method. You can easily convert this using the for loop or while loop construct and use the distance parameter as the terminating condition for the loops. I would recommend the for loop as it really helps to simplify your code.

For loop construct: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

for (int i = 0; i < distance; i++) {
//list.get(i);
//printfriends + "0"
}

Make sure to familiarise yourself with all of the basic loop constructs in java.

Karen
  • 32
  • 5
  • I understand how that makes up for the distance, but how would it know to get friends of friends based upon distance and restart every time? – snipshow7 Apr 22 '17 at 03:03
  • You need to include more details in your question about what exactly you want this method to achieve as it is unclear from the code sample. Is there a particular resason why you can't use recursion? Are you printing the friends of friends in a certain order? Have you considered the type of data structure this is? So far it looks like a tree type of data structure which often calls for recursion rather than iteration. Drawing a diagram for the order that they should be printed in would also really help. – Karen Apr 22 '17 at 10:33