您的位置:首页 > 其它

定义一个字符数组,在其中找出指定的字符,并且返回他的索引值

2016-08-23 00:37 274 查看
class  Index

{
public static void main(String[] args) 
{

char []s=new char[]{'a','b','c','d'};

// char[] s=new char[10];
System.out.println( ArrayIndex(s,'d'));
System.out.println("Hello World!");
}
public static int ArrayIndex(char[] s,char character)
{
if(s==null)
{
throw new IndexException("数组为空");
}

else
{
int sign=-1;
for(int i=0;i<s.length;i++)
{
if(s[i]==character)
{
return i;
}
}
return sign;
}
}

}

class IndexException extends RuntimeException

{
IndexException(String  message)
{
super(message);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