0

I want to know if (and how) I can put my reversed array from my method ArrayReverse in the array result(where the ?????????? are) in my main method.
The code should print out "Funktioniert"

My code:

public class blubb {
    public static void main(String[] args) {
        char[] array = {'t', 'r', 'e', 'i', 'n', 'o', 'i', 't', 'k', 'n', 'u', 'F'};
        char[] result= new char[??????????];

        result=ArrayReversed(array);

        for (int i = 0; i < ergebnis.length; i++) {
            System.out.println(ergebnis[i]);

       }
    }

    public static char[] ArrayReversed(char[] arr) {
        char [] blubb= new char[arr.length];
        for (int i = arr.length-1; i >=0; i--) {
           blubb[i]=arr[i];
        }
        return blubb;

    }
}
3
  • You mean, you want to reassign the result variable? Commented Nov 25, 2014 at 21:45
  • @kolossus yes i want this :) Commented Nov 25, 2014 at 21:49
  • 2
    P.S. psvm is not an abbreviation commonly used to refer to public static void main... please don't use it. Commented Nov 25, 2014 at 21:53

4 Answers 4

1

All the answers are correct, you just assign the return result to your variable:

char[] result = ArrayReversed(array);

The reason you think it doesn't work is because of this line in your code:

for (int i = arr.length-1; i >=0; i--) {
    blubb[i] = arr[i];
}

This is not going to revers the array. You are just copying same characters from one array to another, into same positions. Perhaps you are looking for something like:

for(int i=0; i<arr.length; i++) {
    blubb[i] = arr[arr.length-1-i];
}
Sign up to request clarification or add additional context in comments.

Comments

0

Basically, you don't need to initialize your "result" array. Just set it to the returned value of ArrayReversed. Then in your for loop, you actually want to use the result array and iterate/print out its values.

public class blubb {
    public static void main(String[] args) {
        char[] array = {'t', 'r', 'e', 'i', 'n', 'o', 'i', 't', 'k', 'n', 'u', 'F'};
        char[] result;

        result=ArrayReversed(array);

        for (int i = 0; i < result.length; i++) {
            System.out.println(result[i]);

       }
    }

    public static char[] ArrayReversed(char[] arr) {
        char [] blubb= new char[arr.length];
        for (int i = arr.length-1; i >=0; i--) {
           blubb[i]=arr[i];
        }
        return blubb;

    }
}

Comments

0

why not just do char[] result = ArrayReversed(array);

Also, your method ArrayReversed is wrong. It should be like this.

public static char[] ArrayReversed(char[] arr) {
    char [] blubb= new char[arr.length];
    for (int i = arr.length-1; i >=0; i--) {
       blubb[i]=arr[arr.length-1-i];
    }
    return blubb;

}

3 Comments

doesn't work. it doesn't outprint "Funktioniert" it prints the array "treinoitknuf" out....
well that is because your reverse array is wrong heh
@Saboteur blubb[i]=arr[i] just makes a copy of the array, since you're copying every character into the same position.
0

You don't need to allocate new memory to result. Something like this should work (note some changes I made to your code since there was one more bug in your code)

public class blubb {
    public static void main(String[] args) {
        char[] array = {'t', 'r', 'e', 'i', 'n', 'o', 'i', 't', 'k', 'n', 'u', 'F'};
        char[] result= ArrayReversed(array);

        System.out.println(result);           
    }

    public static char[] ArrayReversed(char[] arr) {
        char [] blubb= new char[arr.length];
        for (int i = arr.length-1, j = 0; i >=0; i--, j++) {
           blubb[i]=arr[j];
        }
        return blubb;
    }
}

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.