- public class getStr {
-
- public static void main(String[] args) {
-
- String[] arr = null;
- //当数组为null
- getStrTest(arr,"c");
- //当字符存在
- arr = new String[]{"a","b","c","d","e","f","c"};
- System.out.println(getStrTest(arr,"c"));
- //当字符不存在
- System.out.println(getStrTest(arr,"h"));
- }
- private static int getStrTest(String[] arr, String str) {
- if(arr == null){
- throw new IllegalArgumentException("数组为null");
- }
- for(int i=0;i<arr.length;i++){
- if(str.equals(arr[i])){
- return i;
- }
- }
- return -1;
- }
复制代码
|