【发布时间】:2015-02-13 22:39:25
【问题描述】:
我必须编写一个包含两个类的代码,一个反转一个字符串改变位置,例如
I love you 变为 uoy evol I
另一个类在不改变位置的情况下反转字符串,例如
I love you 变为 I evol uoy。
我有一个小代码,但如果调用这些类中的方法,我无法找到方法。
我现在所拥有的只是以第一种方式反转字符串的代码。欢迎任何帮助。
class StringReverse2{
public static void main(String[] args){
String string="I love you";
String reverse = new StringBuffer(string). //The object created through StringBuffer is stored in the heap and therefore can be modified
reverse().toString(); //Here the string is reversed
System.out.println("Old String:"+string); //Prints out I love you
System.out.println("New String : "+reverse);//Prints out the reverse that is "uoy evol I"
}
}
【问题讨论】:
-
只是一个建议,避免使用
Reserved Keywords作为变量/对象名称。有时可能会导致意外行为。 -
@PrerakSola: 比如?
-
我当然希望它不会“有时”导致“意外”行为;而不是它导致预期编译器错误总是。
-
@njzk2 - 如
String string -
@njzk2 - @PrekSola 所说的是避免
String string和ArrayList arrayList。它使阅读更容易。当然stringall lower case 不是保留字,但它只是所有小写的保留字。查看this post about class vs clazz。这是一个很好的约定