Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8283317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:41:39+00:00 2026-06-08T10:41:39+00:00

I am creating an ArrayList of Arraylist of ArrayList and am populating it the

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:41:41+00:00Added an answer on June 8, 2026 at 10:41 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ArrayList that im populating within java and passing it off to
So I have an ArrayList in java. And what I'm doing is creating a
I am creating a website that will have articles; each article will have comments.
Basically, I am creating a dynamic 2d ArrayList. private ArrayList<char[]> myArray; This code below
I am creating the Leg Objects this way List<Leg> legs = new ArrayList<Leg>(legdata.length); I
I'm creating an application and I have a FragmentPager that I use. To use
The .NET 1.0 way of creating collection of integers (for example) was: ArrayList list
I'm creating a TableModel which will have a fixed number of columns, but the
In the life-cycle of my application, I have to re-create an ArrayList that contains
I've an arraylist having 30000 items in it, what's the best way of creating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.