- public class LianXi {
- public static void main(String[] args) {
- char[] chs = { 'a', 'b', 'c', 'd', 'b', 'e', 'f', };
- int num = search(chs, 'b');
- System.out.println(num);
- System.out.println(search(chs, 'h'));
- // System.out.println(search(null, 'f'));
- }
- public static int search(char[] chs, char ch) {
- if (chs == null)
- throw new IllegalArgumentException("输入不能为null");
- for (int i = 0; i < chs.length; i++) {
- if (ch==chs[i])
- return i;
- }
- return -1;
- }
- }
复制代码
==在这里作用于引用类型应该比较的是地址值。
问题1:那么都既然想比较字符是否相等;那么用equals行不行呢?怎么用呢?
问题2:用contains行不行呢?怎么做?
|
|