简体   繁体   中英

pass an arrayList created in the main method to another class

i need your help :) i have a 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

}

Hier my second class to draw line :

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);
    }

}

i tried to instantiate Tour in Draw class, create a Loop for to add x,y coordinates in ArrayList then make another Loop to exract x, y to draw a line but in my tour i have no data and i should recreate Adress (x, y) to have data !

If you want to pass something to another class, just write a Constructor of the Class and add parameters for the things you want to pass.

In your case it would be smth like this:

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
    }
...
}

After that you just create an object of the class Draw and pass your arrays.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM