I am trying to reverse an array the user inputs like {hello, Donald, Trump} -> pmurT, dlanoD etc but it outputs [null]

huangapple go评论79阅读模式
英文:

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&lt;String&gt; wordList = Arrays.asList(&quot;hello&quot;, &quot;Donald&quot;, &quot;Trump&quot;);
		System.out.println(reverseListAndItsElements(wordList));
	}

	public static List&lt;String&gt; reverseListAndItsElements(List&lt;String&gt; list) {
		List&lt;String&gt; reversed = new ArrayList&lt;&gt;(list);//Create a copy
		Collections.reverse(reversed);
		return reversed.stream().map(s -&gt; 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 = &quot;hello,Donald,Trump&quot;;
	String[] split = original.split(&quot;,&quot;);
	String[] reversedSplit = new String[split.length];
	IntStream.range(0, split.length).forEach(index -&gt; {
		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 = {&quot;hello&quot;, &quot;Donald&quot;, &quot;Trump&quot;};
String[] reversed = new StringBuilder(String.join(&quot;,&quot;, input))
    .reverse().toString().split(&quot;,&quot;);
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&lt;String&gt; reverseList(List&lt;String&gt; list) {
        List&lt;String&gt; reversed = new ArrayList&lt;&gt;(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={&quot;hello&quot;, &quot;Donald&quot;, &quot;Trump&quot;};
  public static void main (String[]args)
 {
String[] strA=  reverseTheString(arr);

for(int i=0;i&lt;strA.length;i++){
    System.out.println(&quot;&quot;+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&lt;arr.length;i++)
  {
      StringBuilder sb=new StringBuilder(arr[i]);
    reverseArray[j]=sb.reverse().toString();
    j--;
 }
return reverseArray;
 }
}

huangapple
  • 本文由 发表于 2020年8月19日 19:06:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63485648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定