-1

The problem is I have a for loop in android , and its seems to be running in the reverse order.

Here is the code :

for(i=0;i<strlent;i++)
{
    //ch=st.charAt(i);
//disp(String.valueOf(ch));
disp(String.valueOf(i));
}

I have a string and would like to get each characters out of it, but if I feed in "babe" it runs e-b-a-b. I checked the i value and it runs as 3-2-1-0. I seriously don't understand why it behaves this way.

This is my disp function

public void disp(String st) // this function is used to check with message boxes 
{
    AlertDialog.Builder adb = new Builder(this);
    adb.setTitle("Testing");
    adb.setMessage(st);
    adb.show();
}
4

2 回答 2

1
String str = "Let Me Reverse";

System.out.println("\nIn order..");

for(int i = 0; i < str.length(); i++){
    System.out.print(str.substring(i, i + 1));
}

System.out.println();

for(int i = 0; i < str.length(); i++){
    System.out.print(str.charAt(i));
}

System.out.println();

for(char ch : str.toCharArray()){
    System.out.print(ch);
}

System.out.println("\nIn reverse order..");

for(int i = str.length() - 1; i >= 0; i--){
    System.out.print(str.charAt(i));
}
于 2013-08-29T09:05:11.423 回答
0
String name = "Hello";

for(int i=name.length()-1;i>=0;i--){
     System.out.println(name.charAt(i));
}
于 2013-08-29T09:21:54.213 回答