LAMBDA防爆pression扭转一个字符串 - Java的 [英] Lambda Expression to reverse a string - Java
问题描述
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune" };
我的星球名称的简单数组,我想扭转这种被添加到阵列(不是字符串顺序)每个名字。结果
我继续lambda语法奋斗:
I have a simple array of planet names and I want to reverse each name that was added to the array (not the string order).
I continue to struggle with lambda syntax:
Arrays.sort(planets, (first, second) -> new StringBuilder(first).reverse().toString());
System.out.println(Arrays.toString(planets));
目前我得到的:
在该行多个标记
- 的方法
反向()
中的类型的StringBuilder
不适用于参数(字符串)
- 类型不匹配:不能从
字符串
转换为INT
- The method
reverse()
in the typeStringBuilder
is not applicable for the arguments(String)
- Type mismatch: cannot convert from
String
toint
响应:
感谢大家的建议... asList让我接近。我仍然没有正确地排序
Thank you all for suggestions... asList got me close. I still didn't sort correctly.
所以,我需要找到一种方法,使工作的Array.sort因为这是分配(取中的Array.sort和lambda它)。
So i need to find a way to make array.sort work because this was the assignment (to take array.sort and lambda it).
我ping的教练在这里要求澄清,但我的时间不多了。
I have pinged the instructor to ask for clarification here, but i am running out of time.
下面是我的工作,试图保持的Array.sort
Here is my work attempting to keep array.sort
// lambda expression , long to short
System.out.println("5) Sorted by length (descending):");
Arrays.sort(planets, (first, second) -> second.length() - first.length());
System.out.println(Arrays.toString(planets));
// lambda expression , reverse the name , then sort in ascending order
// hint use new StringBuilder(first).reverse().toString() to reverse the string
System.out.println("6) Sorted in dictionary order of the reversed name (ascending)");
Arrays.sort(planets, (first, second) -> new StringBuilder(first).reverse().toString().compareTo(second));
System.out.println(Arrays.toString(planets));
// lambda expression , reverse the name , then sort in descending order
System.out.println("7) Sorted in dictionary order of the reversed name (descending)");
Arrays.sort(planets, (first, second) -> new StringBuilder(second).reverse().toString().compareTo(first));
System.out.println(Arrays.toString(planets));
我不能为我的生活弄清楚是什么在这里是所有前3 pressions让我完全相同的结果......每个那些给了我原来的数组排序时间最长最短的。
What I cannot for the life of me figure out here is that all 3 expressions get me exactly the same result... each of those gives me the original array sorted longest to shortest.
这是我的结果:
5)按长度排序(降序):
[海王星,水星,木星,天王星,土星,金星,地球,火星]
5) Sorted by length (descending): [Neptune, Mercury, Jupiter, Uranus, Saturn, Venus, Earth, Mars]
6)逆转名字字典顺序(升序排序)
[海王星,水星,木星,天王星,土星,金星,地球,火星]
6) Sorted in dictionary order of the reversed name (ascending) [Neptune, Mercury, Jupiter, Uranus, Saturn, Venus, Earth, Mars]
7)排序在扭转名字典顺序(降序)
[海王星,水星,木星,天王星,土星,金星,地球,火星]
7) Sorted in dictionary order of the reversed name (descending) [Neptune, Mercury, Jupiter, Uranus, Saturn, Venus, Earth, Mars]
推荐答案
不使用流API和就地更新字符串替代的解决方案:
An alternative solution which does not use Stream API and updates the strings in-place:
Arrays.asList(planets).replaceAll(s -> new StringBuilder(s).reverse().toString());
演示:
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune" };
Arrays.asList(planets).replaceAll(s -> new StringBuilder(s).reverse().toString());
System.out.println(Arrays.toString(planets));
// [yrucreM, suneV, htraE, sraM, retipuJ, nrutaS, sunarU, enutpeN]
这篇关于LAMBDA防爆pression扭转一个字符串 - Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!