标签:细节 集合 rate one while for quit 自动 UI
一,java的接口跟C语言所能做到的相比确实是让人眼前一亮的东西。利用接口可以将多种东西放到一起,在编程过程中就能省略掉相同类的很多重复代码,将代码进行分类别的,统一的处理。
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); } } }
标签:细节 集合 rate one while for quit 自动 UI
原文地址:http://www.cnblogs.com/buaaguzhanpeng/p/7148143.html