我遇到了从Java中的“二维数组”中检索值的问题。我知道Java中没有二维数组这样的东西,但是对我来说是裸露的。我是Java的新手,正在为我的班级开发一个项目。我应该将信息从jTextField提交到数组,然后将存储在数组中的信息备份到摘要报告中。这是我到目前为止所做的:
public class KETTask1UI extends javax.swing.JFrame {
//Creates the "2d array" to store minutes worked and payment values
int[][] paymentArray = new int [20][2];
/**
* Creates new form KETTask1UI
*/
public KETTask1UI() {
initComponents();
}
private void runreportActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
for (int i=0; i<paymentArray.length; i++) {
for (int j=0; j<paymentArray[i].length; j++) {
//Blanks out the displayTextArea
displayTextArea.setText(" ");
//Displays the contents of the array in the displayTextArea
displayTextArea.append("History of minutes and payment entered:\n");
displayTextArea.append(String.valueOf(paymentArray[i][j]));
}
}
}
private void minutesActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void quitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int min = Integer.parseInt(minutes.getText());
int pay = Integer.parseInt(payment.getText());
for (int i=0; i<paymentArray.length; i++) {
for (int j=0; j<paymentArray[i].length; j++) {
//Sets the amount of minutes worked and payment to the array
paymentArray[i][0] = min;
paymentArray[i][1] = pay;
//display a message to the user to let them know the values that they've entered.
displayTextArea.append("******************\nRaw Tutoring Earnings Data\n\nMinutes Earnings\n");
displayTextArea.append(String.valueOf(paymentArray[i][j]));
displayTextArea.append("\n\n******************");
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new KETTask1UI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextArea displayTextArea;
private javax.swing.JButton enterButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField minutes;
private javax.swing.JTextField payment;
private javax.swing.JLabel paymentLabel;
private javax.swing.JButton quit;
private javax.swing.JButton runreport;
private javax.swing.JLabel timeLabel;
// End of variables declaration
}
现在,当我点击进入按钮时,它给我的全部内容如下(30分是最后一分钟,20分是最后一个付款号码):
******************
Raw Tutoring Earnings Data
Minutes Earnings
30
************************************
Raw Tutoring Earnings Data
Minutes Earnings
20
此消息在textArea中重复20次。如果我输入更多数据,它就会对新数字做同样的事情。
我需要它看起来像这样:
******************
Raw Tutoring Earnings Data
Minutes Earnings
20 30
******************
当我输入新值并再次按下回车按钮时,我需要它来显示这样的更新列表:
******************
Raw Tutoring Earnings Data
Minutes Earnings
20 30
30 15
******************
非常感谢任何帮助。谢谢。
答案 0 :(得分:2)
2D数组确实存在于Java中,您正确使用它们。但是,在您的第二个代码块中,您使用paymentArray的第一个维度的长度,您使用'j'循环。使用它来访问第二个维度的长度。
for (int i = 0; i < paymentArray.length; i++) {
for (int j = 0; j <paymentArray[i].length; j++) {
displayTextArea.append(String.valueOf(paymentArray[i][j]));
}
}
答案 1 :(得分:0)
您的代码有点不正确。将其更改为
for (int i = 0; i < paymentArray.length; i++) {
for (int j = 0; j <paymentArray[i].length; j++) {
displayTextArea.append(String.valueOf(paymentArray[i][j]));
}
}