java字符串集合

  一,java的接口跟C语言所能作到的相比确实是让人眼前一亮的东西。利用接口能够将多种东西放到一块儿,在编程过程当中就能省略掉相同类的不少重复代码,将代码进行分类别的,统一的处理。java

  二,java中的字符串处理,java中的字符串并不像C语言那样利用二维数组来进行操做,而是对应了String这个对象,能够用new的方法建立一个字符串对象,而这个字符串对象有不少方法能够直接调用,这样直接对对象进行操做,显得很是方便。
神奇的字符串操做方法
str.length()
str.indexOf(),indexOf有不一样参数的方法调用,对应不一样的方法。
str.equals("XXX")字符串比较方法,java中的字符串比较与c语言相似,不可以使用==进行比较
str.charAt(int num)
访问字符串对应num上的字符
StringBuffer是能够改变的字符串,能够用append()等String所没有的方法;
java对字符串的操做与C语言相比,由于已经有方法能够直接调用了,就像C语言别人已经把函数给你写好了,因此使用起来只须要调用就行了
  三,java中的容器,java中有不少的容器,容器在我理解来能够用来存放对象所使用的,老师所给的样例中所给是Vector容器,是一个向量类,能够动态调整数据大小,(这个向量彷佛指的不是数学意义上的向量),其余的好比Linklist和Arraylist等,都是能够动态调整大小的,这是C语言所不具有的。并且向量类有不少方法,声明和使用起来都比较方便
 
 
前面写java程序的时候,老师不少会给出样例,写起来的时候感受还能够,因此此次写字符串集合操做方法的时候,显得有些杂乱。
写Mystring类的时候尚未太大区别,直接就定义了一个stringlist向量容器,利用构造方法进行构造后,调用其它方法进行操做便可。可是当我尝试着将这些方法调用的时候,便会有不少细节反面不理解,eclipse进行相应的报错;
1,对不少量进行声明的时候,可能编程过程当中规范不是很好,用了eclipse的自动纠正功能后,将个人代码中在main中定义的对象转化成类的属性并加上了static修饰,查阅了static相关资料后发现,static至关于全局变量,在main程序运行前已经存在,后来在main中从新写了相应的对象定义方法,注释掉了eclipse的自动纠正模块,所幸程序正常.
2.java迭代器的使用,是java容器的特色,可以更安全的对向量容器里面的内容进行遍历,但彷佛本人目前还不会使用迭代器进行双重循环,因此仍然用.length()方法进行遍历。
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Vector;

