Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java从另一个类调用数组_Java_Arrays_Class - Fatal编程技术网

Java从另一个类调用数组

Java从另一个类调用数组,java,arrays,class,Java,Arrays,Class,我试图将对象的arrayList存储到一个文本文件中,该文件是从另一个类中实例化的数组ipvalue[]创建的。我的问题是,每当我尝试将数组从大型机类加载到阵列存储类时,它都会将整个数组显示为空输出。我想问题在于数组数据是在Mainframe方法中生成的,当我从另一个类调用数组时,它只是在初始化数组时获取数组。所以我的问题是如何在数组赋值后而不是初始化时访问它 new String[50] mainframe class: public class Mainframe extends j

我试图将对象的
arrayList
存储到一个文本文件中,该文件是从另一个类中实例化的数组
ipvalue[]
创建的。我的问题是,每当我尝试将数组从
大型机
类加载到
阵列存储
类时,它都会将整个数组显示为空输出。我想问题在于数组数据是在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);
   }
新字符串[50]
大型机类:
公共类大型机扩展了javax.swing.JFrame{
静态int ipcounter=0;
公共静态字符串[]ipvalue=新字符串[50];
/**
*字符串a[]创建新的表单大型机
*/
公共主机(){
试一试{
初始化组件();
此设置位置(500250);
净主(空);
Display.setText(净测试指示器);
Arraystorage.main(空);
Path Path=Path.get(“ipcounter.txt”);
File File=新文件(“ipcounter.txt”);
FileReader in=newfilereader(“ipcounter.txt”);
BufferedReader br=新的BufferedReader(in);
readline=Integer.parseInt(br.readline());
in.close();
ipcounter=读线;
if(ip.tracker.Nettest.status==true){
ipvalue[ipcounter]=Readipfromurl.inputLine;
//交叉检查();
Writer=null;
writer=new BufferedWriter(new OutputStreamWriter(
新文件输出流(“ipcounter.txt”),“utf-8”);
writer.write(String.valueOf(ipcounter+1));
writer.close();
System.out.println(ipvalue[ipcounter]);
ipcounter++;
arraystorage类:
公共类Arraystorage实现可序列化{
公共静态void main(字符串[]args)抛出
ClassNotFoundException{
ArrayList=新建ArrayList();

对于(int i=0;i简言之,您并不是在调用用数据填充数组的
Mainframe()
。在需要使用
ipvalue
数组之前,您应该在
main
方法中执行此操作。因此,只需在
Mainframe
实例上调用此方法:

public static void main(String[] args) throws  ClassNotFoundException { 

  ArrayList<String> list = new ArrayList<String>();
  Mainframe frame = new Mainframe();
  frame.Mainframe();
  for (int i=0; i<50;i++){
  list.add(Mainframe.ipvalue[i]);
  }
  ... // do the rest
publicstaticvoidmain(字符串[]args)抛出ClassNotFoundException{
ArrayList=新建ArrayList();
大型机框架=新的大型机();
frame.Mainframe();

对于(int i=0;i创建静态块初始值设定项:

public class Mainframe extends javax.swing.JFrame {

    static int ipcounter = 0;
    public static String[] ipvalue = new String [50];

    static {
        File file = new File("ipcounter.txt");
        FileReader in = new FileReader("ipcounter.txt");
        BufferedReader br = new BufferedReader(in);

        // Fill your ipvalue

        in.close();
    }

    // The rest of your class definition
    public MainFrame() {

    }

你们可能是对的,但我找到了另一个解决办法 在mainframe类中,我调用arraystorage的main方法 Arraystorage.main(空);
在我给数组赋值之后,它就可以工作了