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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:38:40+00:00 2026-06-02T18:38:40+00:00

I have tried for the last two days to get some code to work,

  • 0

I have tried for the last two days to get some code to work, first with maps and then arrayLists. It is a nested for loop that I am trying to populate a data structure with as the loops are iterated. For some reason it is filling my final data structure (an arrayList of arrayLists called DataArrays2.getCinemaRowLayout2()) with the results of the last iteration. By this I mean that ALL arrayLists that were added are ALL duplicates of the FINAL iteration. I have no idea why this is happening!

An example of 2 iterations each with a different input can be seen here: [[o, o, o, o], [o, o, o, o]]. I appreciate that it might not be the best structure to use but I am running out of time so please accept my apologies.

Any help much appreciated!

for (int i=0; i<Screens.rows; i++){//for loop of screen rows

   System.out.println("Please enter how many seats and blank spaces in this row");
   Screens.columns = Integer.valueOf(scanInValue.next());

      for(int j=0; j<Screens.columns; j++){//for loop of seats in row
        System.out.println("Please enter 'o' for a seat or press enter for a space");
        in = scanInValue.next();
        DataArrays2.getCinemaRowLayout().add(j, in);//seats in a row arraylist
      }
DataArrays2.getCinemaRowLayout2().add(i, DataArrays2.getCinemaRowLayout());//arraylist of row arraylists
DataArrays2.getCinemaRowLayout().clear();//clears seats arrayList

}
DataArrays2.getScreenMapLayout().put(showingName, DataArrays2.getCinemaRowLayout2());//map of arraylists
BookingSystem2.saveShowing(DataArrays2.getScreenMapLayout()); //saves map to file
  • 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-02T18:38:43+00:00Added an answer on June 2, 2026 at 6:38 pm

    Java passing is by object reference, meaning that when you return an Object, you’re returning the location in memory of that Object. So for each run through that loop, you’re modifying the same ArrayList (the property of CinemaRowLayout). If you want to get a separate instance every time, you’ll have to create a new ArrayList from the contents of CinemaRowLayout and add that NEW collection to the map. Right now you’re adding that same object into your map over and over again, and then making changes on that object.

    Alternately, you could have DataArrays2.getCinemaLayout() return a new instance every time. If that’s the case, your innerLoop would look something like this:

    ArrayList<whatever> temp = new ArrayList<whatever>();
    for(int j=0; j<Screens.columns; j++){//for loop of seats in row
        System.out.println("Please enter 'o' for a seat or press enter for a space");
        in = scanInValue.next();
        temp.add(j, in);//seats in a row arraylist
    }
    DataArrays2.getCinemaRowLayout2().add(i, tmp);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

H I need to customize two datepickers. First shold have: minDate: last ten days
I have tried lots of ways to get the last inserted ID with the
The last months I have tried to get started with web developing using Java.
In the last two days I tried to import this Library through Maven: https://github.com/JakeWharton/Android-ViewPagerIndicator
I have tried running the code without the collision, and it works fine. But
I've checked every site I can find for the last two days and read
I've spent the last 2 days trying to figure this out, basically I have
I've been beating my head against my desk for the last two days over
The last two days I spent with hunting down memory leaks. I´ve read the
For the last few days I have been designing a layout for my website

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.