简体   繁体   English

将2个数组列表添加到另一个数组列表

[英]adding 2 arraylists to another arraylist

I have 2 arraylist like this, but im not sure how i can combine them together. 我有2这样的arraylist,但我不确定如何将它们组合在一起。 I have tried the Collections.copy() and addAll method but it wouldnt work, I want to know if there is any way I can combine them together into another arraylist? 我已经尝试过Collections.copy()和addAll方法,但是它无法正常工作,我想知道是否有任何方法可以将它们组合在一起成为另一个arraylist? What I really want is to have the first arraylist storing the data from the second arraylist, so each of the Point would have a few lines inside it. 我真正想要的是让第一个arraylist存储来自第二个arraylist的数据,因此每个Point里面都有几行。 Maybe you guys can give me some suggestions of how i can do this please, thanks. 谢谢你们,也许你们可以给我一些建议。

 static ArrayList<Vertex> getPoints() {
      Scanner input = Reader(Vertex.airports);
      ArrayList<Vertex> result = new ArrayList<Vertex>();

      while(input.hasNext()){
          String point = input.nextLine();
          result.add(new Vertex(point));

      }
      input.close();
      return result;

  }

  static ArrayList<Edge> getlines(){
          Scanner input = Reader(Vertex.graph);
          ArrayList<Edge> result = new ArrayList<Edge>();

          while(input.hasNext()){
              String route = input.nextLine();
              result.add(new Edge(route));
          }
          input.close();
          return result;

      }

the data are from 2 text files, the file looks like this. 数据来自2个文本文件,该文件如下所示。 They are just some of it to show what Im trying to do here. 它们只是为了展示我在这里想要做什么。 (index,name) (索引,名称)

1 England
2 France

3 Holland 3荷兰

(index,start,cost,end) (索引,开始,费用,结束)

1 1 2 2
2 1 3 1
3 1 6 3
4 1 7 3
5 2 1 2
6 2 3 1
7 2 4 1
8 2 5 2
9 2 6 2
10 3 1 1
11 3 2 1
12 3 4 1
13 3 8 1
14 3 10 2

thanks to Captain Ford this is completed! 多亏福特上尉完成了!

Okay, my understanding is that the first list contains a list of airports, and the second list connects the airports in the first list. 好的,我的理解是,第一个列表包含机场列表,第二个列表连接第一个列表中的机场。

If you want to store references to the lines with the associated points, you would need to create a wrapper around the points that contains another list. 如果要存储对具有关联点的线的引用,则需要围绕包含另一个列表的点创建包装。

class Airport {
    public Vertex point;
    public List<Edge> routes = new ArrayList<Edge>();

    public Airport(Vertex p){
        this.point = p;
    }
}

Then when you're loading your lists, you would do something like this: 然后,当您加载列表时,您将执行以下操作:

static ArrayList<Airport> getAirports() {
    Scanner input = Reader(Vertex.airports);
    ArrayList<Vertex> result = new ArrayList<Vertex>();

    while(input.hasNext()){
        String point = input.nextLine();
        //  Note the small change on the next line
        result.add(new Airport(new Vertex(point)));

    }
    input.close();
    return result;
}

static ArrayList<Edge> getlines(){
    Scanner input = Reader(Vertex.graph);
    ArrayList<Edge> result = new ArrayList<Edge>();

    while(input.hasNext()){
        String route = input.nextLine();
        result.add(new Edge(route));
    }
    input.close();
    return result;

}

static void combineLists(List<Airport> airports, List<Edge> lines){
    for(Edge edge : lines){
        //  This is where we add the routes to both airports
        //  Since I don't know the structure of the edge class, I'm making a guess that you're using the integer indices that were described in the file to describe the endpoints. Note, however, that you will need to find a way to guarantee that they match the indices of the list of airports.
        airports.get(edge.p1).routes.add(edge);
        airports.get(edge.p2).routes.add(edge);
    }
}

I think I can improve on my other answer. 我认为我可以改善其他答案。

