• java字符串集合


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

      二,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 == '
    '){
                    String s = b.toString();
                    b.delete(0, b.length());
                    if(list.contains(s)){
                        continue;
                    }
                    else{
                        list.add(s);
                        continue;
                    }
                }
                else if((char)temp == '
    '){
                    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
  • 相关阅读:
    MFC 简介
    C++使用thread类多线程编程
    C++中stack
    C++中头文件简介(stdio.h & chrono)
    别人写的很好Arduino教材
    Communicating to 2 SPI Slaves with USART & SPI ports on Atmega16U2
    HDU 2089 不要62(挖个坑=-=)
    HDU 3555 Bomb(数位DP)
    HDU 3480 Division(斜率优化+二维DP)
    HDU 3045 Picnic Cows(斜率优化DP)
  • 原文地址:https://www.cnblogs.com/buaaguzhanpeng/p/7148143.html
Copyright © 2020-2023  润新知