我正在尝试做的是拥有一个全局变量类,并且该函数可以从/向/向其读/写。
我现在所拥有的是
import java.lang.*;
import lab1.Global;
public class SecondProgram {
public static void main ( String args[] ) {
System.out.println("hi ");
Global.b[0] = " zero ";
Global.b[1] = " One ";
Global.b[2] = " Two ";
Global.b[3] = " Three ";
}
}
我创建了一个用于存储全局变量的类
public class Global {
public static String a = "hi" ;
public static String [] b;
}
当然,重要的是数组的大小不是硬编码的常量,而是一个我可以在某个时候找到并插入的变量。
我希望您可以从代码中看到我正在尝试做的事情,并且您知道如何使其工作。
谢谢!
最佳答案
我认为这是一种更好的方法,可以帮助您入门。
import java.util.ArrayList;
public class SecondProgram {
private static ArrayList <String>file = new ArrayList();
public synchronized boolean writeFile(String str){
//wrtite file to your list
file.add(str);
return true;
}
public static void main(String args[]) {
//read file and use the synchronized method to write it to your list
}
}