题目: 如果输入的字符串是数组里的,输出“找到此字符串”,如果输入的字符串不是数组里的,就把它放到数组中,输出“此字符串已经被输入数组中”

解决方案 »

  1.   

    不要用数组,数组长度是不可变的
    用集合类,ArrayList等查找,匹配的话,可以用Scanner类,也可以用正则表达式
      

  2.   


    import java.util.*;public class TestScanner { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub ArrayList<String> contents = new ArrayList<String>();
    contents.add("aaa");
    contents.add("bbb");
    contents.add("ccc");

    Scanner scan = new Scanner(System.in);
    String temp = scan.next();

    if(contents.contains(temp)){
    System.out.println(temp + " is in the List");
    }else{
    System.out.println("put "+temp + " into the List");
    contents.add(temp);
    }
    }}
      

  3.   

    根据lz的意思,如果输入aaabbb是应该提示已经存在 还是add呢...