如何将非静态变量传递给main方法?

tuwxkamq  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(321)

我对java有问题。我想通过考试 integer 基于 Vector 到节目的主要部分。问题是向量不能 static 因为它包含值​​程序修改的。
我怎样才能得到向量呢 main 方法?
此向量用于通过串行向arduino发送激活状态。

public int [] vector(){
    SwingWorker<int [], Integer> swingWorker1 = new SwingWorker<int [], Integer>(){
        @Override
        public int [] doInBackground()throws IOException, InterruptedException{
     int[] vec={manu,scarico,carico,co2,neon,led,res,filtro,g_luci,aria,uv,intera,decimale};  //initializing array  
   return vec;  
}

    };swingWorker1.execute();

    return null;
}
public static void main(String args[]) {
    SwingWorker<Void, Integer> swingWorker = new SwingWorker<Void, Integer>(){
        @Override
        public Void doInBackground()throws IOException, InterruptedException{
            while(true){
                int a;
                int []vec= new int[13] ;
                int []ric= new int [4] ;
                 JFrameAcquario jfk = new JFrameAcquario();
                 vec =jfk.vector();
                SerialPort sp = SerialPort.getCommPort("COM3"); // device name TODO: must be changed
                sp.setComPortParameters(9600, 8, 1, 0); // default connection settings for Arduino
                sp.setComPortTimeouts(SerialPort.TIMEOUT_WRITE_BLOCKING, 0, 0); // block until bytes can be written
                System.out.println("bo :)");
                if (sp.openPort()) {
                    System.out.println("Port is open :)");
                } else {
                    System.out.println("Failed to open port :(");
                }
                for (int i = 1; i < 12; ++i) {
                    sp.getOutputStream().write(vec[i]);
                    sp.getOutputStream().flush();

                    System.out.println("Sent number: " + vec[i]);
                    Thread.sleep(1000);
                }
                if(sp.getInputStream() != null){
                    for (int i = 0; i < 4; ++i) {
                        a = sp.getInputStream().read();
                        ric[i]=a;
                        System.out.println("Recived number: " + ric[i]);
                        Thread.sleep(1000);
                    }
                }

                if (sp.closePort()) {
                    System.out.println("Port is closed :)");
                } else {
                    System.out.println("Failed to close port :(");
                }
        }
    }                  
    };       
   swingWorker.execute();
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new JFrameAcquario(null).setVisible(true);
        }
    });
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题