0

I have a string array

 "Ben", "Jim", "Ken"

how can I print the above array 3 times to look like this:

 "Ben", "Jim", "Ken"
 "Jim", "Ben", "Ken"
 "Ken", "Jim", "Ben"

I just want each item in the initial array to appear as the first element. The order the other items appear does not matter.

more examples

Input

"a","b","c","d"

output

"a","b","c","d"
"b","a","c","d"
"c","b","a","d"
"d","a","c","d"

Method signature

 public void printArray(String[] s){
 }
1
  • 4
    Is there anything you've already tried? Please try to make an attempt to try it before asking a question here. It's a common beginner's mistake, but just remember this website is not meant for people to do the work for you. Commented Jul 18, 2016 at 19:48

4 Answers 4

2

Rather than give you straight-up code, I'm going to try and explain the theory/mathematics for this problem.

The two easiest ways I can come up with to solve this problem is to either

  • Cycle through all the elements
  • Pick an element and list the rest

The first method would require you to iterate through the indices and then iterate through all the elements in the array and loop back to the beginning when necessary, terminating when you return to the original element.

The second method would require you to iterate through the indices, print original element, then proceed to iterate through the array from the beginning, skipping the original element.

As you can see, both these methods require two loops (as you are iterating through the array twice)

In pseudo code, the first method could be written as:

for (i = array_start; i < array_end; i++) {
    print array_element[i]
    for (j = i + 1; j != i; j++) {
        if (j is_larger_than array_end) {
            set j equal to array_start
        }
        print array_element[j]
    }
}

In pseudo code, the second method could be written as:

for (i = array_start; i < array_end; i++) {
    print array_element[i]
    for (j = array_start; j < array_end; j++) {
        if (j is_not_equal_to i) {
            print array_element[j]
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
public void printArray(String[] s){
    for (int i = 0; i < s.length; i++) {
        System.out.print("\"" + s[i] + "\",");
        for (int j = 0; j < s.length; j++) {
            if (j != i) {
                System.out.print("\"" + s[j] + "\",");
            }
        }
        System.out.println();
    }
}

3 Comments

Please provide explanations or comments to/in your code, otherwise, despite this being quite basic, it's meaningless to someone who doesn't know how the code works.
@AlmightyR I only answered the question because it was too easy. If you think it's insufficient, feel free to upvote another answer or write your own. If you think my answer is totally unhelpful, feel free to downvote or flag it.
shmosel, while your answer is definitely not "totally unhelpful", it does only provide a code solution, without providing any explanation or context of how or why the solution works. You need not more than common-sense to deduce that if the OP didn't use code similar to yours, it's a safe bet that he simply doesn't understand how to, otherwise, he would have reached a solution on his own, and there would be no such question. Basically, by providing just code rather than an answer, you're doing the OP a disservice IMO, and delaying his learning. So, indeed, -1.
0

This sounds like a homework question so while I feel I shouldn't answer it, I'll give a simple hint. You are looking for an algorithm which will give all permutations (combinations) of the "for loop index" of the elements not the elements themselves. so if you have three elements a,b,c them the index is 0,1,2 and all we need is a way to generate permutations of 0,1,2 so this leads to a common math problem with a very simple math formula.

See here: https://cbpowell.wordpress.com/2009/11/14/permutations-vs-combinations-how-to-calculate-arrangements/

Comments

0
    for(int i=0;i<s.length;i++){
        for(int j=i;j<s.length+i;j++) {
            System.out.print(s[j%(s.length)]);
        }
        System.out.println();
    }

Using mod is approppiate for this question. The indexes of the printed values for your first example are like this;

0 1 2

1 2 0

2 0 1

so if you write them like the following and take mod of length of the array (3 in this case) you will reach solution.

0 1 2

1 2 3

2 3 4

1 Comment

Please provide explanations or comments to/in your code, otherwise, despite this being quite basic, it's meaningless to someone who doesn't know how the code works.

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.