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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:38:45+00:00 2026-06-15T04:38:45+00:00

In Java, how do we make a copy of a 3-D array? Thing is,

  • 0

In Java, how do we make a copy of a 3-D array?
Thing is, when we use new_array.clone() or something of that sort, we are putting the address of the entry into the another_array, and not the actual value.
Hence when i clear() old_array, new_array is empty too

private List<List<List<String>>> moves = new ArrayList<List<List<String>>>();
private List<List<List<String>>> moves1 = new ArrayList<List<List<String>>>();

.blah
.blah 
.blah

mid_array = new ArrayList<List<String>>();//will use this array to insert inot moves1

for(int f = 0; f < moves.size(); f++)//make a copy of original array.
{

    List<String> row_st = moves.get(f).get(0);
    List<String> deck_st = moves.get(f).get(1);

    mid_array.add(row_st);//current array of the Rows
    mid_array.add(deck_st);//current array of the Deck

    moves1.add(mid_array);

    System.out.println("Moves1 "+moves1);//displays the new array correctly

    mid_array.clear();//clear mid_array, NOT moves1 or moves arrays

    System.out.println("Moves1 "+moves1);//new array is now empty

}
  • 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-15T04:38:47+00:00Added an answer on June 15, 2026 at 4:38 am

    In Java, Objects are always referenced so when you do:

       moves1.add(mid_array);
    

    It means mid_array is added to moves1 but still referenced with mid_array. This way, when you call mid_array.clear(), its clear from both the places.

    If you want to maintain the list inside moves1, then better to create a new instance of mid_array inside the for loop e.g. below:

     for(int f = 0; f < moves.size(); f++)//make a copy of original array.
     {
        List<List<String>> mid_array = new ArrayList<List<String>>();
        List<String> row_st = moves.get(f).get(0);
        List<String> deck_st = moves.get(f).get(1);
    
        mid_array.add(row_st);//current array of the Rows
        mid_array.add(deck_st);//current array of the Deck
    
        moves1.add(mid_array);
    
        System.out.println("Moves1 "+moves1);//displays the new array correctly
    
        //No need to clear as in each iteration, it will instantiate a new mid_array
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Make copy of array Java I'm a beginner at Java and I
I currently make Java (eclipse) applications for PC use. Everything from basic standalone programs,
Is there command to use on windows from java to make the computer sleep?
I am trying to make a Java tool that will scan the structure of
Using Java 1.6 and the AtomicLongArray, I'd like to copy the original AtomicLongArray into
I would like to use a dynamic array in C++ (something like an ArrayList
I studied that Java passes object references by value, and in order to make
I would like make an java app that communicates with a serial device(handheld scanner)
I'm trying to make a method that expects an array of int and two
I've noticed that Java String will reuse char array inside it to avoid 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.