Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java程序正在以错误的顺序打印对象_Java_Arrays_Loops - Fatal编程技术网

java程序正在以错误的顺序打印对象

java程序正在以错误的顺序打印对象,java,arrays,loops,Java,Arrays,Loops,我正在尝试编写一个名为RailwayStation的类,它将打印一组火车(使用我编写的两个不同的类,分别名为TIME1和train),我的问题是我无法理解为什么输出的排列顺序错误 我假设问题出在名为addTrain的类中的方法中,如果数组中存在空单元格,并且希望添加的trip在数组中不存在,则该类应该添加一个train trip被称为removeTrain,它接收列车行程的参数并将其从数组中删除。我的方法addTrain、removeTrain和toString如下所示: public

我正在尝试编写一个名为
RailwayStation
的类,它将打印一组火车(使用我编写的两个不同的类,分别名为
TIME1
train
),我的问题是我无法理解为什么输出的排列顺序错误

我假设问题出在名为
addTrain
的类中的方法中,如果数组中存在空单元格,并且希望添加的trip在数组中不存在,则该类应该添加一个train trip被称为
removeTrain
,它接收列车行程的参数并将其从数组中删除。我的方法
addTrain
removeTrain
toString
如下所示:

    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;
        }
    }
我希望得到输出:

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.
但我得到的是:

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.

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

添加第一列时,数组如下所示:

Train[0] = Haifa...
Train[1] = Jerusalem..
Train[2] = null
Train[3] = null
...
然后删除海法

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 trains=新堆栈();
f1列车=新列车(“海法”,12,0210250250,55);
f2列=新列车(“耶路撒冷”,10,50210250,40);
火车。推(f1);
火车。推(f2);
//清除雨水
列车。移除(f1);
//开往目的地的第一班火车
f3列=新列车(“特拉维夫”,11,35180100200,35);
火车。推(f3);
f3a列=新列车(“特拉维夫”,7,1518020035);
列车推送(f3a);
String str=“今天的列车是:“+”\n”;
用于(列车:列车){
str=str+train+“\n”;
}
系统输出打印项次(str);

当您添加第一列时,您的阵列如下所示:

Train[0] = Haifa...
Train[1] = Jerusalem..
Train[2] = null
Train[3] = null
...
然后删除海法

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 trains=新堆栈();
f1列车=新列车(“海法”,12,0210250250,55);
f2列=新列车(“耶路撒冷”,10,50210250,40);
火车。推(f1);
火车。推(f2);
//清除雨水
列车。移除(f1);
//开往目的地的第一班火车
f3列=新列车(“特拉维夫”,11,35180100200,35);
火车。推(f3);
f3a列=新列车(“特拉维夫”,7,1518020035);
列车推送(f3a);
String str=“今天的列车是:“+”\n”;
用于(列车:列车){
str=str+train+“\n”;
}
系统输出打印项次(str);

您如何呼叫
addTrain
?请提供完整的@UnholySheepfixed@Jneven你的addTrain呼叫似乎与指纹不匹配?@LarsNielsen-对不起,你是对的,我已修复你如何呼叫
addTrain
?请提供完整的@UnholySheepfixed@Jneven你的addTrain呼叫似乎与指纹?@LarsNielsen-对不起,你是对的,我把它修好了