我想把如:(23,43,89,90)这些值放到一个字段中去?怎么放?还有下次再追加时前面的不变,只在后面追加,中间用,隔开???
取的时候怎么一个一个的取出来???
------解决方案--------------------
没明白LZ的意思
append, split就可以,可以参考
StringBuffer sb = new StringBuffer();
sb.append( "23 ")
.append( ", ").append( "43 ")
.append( ", ").append( "89 ")
.append( ", ").append( "90 ") ...
String s1 = "( " + sb.toString() + ") ";
String[] s2 = sb.toString().split( ", ");
for (int i=0; i <s2.length; i++) {
System.out.println(s2[i]);
}
------解决方案--------------------
当然也可以使用StringBuilder,你可以把楼上的例子修改了run一下就应该很清楚了把
------解决方案--------------------
用split就可以。qybao(阿宝)支持。
------解决方案--------------------
String ddd= " ";
ddd=ddd+ "123 "+ ", ";
ddd=ddd+ "123 "+ ", ";
ddd=ddd+ "123 "+ ", ";
ddd=ddd+ "123 "+ ", ";
String[] array=ddd.split( ", ")
for(int i;i <array.length;i++)
System.out.println(ärray[i]);
------解决方案--------------------
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class StringBuffer1 extends JFrame {
/** Creates a new instance of StringBuffer */
byte buf []=new byte[1024];
public StringBuffer1()
{
super( "添加数字 ");
try
{
final RandomAccessFile raf=new RandomAccessFile( "yisheng.txt ", "rw ");
raf.write( "(20,30) ".getBytes());
Container c=getContentPane();
final JTextField jf=new JTextField(20);
JButton jb=new JButton( "添加 ");
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try
{
raf.seek(0);
byte buf[]=new byte[1024];
int len=raf.read(buf);
String str=new String(buf,0,len-1);
str=str+ ", "+jf.getText()+ ") ";
raf.seek(0);
raf.write(str.getBytes());
}
catch(Exception d)
{
d.printStackTrace();
}
}
});
c.add(jf,BorderLayout.NORTH);
c.add(jb,BorderLayout.SOUTH);
setSize(300,300);
setVisible(true);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String args[])
{
StringBuffer1 sb=new StringBuffer1();
sb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
------解决方案--------------------
TO:yifuzhiming(haha)
String用加号连接时写成:
String ddd= " "+ "123 "+ ", "+ "123 "+ ", "+ "123 "+ ", "+ "123 "+ ", ";这样是没有问题的。但按你的写法不太好,你的那种写法是很耗内存的,每加一次都人重新开发一块内存空间。