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

  • Home
  • SEARCH
  • 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 8849223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:35:42+00:00 2026-06-14T12:35:42+00:00

This question is a followup for this one.. Add Multidimensinal array to an array

  • 0

This question is a followup for this one..

Add Multidimensinal array to an array list using Java

  int tmp;
  int[][] Key = new int [TopSubKey.length+BottomSubKey.length][TopSubKey.length];
  int[][] KeyChoiceTable = {{14,17,11,24,1,5},{3,28,15,6,21,10},{23,19,12,4,26,8}}     
 int [][] KeySelection = new int [KeyChoiceTable.length][KeyChoiceTable[0].length];

List<int [][]> list = new ArrayList<int [][]>();


 int row,col,x;

for(int l =0;l<3;l++) {

 for(int i=0;i<KeyChoiceTable.length;i++){
    for(int j=0;j<KeyChoiceTable[0].length;j++){
         row = 0;
         col = 0;
         x = KeyChoiceTable[i][j];
         while(x>7){x=x-7; row=row+1;}
         col = x-1;

     KeySelection[i][j] = Key[row][col];
     }
 }
 list.add(KeySelection);
 System.out.print(list.size());
  }

If you could just have a look.. because the problem seems so stupid but its killing me.

The Lopp runs 2 times, What I am expecting from the last line is for each loop to store the 2D Array in the list at index 0, and therefore the second loop stores the second 2D Keyselection in the list at index 1.
Whenever i add to the Arraylist (list) the new additiion overwrittes all the other causing all entries to be the same as the last.. I have been trying all day, its the same if i use vectors or stacks.. what is going on ?

  • 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-14T12:35:43+00:00Added an answer on June 14, 2026 at 12:35 pm

    When you add KeySelection to list, you’re really adding a reference to it, not a copy. When you change KeySelection on the next iteration, the change is also reflected in the list. To avoid this, declare KeySelection in the same scope as you add it to list:

    for(int l =0;l<3;l++) {
        int [][] keySelection = new int [KeyChoiceTable.length][KeyChoiceTable[0].length];
        // Populate keySelection
        list.add(keySelection);
        System.out.print(list.size());
    }
    

    Then each entry in list is a different Array.

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

Sidebar

Related Questions

I have a followup question to the one asked here: Java Abstract Class (this
This is a followup question to one I previously asked: start-program-if-not-already-running-in-java I didn't get
I have a follow-up question to this one . I created a new form,
Followup question from this one: Swing font names do not match? (Making a font
This is a followup question to the one I posted last week Ajax.ActionLink not
This is a followup question from this one. Connecting Pyside with matplotlib My PythonFu
This is a followup to the solution for this question. I am using jQuery's
My question is basically is a followup question to this one , I need
This question is a followup to my previous one: Previous Questions . So I
This question is a followup to my previous question, Importing one long line of

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.