因此,我想做的就是找到一种方法来更改此程序的确切功能,但要使用自定义的LinkedList类而不是Java LinkedList库。因此,我不用导入LinkedList,而只是使用自己制作的类。问题是我在执行此操作时遇到很多麻烦。我想知道是否有关于如何执行此操作的提示或任何解决方案?

提前致谢。

主要:

File f = new File("ass3.txt");

    Scanner scan = new Scanner(f);

    if (f.exists() == false) {
        System.out.println("File doesn't exist or could not be found.");
        System.exit(0);
    }

    int nVertices = scan.nextInt();
    int nEdges = scan.nextInt();

    for (int i = 0; i < 21; i++) {
        String s = scan.nextLine();
    }

    int[] dong = new int[99];

    Graph graph = new Graph(nVertices);
    for (int i = 0; i < 99; i++) {
        String vertex = scan.next();
        String connected = scan.next();
        int weight = scan.nextInt();
        dong[i] = weight;

        graph.addEdge(Graph.convertInt(vertex), Graph.convertInt(connected));
    }
    String startPoint = scan.next();
    String finishPoint = scan.next();
    graph.printGraph1(dong);


LinkedList1(我想使用的自定义类,而不是导入LinkedList):

static class LinkedList1 {

    Node head;

    static class Node {

        static int data;
        Node next;

        Node(int d) {
            data = d;
        }

    }

    public LinkedList1 insert(LinkedList1 list, int data) {
        Node new_node = new Node(data);

        new_node.next = null;

        if (list.head == null) {
            list.head = new_node;
        } else {
            Node last = list.head;
            while (last.next != null) {
                last = last.next;
            }
            last.next = new_node;
        }
        return list;
    }

    public void printList(LinkedList1 list) {
        Node currNode = list.head;

        System.out.print("LinkedList: ");

        while (currNode != null) {
            System.out.print(currNode.data + " ");

            currNode = currNode.next;
        }
    }

    @Override
    public String toString() {
        return "Data: " + Node.data;
    }
}


图形:

static class Graph {

    int vertex;
    LinkedList<Integer> list[];

    public Graph(int vertex) {
        this.vertex = vertex;
        list = new LinkedList[vertex];
        for (int i = 0; i < vertex; i++) {
            list[i] = new LinkedList<>();
        }
    }

    public void addEdge(int source, int destination) {

        //add edge
        list[source].addFirst(destination);

        //add back edge ((for undirected)
        list[destination].addFirst(source);
    }

    public void printGraph() {
        for (int i = 0; i < vertex; i++) {
            if (list[i].size() > 0) {
                System.out.print("Vertex " + convertString(i) + " is connected to: ");
                for (int j = 0; j < list[i].size(); j++) {
                    System.out.print(list[i].get(j) + " ");
                }
                System.out.println();
            }
        }
    }

    public void printGraph1(int[] dong) {
        for (int i = 0; i < vertex; i++) {
            if (list[i].size() > 0) {
                System.out.print("Vertex " + convertString(i) + " is connected to: ");
                for (int j = 0; j < list[i].size(); j++) {
                    int l = list[i].get(j);
                    System.out.print(convertString(l) + "(" + dong[j] + ") ");
                }
                System.out.println();
            }
        }
    }

    static int convertInt(String convert) {
        switch (convert) {
            case "a":
                return 0;
            case "b":
                return 1;
            case "c":
                return 2;
            case "d":
                return 3;
            case "e":
                return 4;
            case "f":
                return 5;
            case "g":
                return 6;
            case "h":
                return 7;
            case "i":
                return 8;
            case "j":
                return 9;
            case "k":
                return 10;
            case "l":
                return 11;
            case "m":
                return 12;
            case "n":
                return 13;
            case "o":
                return 14;
            case "p":
                return 15;
            case "q":
                return 16;
            case "r":
                return 17;
            case "s":
                return 18;
            case "t":
                return 19;
        }
        return 0;
    }
}

static String convertString(int convert) {
    switch (convert) {
        case 0:
            return "a";
        case 1:
            return "b";
        case 2:
            return "c";
        case 3:
            return "d";
        case 4:
            return "e";
        case 5:
            return "f";
        case 6:
            return "g";
        case 7:
            return "h";
        case 8:
            return "i";
        case 9:
            return "j";
        case 10:
            return "k";
        case 11:
            return "l";
        case 12:
            return "m";
        case 13:
            return "n";
        case 14:
            return "o";
        case 15:
            return "p";
        case 16:
            return "q";
        case 17:
            return "r";
        case 18:
            return "s";
        case 19:
            return "t";
    }
    return "";
}

最佳答案

如果您只是在寻找一种用您的实例替换现有的LinkedList实例的方法,则需要:


从导入中删除java.util.LinkedList
将类LinkedList1添加为具有完全限定的类名的导入,例如:xyz.abc.LinkedList1
将声明:LinkedList<Integer> list[]替换为LinkedList1 list[],并将初始化:list[i] = new LinkedList<>()替换为list[i] = new LinkedList1()
LinkedList中使用的方法替换为LinkedList1中的等效方法。


让我知道这是否是您想要的。

关于java - 从LinkedList库更改为自定义类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58247593/

10-10 08:57