用于反转字符串数组中每个单词的Java方法

时间:2014-12-12 04:03:22

标签: java arrays string reverse

我试图创建一个方法来获取一个字符串数组并返回一个数组,其中所有单词的顺序相反,但我的代码不起作用。

当我运行它时,我得到了" [Ljava.lang.String; @ 74122d9c"

测试用例:String [] words = {" Monday"," Tuesday"," Wednesday"}; - > {" yadnoM"" yadsueT"" yadsendeW"};

public String[] reverseString(String[] words)
{
    String[] t=new String[words.length];

    for(int i=0;i<words.length;i++)
    {
        for(int j=words[i].length()-1;j>=0;j--)
        {
            t[i]+=words[i].substring(j,j+1);
        }
    }
    return t;
}

8 个答案:

答案 0 :(得分:2)

尝试使用StringBuilder.reverse

public String[] reverseString(String[] words) {
        String[] t = new String[words.length];
        for (int i = 0; i < words.length; i++) {
            t[i]= new StringBuilder(words[i]).reverse().toString();
        }
        return t;
    }

<强>更新

您获得When I run it i get "[Ljava.lang.String;@74122d9c"的事实是因为您正在打印整个String数组,因为[Ljava.lang.String表示字符串数组。您将需要遍历数组以逐个打印字符串

答案 1 :(得分:2)

您可以将字符串转换为StringBuilder,并且它具有反向方法。并且它总是更好地使用foreach而不是直到有实际需要。

public String[] reverseString(String[] words) {
    String[] t = new String[words.length];
    for (String wordTemp : words) {
      StringBuilder sb = new StringBuilder(wordTemp);
      t[i] = sb.reverse().toString();   
    }
    return t;
}

替代方法: -

public  String[]  reverseString(String[] words)
{
    String[] t=new String[words.length];

    for(int i=0;i<words.length;i++)
    {   
        //added for setting elemennt as emptyString instead of null
        t[i] = "";
        for(int j=words[i].length()-1;j>=0;j--)
        {
            t[i]+=words[i].substring(j,j+1);
        }
    }

    //using loop
    for(int i=0;i<words.length;i++)
    {
         System.out.println(t[i]);
    }
    //using Arrays Method
    System.out.println(Arrays.toString(t));
    return t;
}

答案 2 :(得分:2)

当我运行您的代码时,我没有收到您发布的相同错误,但我确实注意到null位于每个反转字的末尾。

nullyadnoM
nullyadseuT
nullyadsendeW

当你创建一个新的字符串数组时,它的所有值都默认为null:

String[] t = new String[words.length];

最简单的解决方法是在开始添加之前将其值设置为空字符串:

public static String[] reverseString(String[] words)
{
    String[] t = new String[words.length];

    for (int i = 0; i < words.length; i++)
    {
        t[i] = "";
        for (int j = words[i].length() - 1; j >= 0; j--)
            t[i] += words[i].charAt(j);
    }
    return t;
}

我已经测试了这段代码,它对我来说非常好。

输出数组,而不是使用

System.out.println(words);

使用以下内容:

System.out.println(Arrays.toString(words));

这将为您提供输出:

[yadnoM, yadseuT, yadsendeW]

答案 3 :(得分:1)

 public static void main(String[] args) {
        String[] words = { "Monday", "Tuesday", "Wednesday" };
        for (int i = 0 ; i < words.length ; i++) {
            words[i] = Reverse(words[i]);
        }
        for (int i = 0 ; i < words.length ; i++) {
            System.out.println(words[i]);
        }

    }

    private static String Reverse(String str) {
        StringBuilder result = new StringBuilder();
        StringTokenizer st = new StringTokenizer(str, "");
        while (st.hasMoreTokens()) {
            StringBuilder thisToken = new StringBuilder(st.nextToken());
            result.append(thisToken.reverse() + " ");
        }
        return result.toString();
    }

<强>输出

yadnoM 
yadseuT 
yadsendeW 

此外,您可以使用org.apache.commons.lang3.StringUtils;,只是为了减少您的工作量/缩短代码

private static String Reverse(String str) {
        return StringUtils.reverse(str);
    }

答案 4 :(得分:1)

使用帮助Stringbuilder反向函数,Code Code正常工作。

公共类示例{

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    String[] irreverseStr = {"Monday","Tuesday","Wendesday","Thursday","Friday","Saturday","Sunday"};           
    String[] reverseStr = new Example().getReverseArray(irreverseStr);

    for(int j = 0; j < reverseStr.length; j++){
        System.out.println(reverseStr[j]);
    }           
}

public String[] getReverseArray(String[] str){      
    String [] reversestr = new String[str.length];      
    for(int i=0;i<reversestr.length;i++){
        reversestr[i] = new StringBuilder(str[i]).reverse().toString();
    }       
    return reversestr;
}

}

答案 5 :(得分:1)

好吧,要以相反的顺序返回一个包含所有单词的数组,请尝试以下代码,

rdx:rax

<强>输出:

import java.util.Arrays;

public class ArrayUtilsReverse 
{
   public static void main(String[] args) 
   {
      String strArray[] = { "Monday","Tuesday","Wednesday" };
      System.out.println("Print string array in reverse order java : " + Arrays.toString(strArray));

      for(int a = 0; a < strArray.length / 2; a++)
      {
         String str = strArray[a];
         strArray[a] = strArray[strArray.length - a - 1];
         strArray[strArray.length - a - 1] = str; 
      }

      System.out.println("Reversed array : ");
      for(int a = 0; a < strArray.length; a++)
      {
         System.out.println(strArray[a]);
      }
   }
}

有关java中反向字符串数组的更多示例,请参阅this资源。

答案 6 :(得分:0)

public static String[] reverseString(String[] words) {
    String[] t = new String[words.length];
    for (int i = 0; i < words.length; i++) {
        t[i]=new StringBuffer(words[i]).reverse().toString();

    }
    return t;
}

尝试使用StringBuffer的反向方法

答案 7 :(得分:0)

Java 8+

public String[] reverseStringArray(String[] test){
       String[] result = Arrays.stream(test).map(item -> {
            List<String> list = Arrays.asList(item.split(""));
            Collections.reverse(list);
            return String.join("", list);
        }).collect(Collectors.toList()).toArray(new String[0]);
        return result;
}