gpt4 book ai didi

java - 从单击的操作按钮获取变量数组java

转载 作者:行者123 更新时间:2023-12-02 07:42:02 26 4
gpt4 key购买 nike

我被编码困住了,我需要你的帮助。

好的,首先我在类下有一个数组变量。

我从 Excel 中获取值并将其放入节点名数组中,如下所示。我缩短了以下代码。

以下类Readexcel也在ConfigGenerator类下。

class ReadExcel {
private String inputFile;

public void setInputFile(String inputFile) {
this.inputFile = inputFile;
}

public void read() throws IOException {
File inputWorkbook = new File(inputFile);
Workbook nodes;
try {
nodes = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet nodessheet = nodes.getSheet(1);
String[] nodename = new String[nodessheet.getRows()];
for (int i = 1; i < nodessheet.getRows(); i++) {
int j = 0;
Cell x1a = nodessheet.getCell(0, i);
nodename[j] = x1a.getContents();
j++;
// System.out.println(nodename[j]);
}
} catch (BiffException e) {
// e.printStackTrace();
}
}
}

但我的问题是通过按钮操作访问此变量。

 public class ConfigGenerator extends javax.swing.JFrame {
public ConfigGenerator() {
initComponents();
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
private void generateActionPerformed(java.awt.event.ActionEvent evt) {
try {
Writer output = null;
for (String name: Generator.ConfigGenerator.ReadExcel.nodename[????]){
System.out.println(name);
}
output.close();
System.out.println("Your files has been written.");
} catch (IOException ex) {
Logger.getLogger(ConfigGenerator.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

我添加问号的部分是我最近两天尝试的问题,但无法从该数组中获取值。首先我需要获取数组的长度,其次是值:)非常感谢您提前提供的帮助。

好的,我通过添加整个部分的代码来编辑问题。

最佳答案

也许你的意思是你想使用nodename.length获取数组长度,但我的代码有两个问题:

首先,您将 nodename 作为 action 的本地数组进行访问,然后将其作为 ConfigGenerator 类的 static 变量进行访问,这意味着您正在访问两个不同的数组。

此外,您将 nodename 数组作为 static 变量访问(尽管 .class 也不是必需的),但您提到了它作为“数组变量”,这意味着您需要首先创建类 ConfigGenerator 的新实例,初始化数组 nodename,然后您可以在其他类中使用它。

关于java - 从单击的操作按钮获取变量数组java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11451298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com