设置一个数组的所有值最快的方法? [英] Fastest way to set all values of an array?

查看:122
本文介绍了设置一个数组的所有值最快的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的char [] ,我想每一个指标的值设置为相同的字符值。结果
还有就是明显的方式做到这一点(迭代):

I have a char [], and I want to set the value of every index to the same char value.
There is the obvious way to do it (iteration):

  char f = '+';
  char [] c = new char [50];
  for(int i = 0; i < c.length; i++){
      c[i] = f;
  }

但我在想,如果有,将绕过需要遍历的方式,我可以利用 System.arraycopy 或等值的东西。有没有办法做到这一点?

But I was wondering if there's a way that I can utilize System.arraycopy or something equivalent that would bypass the need to iterate. Is there a way to do that?

编辑:
Arrays.java

public static void fill(char[] a, int fromIndex, int toIndex, char val) {
        rangeCheck(a.length, fromIndex, toIndex);
        for (int i = fromIndex; i < toIndex; i++)
            a[i] = val;
    }

这是完全一样的过程,这表明,有可能不是一个更好的方式来做到这一点。
结果+1大家谁建议填写反正 - 你是正确的,感谢您

This is exactly the same process, which shows that there might not be a better way to do this.
+1 to everyone who suggested fill anyway - you're all correct and thank you.

推荐答案

尝试 Arrays.fill(C,F):<一href=\"http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#fill%28char%5B%5D,%20char%29\">Arrays javadoc的

这篇关于设置一个数组的所有值最快的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