java Java中的String类中没有反向方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7420428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:54:03  来源:igfitidea点击:

No reverse method in String class in Java?

javastringstringbuilder

提问by Pradeep Kumar

Why there is no reverse method in Stringclass in Java? Instead, the reverse()method is provided in StringBuilder? Is there a reason for this? But String has split(), regionMatches(), etc., which are more complex than the reverse()method.

为什么StringJava的类中没有反向方法?相反,该reverse()方法在StringBuilder? 是否有一个原因?但是 String 有split(),regionMatches()等,比reverse()方法更复杂。

When they added these methods, why not add reverse()?

当他们添加这些方法时,为什么不添加reverse()

回答by aioobe

Since you have it in StringBuilder, there's no need for it in String, right? :-)

因为你在 StringBuilder 中有它,所以在 String 中不需要它,对吧?:-)

Seriously, when designing an API there's lots of things you could include. The interfaces are however intentionally kept small for simplicity and clarity. Google on "API design" and you'll find tonsofpagesagreeing on this.

说真的,在设计 API 时,您可以包含很多东西。然而,为了简单和清晰,界面有意保持较小。谷歌的“API设计”,你会发现网页同意这一点。

Here's how you do it if you actually need it:

如果你真的需要它,你可以这样做:

str = new StringBuilder(str).reverse().toString();

回答by Simone Gianni

If you want an historical reason, String are immutable in Java, that is you cannot change a given String if not creating another String.

如果您想要一个历史原因,String 在 Java 中是不可变的,也就是说,如果不创建另一个 String,您将无法更改给定的 String。

While this is not bad "per se", initial versions of Java missed classes like StringBuilder. Instead, String itself contained (and still contains) a lot of methods to "alter" the String but since String is immutable, each of these methods actually creates and return a NEW String object.

虽然这“本身”还不错,但 Java 的初始版本错过了像 StringBuilder 这样的类。相反,String 本身包含(并且仍然包含)许多“改变”String 的方法,但由于 String 是不可变的,这些方法中的每一个实际上都创建并返回了一个 NEW String 对象。

This caused simple expressions like :

这导致了简单的表达,如:

String s = "a" + anotherString.substr(10,5).trim().toLowerCase();

To actually create in ram something like 5 strings, 4 of which are absolutely useless, with obvious performance problems (despite after there has been some optimizations regarding underlying char[] arrays).

在 ram 中实际创建类似 5 个字符串的东西,其中 4 个绝对没用,有明显的性能问题(尽管在对底层 char[] 数组进行了一些优化之后)。

To solve this, Sun introduced StringBuilder and other classes that ARE NOT immutable. These classes freely modify a single char[] array, so that calling methods does not need to produce many intermediate String instances.

为了解决这个问题,Sun 引入了 StringBuilder 和其他不可变的类。这些类可以自由修改单个 char[] 数组,因此调用方法不需要生成许多中间 String 实例。

They added "reverse" quite lately, so they added it to StringBuilder instead of String, cause that's now the preferred way to manipulate strings.

他们最近添加了“反向”,所以他们将它添加到 StringBuilder 而不是 String,因为现在这是操作字符串的首选方式。

回答by Manish Kumar

String is immutable, meaning it can't be changed.

字符串是不可变的,这意味着它不能被改变。

When you reverse a String, what's happening is that each letter is switched on it's own, means it will always create the new object each times.

当你反转一个字符串时,发生的事情是每个字母都被自己切换,这意味着它每次都会创建新对象。

Let us see with example: This means that for instance Hellobecomes as below

让我们看看例子:这意味着例如Hello变成如下

  • elloH lloeH loleH olleH
  • 嘿嘿嘿嘿嘿嘿

and you end up with 4 new String objects on the heap. So think if you have thousands latter of string or more then how much object will be created.... it will be really a very expensive. So too much memory will be occupied.

最终在堆上有 4 个新的 String 对象。所以想一想,如果你有数千个或更多的字符串,那么将创建多少对象......这将是非常昂贵的。所以会占用太多内存。

So because of this String class not having reverse() method.

所以因为这个 String 类没有 reverse() 方法。

回答by Sajid

Theoretically, String could offer it and just return the correct result as a new String. It's just a design choice, when you get down to it, on the part of the Java base libraries.

理论上,String 可以提供它,并且将正确的结果作为新的 String 返回。这只是 Java 基础库方面的一个设计选择,当您深入研究它时。

回答by Amanpreet

Well I think it could be because it is an immutableclass so if we had a reverse method it would actually create a new object.

好吧,我认为这可能是因为它是一个immutable类,所以如果我们有一个反向方法,它实际上会创建一个新对象。

回答by Luigi Plinge

As a side-note, in Scala you use the same java.lang.Stringclass and you do get a reversemethod (along with all kinds of other handy stuff). The way it does it is with implicit conversions, so that your Stringgets automatically converted into a class that does have a reversemethod. It's really quite clever, and removes the need to bloat the base class with hundred of methods.

附带说明一下,在 Scala 中,您使用相同的java.lang.String类,并且确实获得了一个reverse方法(以及各种其他方便的东西)。它的做法是使用隐式转换,以便您String自动转换为具有reverse方法的类。它真的很聪明,并且不需要用数百种方法来膨胀基类。

回答by Ed Staub

reverse() acts on this, modifying the current object, and String objects are immutable - they can't be modified.

reverse() 作用于this,修改当前对象,而 String 对象是不可变的——它们不能被修改。

It's peculiarly efficient to do reverse() in situ- the size is known to be the same, so no allocation is necessary, there are half as many loop iterations as there would be in a copy, and, for large strings, memory locality is optimal. From looking at the code, one can see that a lot of care was taken to make it fast. I suspect the author(s) had a particular use case in mind that demanded high performance.

在原位执行 reverse() 特别有效- 已知大小相同,因此不需要分配,循环迭代次数是副本中的一半,并且对于大字符串,内存局部性是最佳。从查看代码可以看出,为了使它更快,我们付出了很多努力。我怀疑作者有一个需要高性能的特定用例。