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 8556833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:26:34+00:00 2026-06-11T15:26:34+00:00

Is there a reason why an ArrayList would just lose items? I have an

  • 0

Is there a reason why an ArrayList would just lose items? I have an ArrayList that holds ArrayLists which holds Integers. There are about 32 items in each. Later when I need to call the ArrayList the items are missing except the last one. There are still 32 items, but they’re empty.

[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [-16323110, -16688166, -14978279, -837220, -11002320, -4136419, -13118245, -5370572, -11382698, -13140399, -13754890, -8184649, -11711035, -11668025, -12910969, -11058211, -9974559, -9665946, -12513105, -14320755, -16332438, -7673092, -13016023, -15454286, -12413148, -9780316, -2553149, -3313219, -10953060, -940790, -11989718, -12189591]]

I’m able to read it once and everything is there… but later when i call it again it’s gone. I’m not removing anything. What is happening here?

I cant post my original code but heres a mock up.

private ArrayList<ArrayList<Integer>> myArray = new ArrayList<ArrayList<Integer>>();

private void makeArray() {
for(int i = 0; i<= _height - 1; i++) {
            ArrayList<Integer> row = new ArrayList<Integer>();
            for(int ii = 0; ii <= _width - 1; ii++) {
                row.add(ii);    
            }
            myArray.add(row);
        }
}

private void readArray() {
ArrayList<Integer> row = new ArrayList<Integer>();

            for(int a = 0; a <= _height - 1; a++) {
                row.clear();
                row = myArray.get(a);

                for(int aa = 0; aa <= _width - 1; aa++) {
                    int c = row.get(aa);
//stuff
                }
            }
}
  • 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-11T15:26:35+00:00Added an answer on June 11, 2026 at 3:26 pm

    Look at your code:

    private void readArray() {
        ArrayList<Integer> row = new ArrayList<Integer>();
    
        for(int a = 0; a <= _height - 1; a++) {
            row.clear();
            row = myArray.get(a);
            ...
        }
    }
    

    At the start of each iteration, you’re clearing the ArrayList you were looking at in the last iteration. Why would you do that?

    Just get rid of the row.clear() call, and I’d expect everything to be fine. I’d also suggest using the rather more idiomatic:

    for(int a = 0; a < _height; a++) {
    

    Or even better:

    for(int a = 0; a < myArray.size(); a++) {
    

    Or even better:

    for (ArrayList<Integer> row : myArray) {
        ...
    }
    

    (Then you don’t need to call myArray.get() at all!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason there's a variable called d that is defined immediately after I
Is there a reason you would not set -static-link-runtime-shared-libraries to true when compiling an
is there any reason to use the post-redirect-get (prg) for a request that you
Is there a reason I am missing that a block within a case statement
Is there a reason or something that I am missing that has Sitecore return
When I log out the sectionList there are 20 items; for some reason, all
Is there a reason that they decided not to add the contains method (for
I have a data structure, for which I am currently using an ArrayList .
Is there any reason why I can't just use Guice for DI inside a
Developing Java, you have always learned that its best to create an ArrayList by

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.