问题 单项选择题

下面程序的功能是统计字符串中“array”的个数,在程序的空白处应填入的正确选项是 public class FindKeyWords{  public static void main(String[]args) {   String text="An array is a data structur that stores a collection of"         +"values of the same type. You access each indMdual value"         +"through an integer index. For example,if a is an array"         +"of inergers,then a[i]is the ith integer in the array.";   int arrayCount=0;   int index=-1;   String arrayStr="array";   index=text.indexOf(arrayStr);   while(index>=0) {    ++arrayCount;    index+=arrayStr.length();    index=text.indexOf(arrayStr,index);   }   System.out.println("the text contains"+arrayCount+"arrays");  } }

A.<

B.=

C.<=

D.>=

答案

参考答案:D

解析: 程序中变量index为text.indexOf(arrayStr)的返回值,indexOf方法的原型为:public int indexOf(string str),如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这样的子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回-1。public int indexOf(string str,int fromIndex)从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引,如果没有指定字符串出现,则返回-1。本程序是判断子字符串是否出现,因此判断结果>=0即可。

选择题
多项选择题