簡體   English   中英

將在main方法中創建的arrayList傳遞給另一個類

[英]pass an arrayList created in the main method to another class

我需要您的幫助:)我上了一堂課Main:

public class Main {

   private static ArrayList<Integer> xCoords = new ArrayList<Integer>();
   private static ArrayList<Integer> yCoords = new ArrayList<Integer>();


 public static void main(String[] args) {

     Adress adress2 = new Adress(6, 8);
     TourManager.addAdress(adress2);
     Adress adress3 = new Adress(3, 4);
     TourManager.addAdress(adress3);
     Adress adress4 = new Adress(9, 12);
     TourManager.addAdress(adress4);
     Adress adress5 = new Adress(12, 16);
     TourManager.addAdress(adress5);

      // Initialize intial solution

     Tour currentSolution = new Tour();

     currentSolution.generateIndividual();
     Tour best = new Tour(currentSolution.getTour());
     System.out.println("Distance From adress (0,0): " + best.getDistance());
     System.out.println("Solution distance : " + best.toString()+ "\n"); 

      // i try to sent x and y Coordinates in my too ArraList xCoords, yCoords list
      // in the oder to be used in the second class to draw line.

         for (int i = 0; i < best.tourSize(); i++) {
              xCoords.add(i, best.getAdress(i).x);
              yCoords.add(i, best.getAdress(i).y);
         }

         for (int i = 0; i < best.tourSize(); i++) {
              System.out.print(yCoords.get(i)+ " - ");
         }

         new Draw(); // new instance

}

上第二節課,畫線:

public class Draw extends JPanel {


    //private static ArrayList<Integer> xCoords = new ArrayList<Integer>();
    //private static ArrayList<Integer> yCoords = new ArrayList<Integer>();
    // how can i recover the tow ArrayList xCoords, yCoords in the order to make a loop for to exract x,y


          public void paintComponent(Graphics g)
          {

             final int offset = 5; 

             super.paintComponent(g);
             g.setColor(new Color(54,34,56));

             for(int i = 1; i <xCoords.size() ; i++)
             {
                 g.fillOval(xCoords.get(i), yCoords.get(i), 8, 8);
                 g.setColor(Color.RED);
                 g.drawLine(xCoords.get(i-1)+offset, yCoords.get(i-1)+ offset, xCoords.get(i)+ offset, yCoords.get(i)+ offset);
             }

          }

    public Draw() {

        final int width = 400; // Breite des Fensters
        final int height = 450; // Hoehe des Fensters
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(width, height);
        f.add(this);
        f.setVisible(true);
    }

}

我嘗試在Draw類中實例化Tour,創建一個Loop以在ArrayList中添加x,y坐標,然后制作另一個Loop來提取x,y畫一條線,但是在我的旅行中我沒有數據,我應該重新創建Adress(x, y)有數據!

如果您想將某些東西傳遞給另一個類,只需編寫該類的構造函數,然后為要傳遞的東西添加參數。

在您的情況下,將是這樣的:

public class Draw extends JPanel { 
...
    public Draw(ArrayList<Integer> xCoords, ArrayList<Integer> yCoords) {
        // your code here. You can access the arraylists by the parameter name yCoords and xCoords
    }
...
}

之后,您只需創建Draw類的對象並傳遞數組即可。

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM