可能重複:
String length without using length() method in java如何在沒有字符串API方法的情況下在Java中獲取字符串的長度?
我需要得到一個字符串的長度(不存在的字符),使用for
循環在Java和沒有使用像length()
任何方法。
import java.io.*;
import java.util.*;
public class reversestring
{
public static void main(String arg[])throws IOException
{
String s;
int i=0,j=0,k=0;
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter ur string : ");
s=in.readLine();
char ar[]=s.toCharArray();
System.out.println("Length of the string is : ");
for(j=ar[i];j!='\0';i++)
{
k++;
}
System.out.println(+k);
}
}
我寫了這個程序,但我沒有得到答案。它有什麼問題?
Java``String`對象是**不** 0終止! – 2011-02-10 14:42:25
由於你「不能」使用`length()`..這個應該被標記爲「家庭作業」嗎? – Nanne 2011-02-10 14:44:34
@Nanne:根據[this](http://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions)作業標籤(連同其他元標籤)不鼓勵。 – 2011-02-10 14:47:07