A more complete solution would look like this: 更完整的解决方案如下所示:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Airport {
    public final int id;
    public String name;
    public List<Route> outboundRoutes = new ArrayList<Route>();
    public List<Route> inboundRoutes = new ArrayList<Route>();

    public double minDistance;
    public Airport previous;

    public Airport(int id, String name){
        this.id = id;
        this.name = name;
    }

    public static class Route {
        public final int id;
        public double cost;
        public Airport departure;
        public Airport destination;

        public Route(int id, double cost, Airport departure, Airport destination){
            this.id = id;
            this.cost = cost;
            this.departure = departure;
            this.destination = destination;
        }
    }


    public static Map<Integer,Airport> readAirports(Scanner input){
        Map<Integer,Airport> airports = new HashMap<Integer,Airport>();
        while(input.hasNext()){
            //  Using a map rather than a list simplifies the process of loading 
            //  your routes, and eliminates problems that would crop up if some
            //  airports are listed out of order or removed.
            Airport port = new Airport(input.nextInt(), input.next());
            airports.put(port.id, port);
        }
        return airports;
    }


    //  Note that the list of airports must be passed when loading the routes, 
    //  but that nothing is returned -- the routes are loaded directly 
    //  into the airports.
    public static void readRoutes(Scanner input, Map<Integer,Airport> airports){

        while(input.hasNext()){
            int id = input.nextInt();
            int departureId = input.nextInt();
            int destinationId = input.nextInt();
            double cost = input.nextDouble();

            if(!(airports.containsKey(departureId) && 
                    airports.containsKey(destinationId))){
                //  You'll have to decide how to handle a situation when a route 
                //  refers to airports that don't exist
                throw new RuntimeException(
                        "Undefined airport referenced in route #" + id);
            }

            Route route = new Route(id, cost, airports.get(departureId), 
                    airports.get(destinationId));
            route.departure.outboundRoutes.add(route);
            route.destination.inboundRoutes.add(route);
        }
    }

    public static Map<Integer,Airport> loadAirports() throws FileNotFoundException {
        Scanner inAirports = new Scanner(new File("airports.txt"));
        Scanner inRoutes = new Scanner(new File("routes.txt"));
        Map<Integer,Airport> airports = readAirports(inAirports);
        readRoutes(inRoutes, airports);
        return airports;
    }

    public static void main(String[] args){
        try {
            Map<Integer,Airport> airports = loadAirports();
            for(Airport port : airports.values()){
                System.out.println(port.name + " has " + port.inboundRoutes.size() + " inbound routes and " + port.outboundRoutes.size() + " outbound routes ");
                for(Route r : port.inboundRoutes){
                    System.out.println("\tin from " + r.departure.name + " at a cost of $" + r.cost);
                }
                for(Route r : port.outboundRoutes){
                    System.out.println("\tout to " + r.destination.name + " at a cost of $" + r.cost);
                }
            }

        } catch (IOException e){
            e.printStackTrace();
        }
    }
}

If you wanted a simple, straight up answer, what you were wanting to do with ArrayLists can't be done without doing some kind of nesting. 如果您想要一个简单,简单的答案,那么不进行某种嵌套就无法完成对ArrayList的处理。

Since you're working with data that has IDs, I would strongly recommend you use a HashMap anyway so you don't have to worry about your data not being in a perfect sequence. 由于您正在使用具有ID的数据,因此强烈建议您始终使用HashMap,这样您就不必担心数据排列不正确。 And since you have metadata (airport names), creating these classes is a good way to model the data. 并且由于您具有元数据(机场名称),因此创建这些类是对数据进行建模的好方法。

Edit: Code has now been altered to all fit in one file. 编辑:现在,代码已更改为完全适合一个文件。 With the sample files you provided called "airports.txt" and "routes.txt" respectively, it should work just fine. 使用您提供的分别名为“ airports.txt”和“ routes.txt”的示例文件,它应该可以正常工作。 When you run it, you should get the following output: 运行它时,应该获得以下输出:

