java 字符串添加到集合_java字符串集合

importjava.io.FileReader;importjava.io.IOException;importjava.util.Iterator;importjava.util.Scanner;importjava.util.Vector;public classMain {//static Vector list ;//static Sets set;//private static Scanner scan;//private static Scanner scanstr;

public static void main(String[] args) throwsIOException{

Scanner scanstr;

Sets set= null;

Vector list = new Vector();

System.out.println("choose the way to initualize keyboard(1) or file(2),1 or 2:");

Scanner scans= newScanner(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;elselist.add(str);

}

set= newMyString(list);

}if(i == 2){

list=FileInput();

set= newMyString(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= newScanner(System.in);

i=scans.nextInt();switch(i){case 1:

set.getOrder();

set.myTostring();break;case 2:booleanbool1;

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:booleanbool2;

Vector list1 = new Vector();

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;elselist1.add(str2);

}

bool2=set.findSet(list1);if(bool2){

System.out.println("find set in strings");

}else{

System.out.println("404!");

}break;case 4:

Vector list2 = new Vector();

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;elselist2.add(str);

}

list2=set.interset(list2);

Iterator 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 FileInput() throwsIOException{

FileReader reader= new FileReader("in.txt");inttemp;

Vectorlist;

list=new Vector();

StringBuffer b= newStringBuffer();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();returnlist;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值