创建具有相同的值/对象的n个副本的阵列? [英] Create an array with n copies of the same value/object?
本文介绍了创建具有相同的值/对象的n个副本的阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要的阵列中的每个索引创建一个大小 N
具有相同值的数组。什么是Java中做到这一点的最好方法是什么?
例如,如果 N
是5和值是布尔假
,数组应该是:
= [假的,假的,假的,假的,假的]
解决方案
您可以尝试一下:
布尔[]数组=新的布尔[5];
Arrays.fill(数组,FALSE);
第二个方法与手工填写数组:
布尔[]数组=新的布尔[] {假的,假的,假的,假的,假};
I want to create an array of size n
with the same value at every index in the array. What's the best way to do this in Java?
For example, if n
is 5 and the value is the boolean false
, the array should be:
= [false, false, false, false, false]
解决方案
You can try it with:
boolean[] array = new boolean[5];
Arrays.fill(array, false);
Second method with manual array fill:
boolean[] array = new boolean[] {false, false, false, false, false};
这篇关于创建具有相同的值/对象的n个副本的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文