-1

I need to use a static method that returns an string array from the another string array in reverse. So if the array in the formal parameters is equal to "hello","there" the return needs to be "olleh","ereht".

My idea was to use the charAt command but it doesn't seem to work with an array. I can not use a built in method to solve this problem in one step. I also don't know how to move on to the next element in the array. Here is my code.

Part of main method that sets up the original array:

    String [] d = {"hey","hello"};
    System.out.println(Arrays.toString(count(d)));

My method:

    private static String [] count(String[] d)
{
    String[] reverse = new String [d.length];
    int l = d.length;
    for(int i = l -1; i >= 0; i--)
    {
        reverse = reverse + d.charAt(i);

    }
    return reverse;
}
2
  • That shows part of they way for an Int array. I don't know how to do this for a String array. Commented Nov 1, 2015 at 21:47
  • try this...stackoverflow.com/a/22282907/3660930 Commented Nov 1, 2015 at 21:49

4 Answers 4

2

So you want to reverse each string in an array.

Here's one way to reverse a single string:

private static String reverseString(String s) {
    char[] orig = s.toCharArray();
    char[] reverse = new char[orig.length];
    for (int i = 0; i < orig.length; i++) {    
        reverse[i] = orig[orig.length - i - 1];
    }
    return new String(reverse);
}

With the help of the above method, you can create the array of reversed strings like this:

private static String[] reverseMany(String[] strings) {
    String[] result = new String[strings.length];
    for (int j = 0; j < strings.length; ++j) {
        result[j] = reverseString(strings[j]);
    }
    return result;
}
Sign up to request clarification or add additional context in comments.

5 Comments

For clarification: I need to keep the actual elements in the same place. I need to reverse the contents of the elements and return that.
Is there a way to do this in just one method? I don't believe my instructions would permit me to do this. I know in Matlab there are anon functions. Is there something similar in Java that would allow that?
@frillybob There are ways to do that, but a sane programmer won't do that: Single responsibility principle
@janos @Tom I have a further question regarding the first method. How does reverseString(String s) work? I don't see String s being declared anywhere. I guess I don't understand how result[j] = reverseString(d[j]); is passed to private static String reverseString(String s)
@frillybob string s is declared in the method parameter list. I don't understand your second question
1

You can use StringBuilder#reverse for reversing a String.

The following code will reverse all the Strings from a given array:

private static String [] count(String[] d)
{
    String[] reverse = new String [d.length];
    for(int i = 0; i < d.length; i++)
    {
        reverse[i] = new StringBuilder(d[i]).reverse().toString();
    }
    return reverse;
}    

The more elegant, one line solution using Java 8 streams would be:

private static String [] count(String[] d)
{
    return Arrays.stream(d).map(s -> new StringBuilder(s).reverse().toString()).toArray(String[]::new);
}    

Comments

0

check out this code I created using PHP, it's pretty simple :

// the array you want to reverse
$array = [13, 4, 5, 6, 40];
// get the size of the array
$arrayLength = count($array);
// get the index of the middle of the array
$index = intval($arrayLength / 2); 
for ($i=0; $i < $index; $i++) {
    // we store the value in a temp variable
    $tmp = $array[$i];
    // and finaly we switch values
    $array[$i] = $array[$arrayLength - $i - 1];
    $array[$arrayLength - $i - 1] = $tmp;
}

6 Comments

I'm not sure I can follow that code. I assume the $ character indicates a variable. With that being said I don't know what intval is and its similar method in java.
yes $ names are for variables and the equivalant of intval is simply a cast to int wich can be done using (int) a / b
So in java: index = (a/b) and PHP it would be: $index = invtal(a/b)
@frillybob nop, actually it would be index = (int)(a/b) to get just the integer part of the number, in case the array's length is odd
Are you referring to the Java or PHP code. AFAIK index = (int)(a/b) is not valid java syntax. Also I converted it to java but i get [40, 6, 5, 6, 40] as the output from the string array String [] a = {"13","4","5","6","40"};
|
0

To use this library in android compile 'org.apache.commons:commons-lang3:3.5'

With [Commons.Lang][1], you could simply use

ArrayUtils.reverse(int[] array)

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.