Any shortcut to initialize all array elements to zero?

后端 未结 14 2566
北恋
北恋 2020-11-28 18:12

In C/C++ I used to do

int arr[10] = {0};

...to initialize all my array elements to 0.

Is there a similar shortcut in

相关标签:
14条回答
  • 2020-11-28 18:35

    If you are using Float or Integer then you can assign default value like this ...

    Integer[] data = new Integer[20];
    Arrays.fill(data,new Integer(0));
    
    0 讨论(0)
  • 2020-11-28 18:35

    The int values are already zero after initialization, as everyone has mentioned. If you have a situation where you actually do need to set array values to zero and want to optimize that, use System.arraycopy:

    static private int[] zeros = new float[64];
    ...
    int[] values = ...
    if (zeros.length < values.length) zeros = new int[values.length];
    System.arraycopy(zeros, 0, values, 0, values.length);
    

    This uses memcpy under the covers in most or all JRE implementations. Note the use of a static like this is safe even with multiple threads, since the worst case is multiple threads reallocate zeros concurrently, which doesn't hurt anything.

    You could also use Arrays.fill as some others have mentioned. Arrays.fill could use memcpy in a smart JVM, but is probably just a Java loop and the bounds checking that entails.

    Benchmark your optimizations, of course.

    0 讨论(0)
  • 2020-11-28 18:36

    Yes, int values in an array are initialized to zero. But you are not guaranteed this. Oracle documentation states that this is a bad coding practice.

    0 讨论(0)
  • 2020-11-28 18:37
        int a=7, b=7 ,c=0,d=0;
        int dizi[][]=new int[a][b];
        for(int i=0;i<a;i++){
            for(int q=d;q<b;q++){
                dizi[i][q]=c;               
                System.out.print(dizi[i][q]);
                c++;
            }
    
            c-=b+1;
            System.out.println();               
        }
    

    result 0123456 -1012345 -2-101234 -3-2-10123 -4-3-2-1012 -5-4-3-2-101 -6-5-4-3-2-10

    0 讨论(0)
  • 2020-11-28 18:42

    A default value of 0 for arrays of integral types is guaranteed by the language spec:

    Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10) [...] For type int, the default value is zero, that is, 0.  

    If you want to initialize an one-dimensional array to a different value, you can use java.util.Arrays.fill() (which will of course use a loop internally).

    0 讨论(0)
  • 2020-11-28 18:43

    How it Reduces the Performance of your application....? Read Following.

    In Java Language Specification the Default / Initial Value for any Object can be given as Follows.

    For type byte, the default value is zero, that is, the value of (byte) is 0.

    For type short, the default value is zero, that is, the value of (short) is 0.

    For type int, the default value is zero, that is, 0.

    For type long, the default value is zero, that is, 0L.

    For type float, the default value is positive zero, that is, 0.0f.

    For type double, the default value is positive zero, that is, 0.0d.

    For type char, the default value is the null character, that is, '\u0000'.

    For type boolean, the default value is false.

    For all reference types, the default value is null.

    By Considering all this you don't need to initialize with zero values for the array elements because by default all array elements are 0 for int array.

    Because An array is a container object that holds a fixed number of values of a single type. Now the Type of array for you is int so consider the default value for all array elements will be automatically 0 Because it is holding int type.

    Now consider the array for String type so that all array elements has default value is null.

    Why don't do that......?

    you can assign null value by using loop as you suggest in your Question.

    int arr[] = new int[10];
    for(int i=0;i<arr.length;i++)
        arr[i] = 0;
    

    But if you do so then it will an useless loss of machine cycle. and if you use in your application where you have many arrays and you do that for each array then it will affect the Application Performance up-to considerable level.

    The more use of machine cycle ==> More time to Process the data ==> Output time will be significantly increase. so that your application data processing can be considered as a low level(Slow up-to some Level).

    0 讨论(0)
提交回复
热议问题