日期:2014-05-20 浏览次数:20611 次
没有可用分,求解决啊 import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; class liaot extends Frame { Socket socket=null; private TextArea text1; private TextField text2; //图形界面 liaot(){ text1 = new TextArea(); text2 = new TextField(); add(text1,"Center"); add(text2,"South"); event(); setBounds(300,300,300,300); setVisible(true); } //事件响应 public void event(){ addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); text2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try{ socket = new Socket("192.168.30.129",3334); }catch(Exception ee){} new Thread(new clientsend()).start(); new Thread(new clientrec()).start(); text2.setText(""); } }); } public static void main(String[] args)throws Exception { new liaot(); } //发送 class clientsend implements Runnable { public void run(){ byte[]buf = text2.getText().trim().getBytes(); String str = new String(buf,0,buf.length); System.out.println(str); text1.append(str); try{ OutputStream out = socket.getOutputStream(); out.write((str+"\r\n").getBytes()); out.flush(); }catch(Exception e){} } } //接收 class clientrec implements Runnable { public void run(){ byte[] buf = new byte[1024]; try{ InputStream in = socket.getInputStream(); in.read(buf); String str = new String(buf,0,buf.length); text1.append(str); }catch(Exception e){} } }
------解决方案--------------------
用MAP做的:
public static void count(String info) { Map<Character, Integer> map = new HashMap<Character, Integer>(); for (int i = 0; i < info.length(); i++) { Integer count = map.get(info.charAt(i)); if (count == null) { map.put(info.charAt(i), 1); } else { map.put(info.charAt(i), count + 1); } } for (Character c : map.keySet()) { System.out.println(c + " : " + map.get(c)); } }
------解决方案--------------------
你代码没问题,除了那个cope,还有这样效率太低了。