从另一个类打印ArrayList

时间:2016-10-21 05:14:05

标签: java arraylist

我在另一个类中有一个arrayList的print函数。每当我调用它时,它只显示一个空的ArrayList。我究竟做错了什么?我几乎使用线程模拟电梯,只是将地板添加到arrayList中,然后需要对它们进行排序。我只想打印数组,看看它是否正常。

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


}
}

包含arrayList和print函数的Elevator类

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

1 个答案:

答案 0 :(得分:0)

您可以创建一个具有随机数量的楼层的新Elevator线程,如下所示:

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

这将从您的RandomFloor()方法生成一个随机数,并将许多整数添加到Elevator对象中的floors数组。

正如其他人所指出的,您的代码有一些错误,比如甚至没有调用addFloors方法,而printFloors没有代码

如果你想设置Elevator对象从随机楼层开始,然后给它一些模拟客户呼叫的随机楼层号码,那么你可以像这样修改你的代码:

电梯类:

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

主要方法:

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