字符串数组反向Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22282845/
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
String array reverse Java
提问by
I am trying to reverse all the strings in an array in java, but seem to over write all of them with the first.
我试图在 java 中反转数组中的所有字符串,但似乎用第一个覆盖了所有字符串。
private static void palindrome(String[] s) {
int flag=0;
String reverse;
for(int i=0;i<n;i++) // n is declared globally as number of strings
{
reverse="";
for (int j=s[i].length()-1;i>=0;i--)
reverse=reverse+s[i].charAt(j);
if(s[i].equals(reverse))
{
System.out.println(s[i]);
flag=1;
}
}
if(flag==0)
System.out.println("There are no palindromic strings");
}
回答by nosid
Looks like you used i
instead of j
in the inner loop.
看起来您使用i
而不是j
在内循环中。
for (int j=s[i].length()-1;j>=0;j--)
reverse=reverse+s[i].charAt(j);
回答by óscar López
This line looks wrong:
这一行看起来不对:
for (int j = s[i].length()-1; i >= 0; i--)
It should be:
它应该是:
for (int j = s[i].length()-1; j >= 0; j--)
In other words: the indexes in the inner loop are mistaken, they should be using j
instead of i
. As a side comment - here's a simpler way to reverse a string:
换句话说:内循环中的索引是错误的,它们应该使用j
而不是i
. 作为旁注 - 这是反转字符串的更简单方法:
reverse = new StringBuilder(s[i]).reverse().toString();
回答by davecroman
Your inner loop should be:
你的内部循环应该是:
for (int j=s[i].length()-1; j>=0; j--){
reverse=reverse+s[i].charAt(j);
// ... //
}
回答by Hichamov
Why are you using this:
你为什么使用这个:
for (int j=s[i].length()-1;i>=0;i--)
You should use this instead:
你应该改用这个:
for (int j=s[i].length()-1;j>=0;j--)
回答by eatSleepCode
for (int j=s[i].length()-1; j >=0; j-- )
### ###
you need to correct your inner loop. Instead of i
you need to use loop variable j
.
你需要纠正你的内循环。而不是i
您需要使用循环变量j
。
回答by duffymo
I'd advise you to break even this small problem into chunks.
我建议你把这个小问题分解成块。
Write a separate method that reverses a single String. Get that working and tested. Only then should you iterate over a collection or array and apply it to each member.
编写一个单独的方法来反转单个字符串。得到它的工作和测试。只有这样,您才应该遍历集合或数组并将其应用于每个成员。
You'd have an easier time debugging this if you did it in smaller steps. The fact that your string reversing method was borked would have been apparent right away.
如果您以较小的步骤进行调试,则调试起来会更轻松。您的字符串反转方法被取消的事实将立即显而易见。
You shouldn't write stuff to System.out like that from methods.
你不应该像从方法那样向 System.out 写东西。
public static String reverse(String s) {
StringBuilder reversed = new StringBuilder(s.length());
// reverse one string here
return reversed.toString();
}
public static String [] reverseAll(String [] originals) {
String [] reversed = new String[originals.length];
for (int i = 0; i < originals.length; ++i) {
reversed[i] = reverse(originals[i]);
}
return reversed;
}
回答by Opster Elasticsearch Pro-Vijay
Try these steps:
尝试以下步骤:
String[] strDays = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday"};
List<String> list = Arrays.asList(strDays);
Collections.reverse(list);
strDays = (String[]) list.toArray();