简体   繁体   English

toString一个包含对象的数组列表

[英]toString an arraylist containing object

I have an array list of type car. 我有一个类型为car的数组列表。 I have an overriding toString method which prints my car in the desired format. 我有一个重写的toString方法,该方法以所需的格式打印我的汽车。 I'm using the arraylist.get(index) method to print the cars. 我正在使用arraylist.get(index)方法打印汽车。 I only have one for now it works, but I want it do print for all of the cars in the array list. 我现在只有一个,它可以工作,但是我希望它能为阵列列表中的所有汽车打印。

This is my code: 这是我的代码:

public class Garage {

    private static int carsCapacity = 30;
    ArrayList<Cars> myGarage;
    public Garage() {
        Scanner scanAmountOfCars = new Scanner(System.in);
        System.out.println("Please limit the number of car the garage can contain.");
        System.out.println("If greater than 50, limit will be 30.");
        int validAmount = scanAmountOfCars.nextInt();
        if (validAmount <= 50) {
            carsCapacity = validAmount;
            myGarage = new ArrayList<Cars>();
        }
        System.out.println(carsCapacity);
    }

    public void addCar() {
        Cars car = new Cars(Cars.getID(), Cars.askCarID(), Cars.getPosition(), Attendant.askForAtt(), System.currentTimeMillis());
        myGarage.add(car);
        //System.out.println(car);
    }


    public static int getCarsCapacity() {
        return carsCapacity;
    }

    @Override
    public String toString() {
        return "Garage [Car:" + myGarage.get(0).getPlateNum() + " ID:" + myGarage.get(0).getCarID() +  " Position:" + Cars.getPosition() + 
                " Assigned to:" + Cars.getAssignedTo().getId() + "(" + Cars.getAssignedTo().getName()
                + ")" + " Parked at:" + Cars.convert(myGarage.get(0).getCurrTime()) + "]";
    }
}

I also put the Cars class in case you need it: 如果您需要,我还开设了Cars类:

public class Cars {

    private String carID;
    private String plateNum;
    private static String position;
    private static Attendant assignedTo;
    private long currTime;
    static String[] tempArray2 = new String[Garage.getCarsCapacity()];


    public Cars(String carID, String plateNum, String position, Attendant assignedTo, long currTime) {
        this.carID = carID;
        this.plateNum = plateNum;
        Cars.position = position;
        Cars.assignedTo = assignedTo;
        this.currTime = currTime;
    }

    private static void createCarsID() {

        for (int x = 0; x < Garage.getCarsCapacity(); x++) {
            tempArray2[x] = ("CR" + (x + 1));
        }
    }

    public static String getID() {

        createCarsID();
        String tempID = null;
        String tempPos = null;
        for (int x = 0; x < tempArray2.length; x++) {
            if (tempArray2[x] != null) {
                tempID = tempArray2[x];
                tempPos = tempArray2[x];
                getPos(tempPos);
                tempArray2[x] = null;
                break;
            }
        }

        return tempID;
    }

    public static void getPos(String IdToPos) {
        String strPos = IdToPos.substring(2);
        int pos = Integer.parseInt(strPos);
        position = "GR" + pos;

    }

    public String getPlateNum() {
        return plateNum;
    }


    public String getCarID() {
        return carID;
    }

    public static String getPosition() {
        return position;
    }

    public long getCurrTime() {
        return currTime;
    }


    public static Attendant getAssignedTo() {
        return assignedTo;
    }

    public static String askCarID() {
        boolean valid = false;
        System.out.println("Please enter your car's plate number.");
        Scanner scanCarID = new Scanner(System.in);
        String scannedCarID = scanCarID.nextLine();
        while (!valid) {
            if (scannedCarID.matches("^[A-Za-z][A-Za-z] [0-9][0-9][0-9]$")) {
                valid = true;
                System.out.println(scannedCarID);
            } else {
                System.out.println("Please enter a valid plate number. Ex: AF 378");
                askCarID();
            }
        }
        return scannedCarID.toUpperCase();
    }

    public static String convert(long miliSeconds) {
        int hrs = (int) TimeUnit.MILLISECONDS.toHours(miliSeconds) % 24;
        int min = (int) TimeUnit.MILLISECONDS.toMinutes(miliSeconds) % 60;
        int sec = (int) TimeUnit.MILLISECONDS.toSeconds(miliSeconds) % 60;
        return String.format("%02d:%02d:%02d", hrs, min, sec);
    }

}

Your Garage should have the implementation of toString() which uses ArrayList#toString() implementation: 您的Garage应具有toString()的实现,该实现使用ArrayList#toString()实现:

public String toString() {
    return "Garage: " + myGarage.toString();
}

Also remember to implement toString() in Cars.java . 还记得在Cars.java实现toString()

public String toString() {
    return "[" + this.carID + " " + 
    this.plateNum + " " +
    Cars.position + " " +
    Cars.assignedTo.toString + " " +
    String.valueOf(this.currTime) + "]"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 tostring 表示中获得的自定义对象的数组列表 - arraylist of custom object obtained from tostring representation 如何覆盖对象的ArrayList的ToString方法? - How to override the ToString method of ArrayList of object? ArrayList和toString - ArrayList and toString 如何查找包含Object的ArrayList - How to find ArrayList containing Object 复制Java中包含对象的双ArrayList(ArrayList <ArrayList<Object> &gt;&gt;) - Copying double ArrayList containing objects in Java (ArrayList<ArrayList<Object>>>) 如何在 arraylist (Java) 中搜索对象的元素,如果存在,则打印该对象的 .toString - How to search an element of an object in an arraylist (Java) and if it exists, print the .toString of that object 如何获取由ArrayList的.toString()调用组成的String [] <Object> 在一条线上 - How to get a String[] consisting of the .toString() calls of an ArrayList<Object> in one line 不能在ArrayList中使用对象的toString()方法 - Can't use toString() method of object inside ArrayList 为什么toString()方法在Java中的Array和ArrayList对象之间的工作方式不同 - Why toString() method works differently between Array and ArrayList object in Java 根据包含数组的对象索引ArrayList进行自定义排序 - Custom sorting by object index ArrayList containing arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM