Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,855 questions

51,776 answers

573 users

How to clone a two-dimensional array in Kotlin

3 Answers

0 votes
fun main() {
    val arr2D = arrayOf(
	    arrayOf(1, 2, 3, 0),
    	arrayOf(4, 5, 6, 98),
    	arrayOf(7, 8, 9, 176)
	)

	val clonedArray = arr2D.map { it.clone() }.toTypedArray()
    
    for (row in clonedArray) {
        for (n in row) {
            print("$n ")
        }
        println()
    }
}

  
     
/*
run:
  
1 2 3 0 
4 5 6 98 
7 8 9 176 
 
*/

 



answered Mar 8, 2025 by avibootz
0 votes
fun main() {
    val arr2D = arrayOf(
	    arrayOf(1, 2, 3, 0),
    	arrayOf(4, 5, 6, 98),
    	arrayOf(7, 8, 9, 176)
	)

	val clonedArray = Array(arr2D.size) { i ->
    		Array(arr2D[i].size) { j ->
                arr2D[i][j]
            }
    }
    
    for (row in clonedArray) {
        for (n in row) {
            print("$n ")
        }
        println()
    }
}

  
     
/*
run:
  
1 2 3 0 
4 5 6 98 
7 8 9 176 
 
*/

 



answered Mar 8, 2025 by avibootz
0 votes
fun main() {
    val arr2D = arrayOf(
	    arrayOf(1, 2, 3, 0),
    	arrayOf(4, 5, 6, 98),
    	arrayOf(7, 8, 9, 176)
	)

	val clonedArray = Array(arr2D.size) { i ->
            arr2D[i].copyOf()
        }
    
    for (row in clonedArray) {
        for (n in row) {
            print("$n ")
        }
        println()
    }
}

  
     
/*
run:
  
1 2 3 0 
4 5 6 98 
7 8 9 176 
 
*/

 



answered Mar 8, 2025 by avibootz

Related questions

3 answers 180 views
2 answers 153 views
1 answer 73 views
1 answer 68 views
1 answer 76 views
...