【发布时间】:2013-11-15 19:04:00
【问题描述】:
我有这个 ArrayList 的数据填充了我打开的文件中的数字,我希望能够通过输入样本的大小和我想要的样本数从中生成随机数。所以我有以下...
private JTextField jtfN = new JTextField();// where i enter the amount of samples
private JTextField jtfn = new JTextField();// where i enter the size of samples
private ArrayList< Double> data = new ArrayList< Double>();
我有两个文本字段,一个名为 jtfn(样本大小),一个名为 jtfN(样本数量) 这是我输入值的地方。然后我有一个名为 jbtnGenerate 的按钮,当我单击时,我希望它从上面输入的 jtfn 和 jtfN 的数据中生成随机数,然后将其放入名为 jta 的 TextArea 中
这就是我打开文件并将数字添加到数据中的地方
if( jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION ) {
String file = jfc.getSelectedFile().getPath();
String line = null;
String[] ch;
try {
FileReader fr = new FileReader( file);
BufferedReader br = new BufferedReader(fr);
data.clear();
while( (ligne=br.readLine())!=null ) {
ch = line.split( ";" );
for (int i = 0; i < ch.length; i++) {
data.add( Double.parseDouble(ch[i]) );
}
}
br.close();
}
catch( IOException ioe ) {
}
}
这是我希望操作发生的“生成”按钮的监听器。
jbtnGenerer.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae ) {
try {
int sampSize = Integer.parseInt(jtfn.getText());
int nSamples = Integer.parseInt(jtfN.getText());
double samps[][] = new double[nSamples][sampSize];
for(int i = 0 ; i < nSamples; i++){
for(int j = 0 ; j < sampSize; j++)
samps[i][j] = (Double) data.toArray()[ rng.nextInt() % data.size() ];
}
jta.append(samps.toString());
} catch (NumberFormatException e) {
}
}
});
感谢您的帮助
【问题讨论】:
-
我绝对不会那样命名你的变量...很容易产生错误。
-
我完全被 jtfn 和 jtfN 的意思分心了。
-
只是为了澄清,看起来你想从数据中提取 n 个双精度样本,对吧?
-
@Peter,我猜是 Java 文本字段,
n和N是它们捕获的变量。但这并不正确...... -
它们是我输入值的文本字段。 jtfn 是我设置样本大小的地方, jtfN 是我设置我想要采集的样本数量的地方。这些数字取自作为双精度数组列表的数据
标签: java