英文:
I am trying to reverse an array the user inputs like {hello, Donald, Trump} -> pmurT, dlanoD etc but it outputs [null]
问题
package test;
import java.util.Arrays;
import java.util.Collections;
import javax.swing.JOptionPane;
public class WhileWarmup {
public static void main(String[] args) {
String shoppinglist = JOptionPane.showInputDialog("What do you want to manipulate");
String[] Shoppinglist = shoppinglist.split(",");
String[] Shoppinglist1 = shoppinglist.split(",");
int num = Shoppinglist.length;
String bruh = "";
String bruh1 = "";
while (num >= 0) {
Shoppinglist1 = Arrays.copyOfRange(Shoppinglist, num, num + 1);
Collections.reverse(Arrays.asList(Shoppinglist1));
bruh1 = Arrays.toString(Shoppinglist1);
bruh = bruh + bruh1;
num = num - 1;
System.out.println(bruh);
}
}
}
英文:
I'm new to java and wanted to reverse an array the user inputs (including character) so apple, banana becomes ananab, elppa. But it outputs [null]
package test;
import java.util.Arrays;
import java.util.Collections;
import javax.swing.JOptionPane;
public class WhileWarmup{
public static void main(String[] args) {
String shoppinglist = JOptionPane.showInputDialog("What do you want to manipulate");
String[]Shoppinglist = shoppinglist.split(",");
String[]Shoppinglist1 = shoppinglist.split(",");
int num = Shoppinglist.length;
String bruh = "";
String bruh1 = "";
while (num >= 0) {
Shoppinglist1 = Arrays.copyOfRange(Shoppinglist,num,num + 1);
Collections.reverse(Arrays.asList(Shoppinglist1));
bruh1 = Arrays.toString(Shoppinglist1);
bruh = bruh + bruh1;
num = num - 1;
System.out.println(bruh);
}
}
}
答案1
得分: 1
当涉及到反转字符串时,StringBuilder#reverse
总是可用的:
public static void main(String[] args) {
List<String> wordList = Arrays.asList("hello", "Donald", "Trump");
System.out.println(reverseListAndItsElements(wordList));
}
public static List<String> reverseListAndItsElements(List<String> list) {
List<String> reversed = new ArrayList<>(list); // 创建一个副本
Collections.reverse(reversed);
return reversed.stream().map(s -> new StringBuilder(s).reverse().toString())
.collect(Collectors.toList());
}
输出:
[pmurT, dlanoD, olleh]
如果你不想处理列表:
public static void main(String[] args) {
String original = "hello,Donald,Trump";
String[] split = original.split(",");
String[] reversedSplit = new String[split.length];
IntStream.range(0, split.length).forEach(index -> {
reversedSplit[index] = new StringBuilder(split[index]).reverse().toString();
});
System.out.println(Arrays.toString(reversedSplit));
}
英文:
When it comes to reversing a String, StringBuilder#reverse
is always there:
public static void main(String[] args) {
List<String> wordList = Arrays.asList("hello", "Donald", "Trump");
System.out.println(reverseListAndItsElements(wordList));
}
public static List<String> reverseListAndItsElements(List<String> list) {
List<String> reversed = new ArrayList<>(list);//Create a copy
Collections.reverse(reversed);
return reversed.stream().map(s -> new StringBuilder(s).reverse().toString())
.collect(Collectors.toList());
}
Prints:
[pmurT, dlanoD, olleh]
And in case you do not want to mess with lists:
public static void main(String[] args) {
String original = "hello,Donald,Trump";
String[] split = original.split(",");
String[] reversedSplit = new String[split.length];
IntStream.range(0, split.length).forEach(index -> {
reversedSplit[index] = new StringBuilder(split[index]).reverse().toString();
});
System.out.println(Arrays.toString(reversedSplit));
}
答案2
得分: 0
尝试一下。
String[] input = {"hello", "Donald", "Trump"};
String[] reversed = new StringBuilder(String.join(",", input))
.reverse().toString().split(",");
System.out.println(Arrays.toString(reversed));
输出:
[pmurT, dlanoD, olleh]
英文:
Try this.
String[] input = {"hello", "Donald", "Trump"};
String[] reversed = new StringBuilder(String.join(",", input))
.reverse().toString().split(",");
System.out.println(Arrays.toString(reversed));
output:
[pmurT, dlanoD, olleh]
答案3
得分: -1
你可以使用以下代码:
public static List<String> reverseList(List<String> list) {
List<String> reversed = new ArrayList<>(list);
Collections.reverse(reversed);
return reversed.stream().collect(Collectors.toList());
}
英文:
you can use the following code
public static List<String> reverseList(List<String> list) {
List<String> reversed = new ArrayList<>(list);
Collections.reverse(reversed);
return reversed.stream().collect(Collectors.toList());
}
答案4
得分: -1
这段代码可以用于实现字符串数组的反转。有许多反转字符串的方法。其中一种是使用StringBuilder,我已经使用了。
public class Main {
static String[] arr = {"hello", "Donald", "Trump"};
public static void main(String[] args) {
String[] strA = reverseTheString(arr);
for (int i = 0; i < strA.length; i++) {
System.out.println("" + strA[i]);
}
}
public static String[] reverseTheString(String[] arr) {
String[] reverseArray = new String[arr.length];
int j = arr.length - 1;
for (int i = 0; i < arr.length; i++) {
StringBuilder sb = new StringBuilder(arr[i]);
reverseArray[j] = sb.reverse().toString();
j--;
}
return reverseArray;
}
}
英文:
This code can be used for String array reverse implementation. There are many ways of reversing a string. One is using StringBuilder I have used.
public class Main
{
static String[] arr={"hello", "Donald", "Trump"};
public static void main (String[]args)
{
String[] strA= reverseTheString(arr);
for(int i=0;i<strA.length;i++){
System.out.println(""+strA[i]);
}
}
// This is actual method for reverse an array elements.
public static String[] reverseTheString(String[] arr){
String[] reverseArray=new String[arr.length];
int j=arr.length-1;
for(int i=0;i<arr.length;i++)
{
StringBuilder sb=new StringBuilder(arr[i]);
reverseArray[j]=sb.reverse().toString();
j--;
}
return reverseArray;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论