【发布时间】:2015-06-09 07:27:41
【问题描述】:
昨天面试,面试官告诉我写一个计算数组长度的代码,不用Array类的length属性。
例如-
char[] array=new Scanner(System.in).nextLine().toCharArray();
// i have to write a code for calculating length of this array
//I can use any operator but use of library is restricted
here 给出的所有答案都使用字符串库。
【问题讨论】:
-
在像
c这样的语言中,我们使用while 循环将其与换行符\n匹配以中断,但如何处理对象。 -
使用 try catch 直到得到 ArrayIndexOutOfBounds 异常?
-
@Gosu。赞赏是的,我也试过这个,但这不是一个很好的编码风格来确定异常结果。
-
我希望有更好的方法来做到这一点(try/catch 看起来确实很愚蠢)。
-
@gosu Array 类如何评估其长度属性?因为这个
.class文件在 src 中不可用?
标签: java arrays string string-length