繁体   English   中英

在Java中使用清单清单

[英]using a list of lists in java

我正在编写一个代码,该代码在理论上应采用带有多个顶点,边和边列表的图形的文本文件表示形式,并使用深度优先搜索来确定它是否是二分的。 但是,我使用列表的Arraylist来存储邻接列表,并且不断获取child.Color方法中for循环的java.lang.IndexOutOfBoundsException:Index:1,Size:0错误

import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import static java.lang.System.*;
import java.util.*;

public class detect_bipartite {
    public static void main(String[] args){
        int bipartCheck = 1;
        List<Integer> numList = new ArrayList<Integer>();   
        File inFile = null;
        //separate white space
        if (0 < args.length) {
            inFile = new File(args[0]);
        }        

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(inFile));
            String text = null;

            while ((text = reader.readLine()) != null) {
                String[] tmp = text.split(" ");
                for(String str: tmp)
                    if(!str.equals("")){
                        numList.add(Integer.parseInt(str));}

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
            }
        }

        int n = numList.get(0);
        int m = numList.get(1);

        List<Integer> edgeA = new ArrayList<Integer>();
        List<Integer> edgeB = new ArrayList<Integer>();

        for(int i=2; i<numList.size(); i++){
            if(i%2==0){edgeA.add(numList.get(i));}
            else if(i%2==1){edgeB.add(numList.get(i));}
        }

        List<List<Integer>> adjLists = new ArrayList<List<Integer>>(n);
        for(int j = 1; j <= adjLists.size(); j++){      //get adjacency lists
            for(int a=1; a <=edgeA.size(); a++){
                if(edgeA.get(a)==j){ (adjLists.get(j)).add(edgeB.get(a));}
                if(edgeB.get(a)==j){ (adjLists.get(j)).add(edgeA.get(a));}
            }
        }

        int[] color = new int[n];
        Arrays.fill(color, 0);
        //0 = uncolored
        //1 = red
        //2 = blue

        int bipart = childColor(n, 1, adjLists, color, 1);
        if (bipart==0){bipartCheck = 0;}

        for(int d = 0; d < n; d++)      //for any disconnected graphs
        {
            if(color[d] == 0){
                bipart = childColor(n, d, adjLists, color, 1);}
            if (bipart==0){bipartCheck = 0;}
        }


        if(bipartCheck == 1){
            System.out.println("Bipartite");
        } else{
            System.out.println("Not a Bipartite");
        }
    }

    public static int childColor(int n, int node, List<List<Integer>> adjLists, int[] color, int nodeColor){
        if(color[node] == nodeColor){
            return 1;}
        int bipart = 1;
        int newColor;
        if(nodeColor == 1){newColor = 2;}
        else{newColor = 1;}
        if(color[node] == newColor)
        {return 0;}
        color[node] = nodeColor;
        for(int k = 0; k < adjLists.get(node).size(); k++){  **//This is the error line**
            bipart = childColor(n, adjLists.get(node).get(k), adjLists, color, newColor);
            if(bipart == 0)
            {return 0;}
        }

        return bipart;
    }
}

当您这样做时:

List<List<Integer>> adjLists = new ArrayList<List<Integer>>(n);

您创建一个具有初始容量nArrayList 这并不意味着列表的大小为n 实际上,由于列表没有元素,因此其大小为0。鉴于此, 1 <= 0false并且永远不会执行此循环:

for(int j = 1; j <= adjLists.size(); j++){      //get adjacency lists
    for(int a=1; a <=edgeA.size(); a++){
        if(edgeA.get(a)==j){ (adjLists.get(j)).add(edgeB.get(a));}
        if(edgeB.get(a)==j){ (adjLists.get(j)).add(edgeA.get(a));}
    }
}

因此,当您调用childColor(n, 1, adjLists, color, 1)adjLists为空 当您尝试通过执行adjLists.get(node)访问索引1处的元素时,没有任何元素,因此没有IndexOutOfBoundsException

另请注意,Java列表和数组索引从0开始,而不是1。

要解决此问题,您可以在尝试使用所有列表之前对其进行初始化:

List<List<Integer>> adjLists = new ArrayList<List<Integer>>(n);
for (int i = 0; i < n; i++) {
    adjLists.add(new ArrayList<>());
}

暂无
暂无

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

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