England(LHR) has 4 inbound routes and 4 outbound routes 
    in from France(CDG) at a cost of $2.0
    in from Holland(AMS) at a cost of $1.0
    in from Portugal(LIS) at a cost of $3.0
    in from Iceland(KEF) at a cost of $3.0
    out to France(CDG) at a cost of $2.0
    out to Holland(AMS) at a cost of $1.0
    out to Portugal(LIS) at a cost of $3.0
    out to Iceland(KEF) at a cost of $3.0
France(CDG) has 5 inbound routes and 5 outbound routes 
    in from England(LHR) at a cost of $2.0
    in from Holland(AMS) at a cost of $1.0
    in from Germany(FRA) at a cost of $1.0
    in from Italy(LIN) at a cost of $2.0
    in from Portugal(LIS) at a cost of $2.0
    out to England(LHR) at a cost of $2.0
    out to Holland(AMS) at a cost of $1.0
    out to Germany(FRA) at a cost of $1.0
    out to Italy(LIN) at a cost of $2.0
    out to Portugal(LIS) at a cost of $2.0
Holland(AMS) has 5 inbound routes and 5 outbound routes 
    in from England(LHR) at a cost of $1.0
    in from France(CDG) at a cost of $1.0
    in from Germany(FRA) at a cost of $1.0
    in from Denmark(CPH) at a cost of $1.0
    in from Finland(HEL) at a cost of $2.0
    out to England(LHR) at a cost of $1.0
    out to France(CDG) at a cost of $1.0
    out to Germany(FRA) at a cost of $1.0
    out to Denmark(CPH) at a cost of $1.0
    out to Finland(HEL) at a cost of $2.0
Germany(FRA) has 4 inbound routes and 4 outbound routes 
    in from France(CDG) at a cost of $1.0
    in from Holland(AMS) at a cost of $1.0
    in from Italy(LIN) at a cost of $1.0
    in from Sweden(ARN) at a cost of $2.0
    out to France(CDG) at a cost of $1.0
    out to Holland(AMS) at a cost of $1.0
    out to Italy(LIN) at a cost of $1.0
    out to Sweden(ARN) at a cost of $2.0
Italy(LIN) has 3 inbound routes and 3 outbound routes 
    in from France(CDG) at a cost of $2.0
    in from Germany(FRA) at a cost of $1.0
    in from Portugal(LIS) at a cost of $3.0
    out to Portugal(LIS) at a cost of $3.0
    out to France(CDG) at a cost of $2.0
    out to Germany(FRA) at a cost of $1.0
Portugal(LIS) has 3 inbound routes and 3 outbound routes 
    in from England(LHR) at a cost of $3.0
    in from France(CDG) at a cost of $2.0
    in from Italy(LIN) at a cost of $3.0
    out to England(LHR) at a cost of $3.0
    out to France(CDG) at a cost of $2.0
    out to Italy(LIN) at a cost of $3.0
Iceland(KEF) has 1 inbound routes and 1 outbound routes 
    in from England(LHR) at a cost of $3.0
    out to England(LHR) at a cost of $3.0
Denmark(CPH) has 2 inbound routes and 2 outbound routes 
    in from Holland(AMS) at a cost of $1.0
    in from Norway(OSL) at a cost of $1.0
    out to Holland(AMS) at a cost of $1.0
    out to Norway(OSL) at a cost of $1.0
Norway(OSL) has 2 inbound routes and 2 outbound routes 
    in from Denmark(CPH) at a cost of $1.0
    in from Sweden(ARN) at a cost of $1.0
    out to Denmark(CPH) at a cost of $1.0
    out to Sweden(ARN) at a cost of $1.0
Finland(HEL) has 2 inbound routes and 2 outbound routes 
    in from Holland(AMS) at a cost of $2.0
    in from Sweden(ARN) at a cost of $1.0
    out to Sweden(ARN) at a cost of $1.0
    out to Holland(AMS) at a cost of $2.0
Sweden(ARN) has 3 inbound routes and 3 outbound routes 
    in from Germany(FRA) at a cost of $2.0
    in from Norway(OSL) at a cost of $1.0
    in from Finland(HEL) at a cost of $1.0
    out to Germany(FRA) at a cost of $2.0
    out to Norway(OSL) at a cost of $1.0
    out to Finland(HEL) at a cost of $1.0

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM