一,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 == '\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; } }

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); } } }