【问题标题】:java program is printing objects in the wrong orderjava程序以错误的顺序打印对象
【发布时间】:2020-05-07 13:49:41
【问题描述】:

我正在尝试编写一个名为 RailwayStation 的类,它将打印一个火车数组(使用我编写的两个不同的类,称为 TIME1Train。我的问题是我不明白为什么输出排列顺序不对。

我认为问题出在名为 addTrain 的类中的方法中,如果数组中存在空单元格,并且如果希望添加的行程不存在,则该方法应该添加火车行程数组。我使用的另一种方法(可能是问题所在)称为removeTrain,它接收火车行程的参数并将其从数组中删除。我的方法addTrainremoverTraintoString如下:

    public class RailwayStation {

        // declrations of final variables
        private final int MAX_TRAINS = 100;
        private final int MIN_VAL = 0;

        // declrations of instant variables

        private Train[] _station;
        private int _noOfTrs;

        /**
         * Empty construter which initialize the instant variables of the class such that the trips array will be in a maximal size
         */
        public RailwayStation() {
            _station = new Train[MAX_TRAINS];
            _noOfTrs = MIN_VAL;
        }

        /**
         * adds a train trip to the trips array
         *
         * @param f the train trip
         * @Returns true if a train trip has been added to the trips array
         */
        public boolean addTrain(Train f) {
            int i, j;
    //        boolean found = false;
            if (isTrainOnSomeStation(f)) {
                return false;
            }
            else {
                for (j = MIN_VAL; j < _station.length; j++) {
                    if (_station[j] == null) {
                        _station[j] = f;
                        _noOfTrs++;
                        return true;
                    }
                }
                return false;
            }
        }

        // a private method that checks if @param f is null
        private boolean isTrainOnSomeStation(Train f) {
            if (f == null) {
                return false;
            }
            for (int i = MIN_VAL; i < _station.length; i++) {
                if (_station[i] != null && _station[i].equals(f)) {
                    return true;
                }
            }
            return false;
        }
 /**
     * removes a trip from the trips array
     * @param f the train trip
     * @returns true if the train trip has been removed
     */
    public boolean removeTrain(Train f) {
        int i, j;
        boolean found = false;
        for (j = MIN_VAL; j < _station.length && !found; j++) {
            if (_station[j] != null) {
                for (i = MIN_VAL; i < _noOfTrs && !found; i++)
                    if (_station[i].equals(f)) {
                        _station[i] = _station[_noOfTrs];
                        _station[_noOfTrs] = null;
                        found = true;
                        _noOfTrs--;
                    }
            }
        }
        return found;
    }
        /** Returns a string which describes all train in the array as apperas in the arrray
         * @Returns a string of trains as appears in the arrat
         */
        public String toString(){
            String str = "The trains today are:" +"\n";
            if(_noOfTrs == MIN_VAL){
                return "There are no trains today.";
            }
            else {
                String capacity = "";
                for (int i = 0; i < _station.length; i++) {
                    if (_station[i] != null) {
                        if (_station[i].isFull() == true) {
                            capacity = "Train is full";
                        }
                        else {
                            capacity = "Train is not full";
                        }
                        str += _station[i].toString() + "\n";
                    }
                }
            }
            return str;
        }
    }

为了调用 addTrain 方法,我会写:

//Check constructor
        RailwayStation rs = new RailwayStation();

        //AddTrain
        Train f1 = new Train("Haifa",12,0,210,250,250,55);
        Train f2 = new Train("Jerusalem",10,50,210,250,250,40);
        rs.addTrain(f1);
        rs.addTrain(f2);
        System.out.println(rs);

        //RemoveTrain
        rs.removeTrain(f1);
        System.out.println(rs);

        //First Train to Destination
        Train f3 = new Train("Tel-Aviv",11,35,180,100,200,35);
        rs.addTrain(f3);
        Train f3a = new Train("Tel-Aviv",7,15,180,200,200,35);
        rs.addTrain(f3a);

我期待得到输出:

The trains today are:
Train to Jerusalem departs at 10:50. Train is full.
Train to Tel-Aviv departs at 11:35. Train is not full.
Train to Tel-Aviv departs at 07:15. Train is full.

但我得到的是:

The trains today are:
Train to Tel-Aviv departs at 11:35. Train is not full.
Train to Jerusalem departs at 10:50. Train is full.
Train to Tel-Aviv departs at 07:15. Train is full.

我尝试使用调试器来了解顺序出错的部分,但我找不到问题所在。

【问题讨论】:

  • 你怎么打电话给addTrain?请提供完整的minimal reproducible example
  • @UnholySheep 已修复
  • @Jneven 你的 addTrain 调用似乎与打印不匹配?
  • @LarsNielsen - 对不起,你是对的,我已经解决了

标签: java arrays loops


【解决方案1】:

当你添加第一批火车时,你的数组是这样的:

Train[0] = Haifa...
Train[1] = Jerusalem..
Train[2] = null
Train[3] = null
...

然后你删除Haifa:

Train[0] = null
Train[1] = Jerusalem..
Train[2] = null
Train[3] = null
...

然后你添加其他火车:

Train[0] = Tel Aviv..
Train[1] = Jerusalem..
Train[2] = Tel Aviv..
Train[3] = null
...

这能解释清楚吗?


您在此处尝试构建的数据结构是 Stack - 但好消息是 java already has one,因此无需执行您正在尝试执行的操作:

        Stack<Train> trains = new Stack<>();
        Train f1 = new Train("Haifa",12,0,210,250,250,55);
        Train f2 = new Train("Jerusalem",10,50,210,250,250,40);
        trains.push(f1);
        trains.push(f2);

        //RemoveTrain
        trains.remove(f1);


        //First Train to Destination
        Train f3 = new Train("Tel-Aviv",11,35,180,100,200,35);
        trains.push(f3);
        Train f3a = new Train("Tel-Aviv",7,15,180,200,200,35);
        trains.push(f3a);

        String str = "The trains today are:" +"\n";
        for(Train train: trains) {
            str = str + train + "\n";
        }
        System.out.println(str);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    相关资源
    最近更新 更多