如何在Java中检索数组的引用号?

时间:2015-10-03 23:25:13

标签: java arrays

一旦我的阵列完成,我想打印这样的东西:

腿_______出​​发时间______ _Origin_____Destination________旅行时间

1 ..................(时间)......................... ....(地点).........(地点)........................(时间)

2 ..................(时间)......................... ....(地点).........(地点)........................(时间)

3 ..................(时间)......................... ....(地点).........(地点)........................(时间)

我想根据用户输入数组项的顺序打印出具有自动分配数值的数组。我对如何打印出数组对象(时间,地点)有一个好主意,但我不知道如何打印出对应的参考数字。如何制作这些数字? (点只是占位符,不应该出现在代码中。我想把它漂浮在列中。所以不要让我放空的步伐)

我以为我可以向用户询问位置并将该项跟踪到引用并将该引用添加到数组中,但这会使数字顺序保持静态,我希望它们按顺序排列。任何帮助将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:0)

根据您提供的内容,一个简单的实现是:

List <FlightDetails> flightDetails = new ArrayList<>();

for (FlightDetails aflightDetail : flightDetails) {   
    System.out.println(aflightDetail.getDepartureTime() + " " +            
    aflightDetail.getOrigin() //and so on;
}

其中FlightDetails是一个包含各种属性的类:

class FlightDetails {
    private long departureTime;
    private String origin;
    //and so on, with various getters/setters
}

for语句将按插入顺序打印项目,因为ArrayList会保留它们。此外,您还可以通过简单地执行

找到第5个插入的元素
flightDetails.get(5);

__

如果您正在寻找基于插入以及访问时间的订购(这样当您访问航班时,它会被置于列表的顶部),您可以使用{ {1}}。 This link深入探讨了这一点。

答案 1 :(得分:0)

无需询问用户,跟踪或映射序列。列表排序时打印序列。

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;

public class SequenceOfArray
{
    // Declare an ArrayList of Travel
    ArrayList<Travel>   travelList  = new ArrayList<Travel>();

    public static void main(String[] args)
    {

        SequenceOfArray s = new SequenceOfArray();
        s.addInput(0, 4); // add non sequentional input , so that sorting by departTime gives different sequenc
        s.addInput(10, 15);
        s.addInput(5, 9);

        s.travelList.sort(s.compareByDepartTime); // sort travel by departTime
        s.print();
        s.travelList.sort(s.compareById); // sort by id
        s.print();
    }

    public void print()
{
    System.out.println("id_______Leg______________Departure Time___________________________Origin_________Destination_____________Travel Time");
    for (int i = 0; i < travelList.size(); i++)
    {
        Travel t = travelList.get(i);
        // print travel, leg is dynamic, as your list is sorted.
        System.out.println(t.id + ".........." + i + ".................." + t.departTime + "........" + t.origin + "........." + t.destination + ".............." + t.departTime);
    }

}

    Comparator<Travel>  compareByDepartTime = new Comparator<Travel>()
                                            {

                                                @Override
                                                public int compare(Travel o1, Travel o2)
                                                {
                                                    return o1.departTime.compareTo(o2.departTime);
                                                }
                                            };
    Comparator<Travel>  compareById         = new Comparator<Travel>()
                                            {

                                                @Override
                                                public int compare(Travel o1, Travel o2)
                                                {
                                                    int i = 0;
                                                    if (o1.id - o2.id > 0)
                                                        return 1;
                                                    if (o1.id - o2.id < 0)
                                                        return -1;

                                                    return i;
                                                }
                                            };

    public void addInput(int start, int end)
    {
        for (int i = start; i < end; i++)
        {
            Travel t = new Travel();
            t.id = i;
            t.departTime = Calendar.getInstance().getTime();
            t.origin = "Origin" + (i + 1);
            t.destination = "Destination" + (i + 1);
            t.travelTime = Calendar.getInstance().getTime();
            travelList.add(t);
        }

    }

}

class Travel
{
    long    id;
    Date    departTime;
    String  origin;
    String  destination;
    Date    travelTime;

    // getters and setters

}

示例输出:

 id_______Leg______________Departure Time___________________________Origin_________Destination_____________Travel Time
0..........0..................Sun Oct 04 05:11:51 GST 2015........Origin1.........Destination1..............Sun Oct 04 05:11:51 GST 2015
1..........1..................Sun Oct 04 05:11:51 GST 2015........Origin2.........Destination2..............Sun Oct 04 05:11:51 GST 2015
2..........2..................Sun Oct 04 05:11:51 GST 2015........Origin3.........Destination3..............Sun Oct 04 05:11:51 GST 2015
3..........3..................Sun Oct 04 05:11:51 GST 2015........Origin4.........Destination4..............Sun Oct 04 05:11:51 GST 2015
10..........4..................Sun Oct 04 05:11:51 GST 2015........Origin11.........Destination11..............Sun Oct 04 05:11:51 GST 2015
11..........5..................Sun Oct 04 05:11:51 GST 2015........Origin12.........Destination12..............Sun Oct 04 05:11:51 GST 2015
12..........6..................Sun Oct 04 05:11:51 GST 2015........Origin13.........Destination13..............Sun Oct 04 05:11:51 GST 2015
13..........7..................Sun Oct 04 05:11:51 GST 2015........Origin14.........Destination14..............Sun Oct 04 05:11:51 GST 2015
14..........8..................Sun Oct 04 05:11:51 GST 2015........Origin15.........Destination15..............Sun Oct 04 05:11:51 GST 2015
5..........9..................Sun Oct 04 05:11:51 GST 2015........Origin6.........Destination6..............Sun Oct 04 05:11:51 GST 2015
6..........10..................Sun Oct 04 05:11:51 GST 2015........Origin7.........Destination7..............Sun Oct 04 05:11:51 GST 2015
7..........11..................Sun Oct 04 05:11:51 GST 2015........Origin8.........Destination8..............Sun Oct 04 05:11:51 GST 2015
8..........12..................Sun Oct 04 05:11:51 GST 2015........Origin9.........Destination9..............Sun Oct 04 05:11:51 GST 2015
id_______Leg______________Departure Time___________________________Origin_________Destination_____________Travel Time
0..........0..................Sun Oct 04 05:11:51 GST 2015........Origin1.........Destination1..............Sun Oct 04 05:11:51 GST 2015
1..........1..................Sun Oct 04 05:11:51 GST 2015........Origin2.........Destination2..............Sun Oct 04 05:11:51 GST 2015
2..........2..................Sun Oct 04 05:11:51 GST 2015........Origin3.........Destination3..............Sun Oct 04 05:11:51 GST 2015
3..........3..................Sun Oct 04 05:11:51 GST 2015........Origin4.........Destination4..............Sun Oct 04 05:11:51 GST 2015
5..........4..................Sun Oct 04 05:11:51 GST 2015........Origin6.........Destination6..............Sun Oct 04 05:11:51 GST 2015
6..........5..................Sun Oct 04 05:11:51 GST 2015........Origin7.........Destination7..............Sun Oct 04 05:11:51 GST 2015
7..........6..................Sun Oct 04 05:11:51 GST 2015........Origin8.........Destination8..............Sun Oct 04 05:11:51 GST 2015
8..........7..................Sun Oct 04 05:11:51 GST 2015........Origin9.........Destination9..............Sun Oct 04 05:11:51 GST 2015
10..........8..................Sun Oct 04 05:11:51 GST 2015........Origin11.........Destination11..............Sun Oct 04 05:11:51 GST 2015
11..........9..................Sun Oct 04 05:11:51 GST 2015........Origin12.........Destination12..............Sun Oct 04 05:11:51 GST 2015
12..........10..................Sun Oct 04 05:11:51 GST 2015........Origin13.........Destination13..............Sun Oct 04 05:11:51 GST 2015
13..........11..................Sun Oct 04 05:11:51 GST 2015........Origin14.........Destination14..............Sun Oct 04 05:11:51 GST 2015
14..........12..................Sun Oct 04 05:11:51 GST 2015........Origin15.........Destination15..............Sun Oct 04 05:11:51 GST 2015