public class Main {
    //static Vector<String> list ;
    //static Sets set;
    //private static Scanner scan;
    //private static Scanner scanstr;
    public static void main(String[] args) throws IOException{
        Scanner scanstr;
        Sets set = null;
        Vector<String> list = new Vector<String>();
        System.out.println("choose the way to initualize keyboard(1) or file(2),1 or 2:");
        Scanner scans = new Scanner(System.in);
        int i = scans.nextInt();
        if(i == 1){
            System.out.println("type \" \" to quit");
            while(scans.hasNextLine()){
                String str = scans.nextLine();
                if(str.equals(" ")){
                    break;
                }
                if(list.contains(str)) continue;
                else list.add(str);
            }
            set = new MyString(list);
        }
        if(i == 2){
            list = FileInput();
            set = new MyString(list);
        }
        System.out.println("choose how to operate Strings");
        System.out.println("getOrder(1),findString(2),findSet(3)");
        System.out.println("interset(4),times(5),add(6),delete(7),print(8)");
        while(scans.hasNextInt()){
            scanstr = new Scanner(System.in);
            i = scans.nextInt();
            switch(i){
            case 1:
                set.getOrder();
                set.myTostring();
                break;
            case 2:
                boolean bool1;
                System.out.println("type the string to find");
                String strtemp = scanstr.nextLine();
                bool1 = set.findString(strtemp);
                if(bool1){
                    System.out.println("find"+strtemp+"in strings");
                }
                else{
                    System.out.println("404!");
                }
                break;
            case 3:
                boolean bool2;
                Vector<String> list1 = new Vector<String>();
                System.out.println("type the strings set to find");
                System.out.println("type \" \" to quit");
                while(scanstr.hasNextLine()){
                    String str2 = scanstr.nextLine();
                    if(str2.equals(" ")){
                        break;
                    }
                    if(list1.contains(str2)) continue;
                    else list1.add(str2);
                }
                bool2 = set.findSet(list1);
                if(bool2){
                    System.out.println("find set in strings");
                }
                else{
                    System.out.println("404!");
                }
                break;
            case 4:
                Vector<String> list2 = new Vector<String>();
                System.out.println("type the strings set to find");
                System.out.println("type \" \" to quit");
                while(scanstr.hasNextLine()){
                    String str = scanstr.nextLine();
                    if(str.equals(" ")){
                        break;
                    }
                    if(list2.contains(str)) continue;
                    else list2.add(str);
                }
                list2 = set.interset(list2);
                Iterator<String> iter = list2.iterator();
                while(iter.hasNext()){
                    String strs = iter.next();
                    System.out.println(strs);
                }
                break;
            case 5:
                System.out.println("type the times to caculate");
                String strtemp1 = scanstr.nextLine();
                int j = 0 ;
                j = set.times(strtemp1);
                System.out.println(strtemp1+" "+j);
                break;
            case 6:
                System.out.println("type the string to add");
                String strtemp2 = scanstr.nextLine();
                set.add(strtemp2);
                set.myTostring();
                break;
            case 7:
                System.out.println("type the string to delete");
                String strtemp3 = scanstr.nextLine();
                set.delete(strtemp3);
                set.myTostring();
                break;
            }
        }
        scans.close();
    }
    public static Vector<String> FileInput() throws IOException{
        FileReader reader = new FileReader("in.txt");
        int temp;
        Vector<String> list;
        list =new Vector<String>();
        StringBuffer b = new StringBuffer();
        while((temp = reader.read()) != -1){
            if((char)temp == '\r'){
                String s = b.toString();
                b.delete(0, b.length());
                if(list.contains(s)){
                    continue;
                }
                else{
                    list.add(s);
                    continue;
                }
            }
            else if((char)temp == '\n'){
                continue;
            }
            else{
                b.append((char)temp);
            }
        }
        reader.close();
        return list;
    }
}
Main
import java.util.Iterator;
import java.util.Vector;

public class MyString implements Sets{
    protected Vector<String> stringlist;
    @SuppressWarnings("unchecked")
    public MyString(Vector<String> strcopy){
        stringlist = new Vector<String>();
        stringlist = (Vector<String>)strcopy.clone();
    }
    public boolean findSet(Vector<String> strs){
        Iterator<String> iter = strs.iterator();
        while(iter.hasNext()){
            String str = (String)iter.next();
            if(stringlist.contains(str)){
                continue;
            }
            else{
                return false;
            }
        }
        return true;
        
    }
    public boolean findString(String s){
        return stringlist.contains(s);
    }
    public Vector<String> getOrder(){
        String s1 , s2;
        for(int i = 0 ; i < stringlist.size() ; i ++){
            for(int j = 0 ; j < stringlist.size() - i -1; j++){
                s1 = stringlist.get(j);
                s2 = stringlist.get(j+1);
                if(s1.compareTo(s2) > 0){
                    stringlist.set(j+1 , s1);
                    stringlist.set(j , s2);
                }
            }
        }
        return stringlist ; 
    }
    public Vector<String> interset(Vector<String> strs){
        Vector<String> temp = new Vector<String>();
        Iterator<String> iter = strs.iterator();
        while(iter.hasNext()){
            String str = (String)iter.next();
            if(stringlist.contains(str)){
                temp.add(str);
            }
        }
        return temp;
    }
    public int times(String s){
        int time = 0 ;
        Iterator<String> iter = stringlist.iterator();
        while(iter.hasNext()){
            String str = (String)iter.next();
            if(str.indexOf(s) == -1){
                continue;
            }
            else{
                time ++ ;
            }
        }
        return time;
    }
    public void add(String s){
        stringlist.add(s);
    }
    public void delete(String s){
        if(stringlist.contains(s)) stringlist.remove(s);
        else System.out.println(s+" not exists");
    }
    public void myTostring(){
        Iterator<String> iter = stringlist.iterator();
        while(iter.hasNext()){
            String str = (String)iter.next();
            System.out.println(str);
        }
    }
    
}
MyString
相关文章
相关标签/搜索