【发布时间】:2017-09-25 18:20:39
【问题描述】:
我正在尝试将对象的arrayList 存储到从数组ipvalue[] 创建的文本文件中,该数组在另一个类中实例化。我的问题是,每当我尝试将数组从 Mainframe 类加载到 Arraystorage 类时,它会将整个数组显示为空输出。我想问题是数组数据是在 Mainframe 方法中生成的,当我从另一个类调用数组时,它只是在初始化时获取数组。所以我的问题是如何在赋值后访问数组,而不是在初始化时访问。
new String[50]
mainframe class:
public class Mainframe extends javax.swing.JFrame {
static int ipcounter = 0;
public static String[] ipvalue = new String [50];
/**
* String a[]Creates new form Mainframe
*/
public Mainframe() {
try {
initComponents();
this.setLocation(500,250);
Nettest.main(null);
Display.setText(Nettest.indicator);
Arraystorage.main(null);
Path path = Paths.get("ipcounter.txt");
File file = new File("ipcounter.txt");
FileReader in = new FileReader("ipcounter.txt");
BufferedReader br = new BufferedReader(in);
readline = Integer.parseInt(br.readLine());
in.close();
ipcounter = readline;
if(ip.tracker.Nettest.status == true){
ipvalue[ipcounter] = Readipfromurl.inputLine;
// crosscheck();
Writer writer = null;
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("ipcounter.txt"), "utf-8"));
writer.write(String.valueOf(ipcounter+1));
writer.close();
System.out.println(ipvalue[ipcounter]);
ipcounter++;
arraystorage class:
public class Arraystorage implements Serializable{
public static void main(String[] args) throws
ClassNotFoundException {
ArrayList<String> list = new ArrayList<String>();
for (int i=0; i<50;i++){
list.add(Mainframe.ipvalue[i]);}
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new
FileWriter("data.txt")));
for( int x = 0; x < list.size(); x++)
{
out.println(list.get(x));
}
out.close();
} catch (IOException ex) {
Logger.getLogger(Arraystorage.class.getName()).log(Level.SEVERE,
null, ex);
}
【问题讨论】: