JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java SE 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 如何清除 StringBuffer
jansmin

嗯...舒服



發文: 30
積分: 0
於 2003-08-20 12:53 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我宣告了一個StringBuffer變數 sb,
StringBuffer sb=new StringBuffer("");

經過一些運算後, 內容已有值, 我該如何將它清成空的? 我試過下列方法, 會有Exception耶,
sb=sb.delete(0, sb.toString().length() - 1);

我查過API好像沒其他method可用了, 不能像String直接指定嗎? 如下列:
String s=new String("12345");
...經過一些運算, s已有值....
s="";


reply to postreply to post
作者 Re:如何清除 StringBuffer [Re:jansmin]
darkmomo





發文: 8
積分: 0
於 2003-08-20 13:02 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
delete 可以嗎?
我自已幾乎都是再new一個出來 ^^b


reply to postreply to post
作者 Re:如何清除 StringBuffer [Re:jansmin]
snpshu

Norman

版主

發文: 1073
積分: 5
於 2003-08-20 13:03 user profilesend a private message to usersend email to snpshureply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
String s=new String("12345");
s="";
他並不是清掉s , 他不是解譯產生一個新的String object嗎? Smile


reply to postreply to post
作者 Re:如何清除 StringBuffer [Re:jansmin]
Duncan

還隱隱作痛

版主

發文: 7816
積分: 39
於 2003-08-20 13:59 user profilesend a private message to usersend email to Duncanreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
jansmin wrote:
我宣告了一個StringBuffer變數 sb,
StringBuffer sb=new StringBuffer("");

經過一些運算後, 內容已有值, 我該如何將它清成空的? 我試過下列方法, 會有Exception耶,
sb=sb.delete(0, sb.toString().length() - 1);

我查過API好像沒其他method可用了, 不能像String直接指定嗎? 如下列:
String s=new String("12345");
...經過一些運算, s已有值....
s="";


sb.delete(0, sb.length());


reply to postreply to post

給我
辣味豆腐 其餘免談
作者 Re:如何清除 StringBuffer [Re:jansmin]
jansmin

嗯...舒服



發文: 30
積分: 0
於 2003-08-20 16:21 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
還是不行耶, 目的是要去讀一個文字檔, 每行兩個值, 以 Tab 隔開, 將結果塞在一個陣列.

文字檔 planners.txt 如下:
1A  111
1B  222
1D  333
1F  444

我的程式如下:

<%@ page contentType="text/html;charset=Big5" %>
<%@ page import="java.util.*,java.io.*" %>

<HTML>
<HEAD>
<TITLE>Quick Picks Test</TITLE>
</HEAD>
<BODY>

<%
String v_quick_picks[][]=new String[100][2];
int ser=0, chr=0;
StringBuffer sb=new StringBuffer("");

FileReader fr=new FileReader(pageContext.getServletContext().getRealPath("/QuickPicks/planners.txt"));

while ((chr=fr.read()) != -1)
{
if (chr == 13)
{
v_quick_picks[ser][1] = sb.toString();
sb=sb.delete(0, sb.length()-1);
ser++;
continue;
}
else if (chr == 9)
{
v_quick_picks[ser][0] = sb.toString();
sb=sb.delete(0, sb.length()-1);
continue;
}
else
{
sb.append( (char)chr );
}
}
fr.close();

for (int i=0; i<=v_quick_picks.length; i++)
{
out.write("<br>"+ String.valueOf(i)+" "+ v_quick_picks[i][0] + "=" + v_quick_picks[i][1] );
}
%>

</BODY>
</HTML>

Exception 發生在 sb=sb.delete(0, sb.length()-1);
我是希望把 sb 清成空的再重新塞值,
不知道這樣描述,各位前輩看得懂嗎?


browser edited on 2003-08-20 16:33
reply to postreply to post
作者 Re:如何清除 StringBuffer [Re:jansmin]
darkmomo





發文: 8
積分: 0
於 2003-08-20 17:37 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
也許…用stringtokenizer會比較好一點 省得自已抓token
而清空真的不行就直接用sb = new StringBuffer()吧
(用stringtokenizer連清空都省下來了)


reply to postreply to post
作者 Re:如何清除 StringBuffer [Re:jansmin]
Duncan

還隱隱作痛

版主

發文: 7816
積分: 39
於 2003-08-20 21:24 user profilesend a private message to usersend email to Duncanreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
jansmin wrote:
還是不行耶, 目的是要去讀一個文字檔, 每行兩個值, 以 Tab 隔開, 將結果塞在一個陣列.

Exception 發生在 sb=sb.delete(0, sb.length()-1);
我是希望把 sb 清成空的再重新塞值,
不知道這樣描述,各位前輩看得懂嗎?


我寫的很清楚,sb.delete(0, sb.length());
為什麼要減一呢?如果一開始就讀到換行,sb 還是空的,sb=sb.delete(0, sb.length()-1); 當然是 exception 囉!


reply to postreply to post

給我
辣味豆腐 其餘免談
作者 Re:如何清除 StringBuffer [Re:jansmin]
jansmin

嗯...舒服



發文: 30
積分: 0
於 2003-08-21 00:17 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
喔.....因為我查 API :

delete(int start, int end) Removes the characters in a substring of this StringBuffer.

一直以為 end 是要放結束的位置所以才用長度減一(因為從0算起), 但我把減一拿掉依然會有 exception, 最後我才發現問題應該在最後一段:
for (int i=0; i<=v_quick_picks.length; i++)

v_quick_picks.length會等於我宣告時的長度100, 但其實讀檔案時只有10行, 所以讀到第11行時, 就會 java.lang.ArrayIndexOutOfBoundsException !

所以, 我再問一個問題, 宣告陣列時可不可以先不指定大小, 由程式動態讀取檔案後再決定陣列的大小? 這樣 v_quick_picks.length 才會是真正的長度, 我這樣想對不對啊?


reply to postreply to post
作者 Re:如何清除 StringBuffer [Re:jansmin]
Duncan

還隱隱作痛

版主

發文: 7816
積分: 39
於 2003-08-21 00:40 user profilesend a private message to usersend email to Duncanreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
jansmin wrote:
所以, 我再問一個問題, 宣告陣列時可不可以先不指定大小, 由程式動態讀取檔案後再決定陣列的大小? 這樣 v_quick_picks.length 才會是真正的長度, 我這樣想對不對啊?


不確定資料量的場合最適合 java.util.Collection 出場了,你可以用 collection 把 parse 出來的每一行存起來,然後再由 collection 取得 array。


reply to postreply to post

給我
辣味豆腐 其餘免談
作者 Re:如何清除 StringBuffer [Re:jansmin]
jansmin

嗯...舒服



發文: 30
積分: 0
於 2003-08-22 17:50 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
能不能給我幾行範例參考參考.....

reply to postreply to post
作者 Re:如何清除 StringBuffer [Re:jansmin]
anthonycs



版主

發文: 1195
積分: 6
於 2003-08-23 00:55 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
jansmin wrote:
能不能給我幾行範例參考參考.....


Collection的觀念及範例程式可以找Thinking in Java 2 中文版 by 侯捷
相關內容在第九章


reply to postreply to post
作者 Re:如何清除 StringBuffer [Re:jansmin]
felixjen





發文: 18
積分: 0
於 2003-08-28 10:24 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
StringBuffer.setLength(0);

reply to postreply to post
» JWorld@TW »  Java SE 討論區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8