简体   繁体   English

Java,数组的复制大小

[英]Java, Copy Size of an Array

I just took up Java and was wondering. 我刚开始学习Java并想知道。 Let's say I have an array (in my case multidimensional) and I wanted to make a copy of it but using a different variable, eg I have a boolean array 假设我有一个数组(在我的情况下是多维数组),我想复制一个数组,但使用另一个变量,例如,我有一个布尔数组

boolean[][] arrBool = [2][3]

and I wanted to create an int array of the same dimensions 我想创建一个相同尺寸的整数数组

int[][] arrInt= [2][3]

array. 数组。 What would the simplest way to do this be? 最简单的方法是什么?

Thanks 谢谢

使用length ,它返回数组的长度

int[][] arrInt = new int[arrBool.length][arrBool[0].length]

Just as you'd think you would 就像您认为的那样

boolean[][] arrBool = new boolean[2][3];
int[][] arrInt = new int[2][3];

But I guess you probably mean what if the dimensions are dynamic. 但是我想你可能是说尺寸是动态的。 Then you could do 那你可以做

boolean[][] arrBool = ...;
int[][] arrInt = new int[arrBool.length][arrBool[0].length];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM