Java 如何在初始化对象类型数组一次后向其添加新对象?

Java 如何在初始化对象类型数组一次后向其添加新对象?,java,arrays,object,Java,Arrays,Object,我需要向以下数组中添加更多元素(本例中为边)。我怎么做?我需要一个快速的解决方案,因为没有时间改变架构。有可能吗?我尝试将所有内容从this数组复制到ArrayList。但我被对象类型不匹配所困扰 这是我的Dijkstra类,它包含主方法: import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class Dijkstra { public static Graph g

我需要向以下数组中添加更多元素(本例中为边)。我怎么做?我需要一个快速的解决方案,因为没有时间改变架构。有可能吗?我尝试将所有内容从this数组复制到ArrayList。但我被对象类型不匹配所困扰

这是我的Dijkstra类,它包含主方法:

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;


public class Dijkstra {
public static Graph graphMappingTemp;
public static ArrayList<Edge> arrayListEdge;


public static Edge[] GRAPH = {
  new Edge("Sri Lanka", "UK", 46),
  new Edge("Sri Lanka", "USA", 65),
  new Edge("Sri Lanka", "Dubai", 20),
  new Edge("Sri Lanka", "Singapore", 20),
  new Edge("Sri Lanka", "Malaysia", 35),
  new Edge("USA", "UK", 35),
  new Edge("UK", "USA", 35),
  new Edge("UK", "Dubai", 26),
  new Edge("Dubai", "Sri Lanka", 20),
  new Edge("Singapore", "Sri Lanka", 20),
  new Edge("Singapore", "Malaysia", 35),
  new Edge("Singapore", "Australia", 110),
  new Edge("Malaysia", "New Zealand", 73),
  new Edge("New Zealand", "Singapore", 113),
  new Edge("New Zealand", "Australia", 43),
  new Edge("Australia", "Dubai", 150),};

public static Graph graphMapping = new Graph(GRAPH);


private static final String START = "UK";
private static final String END = "USA";

public static void main(String[] args) {
    //graphMappingTemp = new Graph(GRAPH); //Create a new Graph
    //arrayListEdge = new ArrayList<Edge>(); //Create a new array list

    for (Edge edge : GRAPH) {
        arrayListEdge.add(edge); //copy all edges from Graph to ArrayList
    }

    //graphMapping = new Graph(arrayListEdge); //Create a new graph with the ArrayList

    Scanner input = new Scanner(System.in);


    graphMapping.runDijkstrasAlgo(START);
    graphMapping.printLeastCostPath(END);

    addCountry(input);
    graphMapping.printAllPaths();



    /*removeCountry(input);
    graphMapping.printAllPaths();*/

    //arrayListEdge.add(new Edge("UK", "USA", 56));         //test code

    input.close();
}

//Search least cost path option
public void searchLCP(Scanner input) {
    System.out.println("Please Enter the Origin : ");
    String inputOrigin = input.next();

    System.out.println("Please Enter the Destination : ");
    String inputDestination = input.next();
    //printLeastCostPath(inputDestination);
}

//Add country option
public static void addCountry(Scanner input){
    System.out.println("Please Enter your Country : ");
    String country = input.next();
    graphMapping.graph.put(country, new Vertex(country));

    //System.out.println(graphMapping.graph.containsKey("PAK"));

    System.out.println("Country successfully added!");

}

//Remove Country option
public static void removeCountry(Scanner input){
    System.out.println("Please Enter your Country : ");
    String country = input.next();
    System.out.println(country);
    graphMapping.graph.remove(country);

    System.out.println("Country successfully removed!");
}
图形类:

public class Edge {
public final String country1, country2;
public final int distance;

public Edge(String country1, String country2,int distance) {
    this.country1 = country1;
    this.country2 = country2;
    this.distance = distance;
}

}
import java.util.HashMap;
import java.util.Map;
import java.util.NavigableSet;
import java.util.TreeSet;


public class Graph {
public final Map<String, Vertex> graph;

public Graph(Edge[] edges) {

    graph = new HashMap<>(edges.length);

    for (Edge e : edges) {
        if (!graph.containsKey(e.country1)){
            graph.put(e.country1, new Vertex(e.country1));
        }

        if (!graph.containsKey(e.country2)){
            graph.put(e.country2, new Vertex(e.country2));
        }
    }

    for (Edge e : edges) {
        graph.get(e.country1).neighbours.put(graph.get(e.country2),                          e.distance); // Since it is an directed graph
    }
}

public void runDijkstrasAlgo(String origin) {
    if(!graph.containsKey(origin)){
        System.err.printf("No starting vertex to be found \"%s\"\n", origin);
    }

    final Vertex source = graph.get(origin);
    NavigableSet<Vertex> q = new TreeSet<>();

    for (Vertex v : graph.values()) {
        v.previous = v == source ? source : null;
        v.distance = v == source ? 0 : Integer.MAX_VALUE;
        q.add(v);
    }

    runDijkstrasAlgo(q);
}

private void runDijkstrasAlgo(final NavigableSet<Vertex> q) {
    Vertex u,v;

    while (!q.isEmpty()) {
        u = q.pollFirst();
        if (u.distance == Integer.MAX_VALUE) {
            break;
        }

        for (Map.Entry<Vertex, Integer> a : u.neighbours.entrySet()) {
            v = a.getKey();

            final int alternateDistance = u.distance + a.getValue();

            if (alternateDistance < v.distance) { // shorter path to neighbour found
                q.remove(v);
                v.distance = alternateDistance;
                v.previous = u;
                q.add(v);
            }
        }
    }
}

 /** Prints a path from the source to the specified vertex */
   public void printLeastCostPath(String destination) {
      if (!graph.containsKey(destination)) {
         System.err.printf("No end vertex to be found \"%s\"\n", destination);
         return;
      }

      graph.get(destination).printLeastCostPath();
      System.out.println();
   }

   public void printAllPaths() {
       for (Vertex v : graph.values()) {
        v.printLeastCostPath();
        System.out.println();
    }
}

}
import java.util.HashMap;
导入java.util.Map;
导入java.util.NavigableSet;
导入java.util.TreeSet;
公共类图{
公共最终地图图;
公共图(边[]边){
图形=新的HashMap(edges.length);
用于(边e:边){
if(!graph.containsKey(e.country1)){
图.put(e.country1,新顶点(e.country1));
}
如果(!图.containsKey(e.country2)){
图.put(e.country2,新顶点(e.country2));
}
}
用于(边e:边){
graph.get(e.country1).neights.put(graph.get(e.country2),e.distance);//因为它是一个有向图
}
}
public void runDijkstrasAlgo(字符串原点){
如果(!graph.containsKey(原点)){
System.err.printf(“找不到起始顶点\%s\”\n),原点);
}
最终顶点源=graph.get(原点);
NavigableSet q=新树集();
对于(顶点v:graph.values()){
v、 previous=v==source?source:null;
v、 距离=v==源?0:整数.MAX_值;
q、 添加(v);
}
runDijkstrasAlgo(q);
}
私人void runDijkstrasAlgo(最终NavigableSet q){
顶点u,v;
而(!q.isEmpty()){
u=q.pollFirst();
if(u.distance==整数最大值){
打破
}
对于(Map.Entry a:u.neights.entrySet()){
v=a.getKey();
最终int AlternativeInstance=u.distance+a.getValue();
if(AlternativeDistance
如果这样明确定义数组,则无法向数组中添加更多元素。如果希望有一个可以扩展的数组,可以使用ArrayList。你能告诉我们它的错误是什么,在哪里

更新:在构建阵列时,将其保留为ArrayList。然后,当您准备将其转换为数组时(即,当您完成对它的增长时),您可以这样做

Edge[] convertedArray = new Edge[thatArrayList.size()];
convertedArray = thatArrayList.toArray(convertedArray );

要求澄清应该在评论中完成,而不是回答。我尝试使用arraylist。但是当我使用它时,我必须将Graph类更改为ArrayList类型并传递它。因此构造函数可以使用它来创建我在那里创建的新HashMap。问题是,如何在那里使用ArrayList而不是当前的Edge[]数组?谢谢你的回复:)@david conrad即使在评论中我也回答了他的问题?他的部分问题是是否可以这样做,我回答了。@k9yosh你们不能把图表改成一个
列表
?将HashMap选择器更改为使用
edges.size()
,或者删除它,因为它毫无意义。您甚至不需要更改
for(Edge e:edges)
循环的语法,因为增强的for循环语法接受数组或Iterables。“我被对象类型不匹配卡住”是什么意思?我的意思是,当我创建一个ArrayList时,我如何将其传递到我的Graph类的构造函数中?(检查main方法中的注释行。)更改Graph以获取
列表
。我之前尝试过,但我的实现方式存在问题。我再查一下,然后再给你回电话。