简体   繁体   中英

Printing an ArrayList from another class

I have a print function for an arrayList in another class. Whenever I call it, it just shows an empty ArrayList. What am I doing wrong? I am pretty much simulating a elevator using threads and just adding the floors into the arrayList and then needing to sort them. I just want to print the array to just see if it's in order.

    import java.util.Random;
import java.util.concurrent.Semaphore;

public class Threads  {

    public static int RandomFloor(){
        Random random = new Random();
        int floor = random.nextInt(6) + 2;
        return floor;
    }
    public static void main(String[] args) throws Exception {

        Semaphore sem = new Semaphore(0);
        //Random number for floors

        Elevator elevator1 = new Elevator();

        //Creates the first set of threads and assigns the floor using random number generator defined at the top.
        Thread myThread1 = new Thread(new Driver(1,RandomFloor()));
        Thread myThread2 = new Thread(new Driver(2,RandomFloor()));
        Thread myThread3 = new Thread(new Driver(3,RandomFloor()));
        Thread myThread4 = new Thread(new Driver(4,RandomFloor()));
        Thread myThread5 = new Thread(new Driver(5,RandomFloor()));
        Thread myThread6 = new Thread(new Driver(6,RandomFloor()));
        Thread myThread7 = new Thread(new Driver(7,RandomFloor()));
        Thread elevator = new Thread(new Elevator());

        //Start first set of threads
        myThread1.start();
        myThread1.join();
        myThread2.start();
        myThread3.start();
        myThread4.start();
        myThread5.start();
        myThread6.start();
        myThread7.start();

        elevator1.printFloors();

        sem.acquire();


}
}

Elevator class which contains the arrayList and print function

 import java.util.*;
    public class Elevator implements Runnable
{

    List<Integer> floors = new ArrayList<Integer>();
    int floor;
    //add floors into Arraylist
    public void addFloors(int floor)
    {

    this.floor=floor;
        floors.add(floor);

        System.out.println("Added to floor array\n");

        System.out.println(floors);
    }

    public void run()
    {

    }


    public void sortFloors()
    {
        Collections.sort(floors);
    }

    public void printFloors()
    {
        //System.out.println(floors);
    }
}

You can create a new Elevator Thread with random number of floors like this:

Elevator elevator1 = new Elevator();
int numberOfFloors = RandomFloor();
for(int i = 1; i <= numberOfFloors; i++)
    elevator1.addFloors(i);

This will generate a random number from your RandomFloor() method and add that many integers to the floors array in your Elevator object.

Like other have pointed out, your code has some errors, like not even calling the addFloors method, and printFloors having no code


If you want to setup the Elevator object to start at a random floor and then give it some random floor numbers simulating customer calls then you could modify your code like this:

Elevator Class:

public class Elevator implements Runnable
{    
    private List<Integer> floors = new ArrayList<Integer>();
    private int startingFloor = 0;

    //add floor into Arraylist
    public void addFloor(int floor)
    {
        floors.add(floor);
        System.out.println("Added to floor array\n");
        System.out.println(floors);
    }

    public void setStartingFloor(int floor){
        this.startingFloor = floor;
    }

    public void run()
    {

    }

    public void sortFloors()
    {
        Collections.sort(floors);
    }

    public void printFloors()
    {
        System.out.println(floors);
    }
}

main method:

Elevator elevator1 = new Elevator();
elevator1.setStartingFloor(RandomFloor());

int numberOfElevatorCalls = 50;
for(int i = 0; i < numberOfElevatorCalls, i++){
    elevator1.addFloor(RandomFloor());
}

// sort
elevator1.sortFloors();

// print
elevator1.printFloors();

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