2

I'm trying to create the "cat finds mouse" thing myself but I have troubles doing it because the way I print the field is pretty complicated / inefficient.

So I have the field:

String[][]field = {
            {"+", "+", "+", "+", "+", "+", "+", "+", "+"},
            {"+", " ", " ", " ", " ", " ", " ", "M", "+"},
            {"+", " ", "+", "+", "+", "+", "+", " ", "+"},
            {"+", " ", "+", "+", " ", " ", "+", " ", "+"},
            {"+", " ", " ", " ", " ", "+", "+", " ", "+"},
            {"+", " ", " ", "+", "+", "+", "+", " ", "+"},
            {"+", "C", " ", " ", " ", " ", " ", " ", "+"},
            {"+", "+", "+", "+", "+", "+", "+", "+", "+"},
        };

And it is supposed to be printed like this:

+++++++++
+      M+
+ +++++ +
+ ++  + +
+    ++ +
+  ++++ +
+C      +
+++++++++

Here is my code how I print it like that. The print is very fine as desired but the way it is coded and done is very inefficient... Isn't there a way to do all that with just few lines of code? Please note that it must look exactly as I posted.

Here is my way:

public class CatMouseCheap {

    public static void main(String[] args){

        String[][]field = {
            {"+", "+", "+", "+", "+", "+", "+", "+", "+"},
            {"+", " ", " ", " ", " ", " ", " ", "M", "+"},
            {"+", " ", "+", "+", "+", "+", "+", " ", "+"},
            {"+", " ", "+", "+", " ", " ", "+", " ", "+"},
            {"+", " ", " ", " ", " ", "+", "+", " ", "+"},
            {"+", " ", " ", "+", "+", "+", "+", " ", "+"},
            {"+", "C", " ", " ", " ", " ", " ", " ", "+"},
            {"+", "+", "+", "+", "+", "+", "+", "+", "+"},
        };

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

    }

}
  • 2
    Why do you think this is inefficient ? – Arnaud Feb 03 '17 at 14:37
  • I would say, that this is O(n) complexity and it won't get any better. :-) – holmicz Feb 03 '17 at 14:38
  • This is as efficient as it can get. Maybe using a stringbuilder instead of seperated characters would make it slightly faster, but that's not noticeable. – Tobias G Feb 03 '17 at 14:38
  • @Berger because I want find the shortest way from cat to mouse. And in these 2 for loops I have big troubles doing it :s Edit: And this shouldn't be O(n), rather O(n^2) or am I wrong? –  Feb 03 '17 at 14:39
  • @cnmesr It is O(n), because the number of iterations is equal to number of elements. It would be O(n^2) if you would e.g. iterate over all elements and compare each of them to all others. – holmicz Feb 03 '17 at 18:34

1 Answers1

2
System.out.println(
            Arrays.stream(field)
                .map(a -> String.join("", a))
                    .collect(Collectors.joining("\n")));

Works with java8

mc20
  • 1,145
  • 1
  • 10
  • 26
  • I know of this. But the output is horrible, it isn't as I described in my question : / Maybe there is a way to modify it? Edit: I didn't downvote! –  Feb 03 '17 at 14:35
  • @cnmesr Sorry I missed it. – mc20 Feb 03 '17 at 14:36
  • Changed to java8. Hope this helps – mc20 Feb 03 '17 at 14:45
  • Haha so good, I hope it will also help finding the shortest way from cat to mouse. I mean the 2 for loops I used made a lot things harder to code it, If I imagined all correctly. But this is very nice already, to see another way, thank you :) –  Feb 03 '17 at 14:51
  • Java 8 makes it happen. Refer this http://stackoverflow.com/questions/30065577/how-to-loop-and-print-2d-array-using-java-8 for more info. Its very similar – mc20 Feb 03 '17 at 14:55