我试图反转一个数组,用户输入{hello,donald,trump}->pmurt,dlanod等,但它输出[null]

gmol1639  于 2021-07-12  发布在  Java
关注(0)|答案(4)|浏览(278)

关闭。这个问题需要更加突出重点。它目前不接受答案。
**想改进这个问题吗?**通过编辑这篇文章更新这个问题,使它只关注一个问题。

5个月前关门了。
改进这个问题
我是java新手,想反转用户输入的数组(包括字符),这样apple,banana就变成了ananab,elppa。但它输出[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);
        }
    }
}
db2dz4w8

db2dz4w81#

当要反转字符串时, 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);//Create a copy
        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));
}
isr3a4wc

isr3a4wc2#

试试这个。

String[] input = {"hello", "Donald", "Trump"};
String[] reversed = new StringBuilder(String.join(",", input))
    .reverse().toString().split(",");
System.out.println(Arrays.toString(reversed));

输出:

[pmurT, dlanoD, olleh]
ki0zmccv

ki0zmccv3#

您可以使用以下代码

public static List<String> reverseList(List<String> list) {
        List<String> reversed = new ArrayList<>(list);
        Collections.reverse(reversed);
        return reversed.stream().collect(Collectors.toList());
    }
rm5edbpk

rm5edbpk4#

此代码可用于字符串数组的反向实现。反转字符串的方法有很多种。一个是使用我用过的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;
 }
}

相关问题