2

I am creating an ArrayList of Arraylist of ArrayList and am populating it the way below. It populates it correctly. I have figured that out through debug and println. But when I try to retrieve those integers, it does not work correctly and shows me the last integer from the file which I have read for all the values of my ArrayList variable.

Below is the code where I populate and it works well.

    Scanner sc = new Scanner(new File("e1.txt"));
    Scanner lineSc;
    String lineStr;


    String lineRegEx = "(\\d+),(\\d+)";
    Pattern linePattern = Pattern.compile(lineRegEx);
    Matcher matcher;

    Integer vertex = 0, edge = 0, length = 0;
    String strE, strL ;
    ArrayList<Integer> tmpLE = new ArrayList<Integer>() ;
    ArrayList<ArrayList<Integer>> singleV = new ArrayList<ArrayList<Integer>>() ;

    sc.useDelimiter("\\n");


    int i = 0, j = 0;
    while (sc.hasNextLine()) {
        lineStr = sc.nextLine();
        lineSc = new Scanner(lineStr);

        lineSc.nextInt(); 

        matcher = linePattern.matcher(lineStr);

        while (matcher.find()) {
                strE = matcher.group(1);
                edge = Integer.parseInt(strE);
                tmpLE.add(EI, edge);
                strL = matcher.group(2);
                length = Integer.parseInt(strL);
                tmpLE.add(LI, length);

                singleV.add(j ,tmpLE);
                graph.add(i, singleV);
                //System.out.println (graph.get(i).get(j).get(0));
                //System.out.println (graph.get(i).get(j).get(1));
                ++j;
        }
        j = 0;
        ++i;
        lineSc.close();
    }
    sc.close();

}

Here is the code I have used for retrieving which does not work well

    for (int i = 0; i < N; ++i) {
        Integer minLen = graph.get(i).get(0).get(LI);
        System.out.println (minLen);
        Integer choosenEdge = 0;
        for (int j = 0; j < graph.get(i).size(); ++j) {
            ArrayList<Integer> tmpArr = graph.get(i).get(j);
            System.out.print(tmpArr.get(LI) + "  " + minLen); //Wrong Output
            if (minLen > tmpArr.get(LI))
            {
                minLen = tmpArr.get(LI);
                choosenEdge = graph.get(i).get(j).get(EI);
                System.out.println (choosenEdge); // Wrong Output
            }

        }

    }

1 Answer 1

1

You need to create ArrayList<Integer> for each matcher.find() loop.

 tmpLE = new ArrayList<Integer>() ;
 strE = matcher.group(1);
 edge = Integer.parseInt(strE);
 tmpLE.add(EI, edge);
 strL = matcher.group(2);
 length = Integer.parseInt(strL);
 tmpLE.add(LI, length);

 singleV.add(j ,tmpLE);
 graph.add(i, singleV);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.