简体   繁体   中英

Java arrays objects and initializer confusion

I'm learning java and was told arrays are implemented as objects. But they show two different codes without diving into details.

First they ask us to use arrays like this, but the downside is to manually add the values:

int nums[] = new int[10];  
nums[0] = 99;
nums[1] = -622;
.
.
.

Then they use this in some programs saying new is not needed because Java automatically does stuff:

int nums[] = {99, - 10, 100123, 18, - 972 ......}

If the second code is shorter and allows me to use arrays straightaway whats the point of the first code if they do the same thing but the first one require more code to input value by hand.

Let's say you were initializing an array of 1 million values, would you use the second method? No, because you would have a huge java file.

The first method is essentially allocating space:

int[] array = new int[1000000];

Creates 1 million spaces in memory with default value 0. Now if you want to initialize them, you may use a loop:

for (int i = 0; i < array.length; i++) {

    array[i] = i;
}

If you wanted an array of 10 million values, you only change one number:

// Just add a 0 to 1000000
int[] array = new int[10000000]

Now, if the size of your array changes, you don't have to change the loop. But if you used the second method and wanted an array of 10 million values, you would have to add 9 million values, and 9 million commas to your java file - not scalable.

int[] array = {1, 2, 3, 4, ... 1000000};

The second method is not "scalable." It only works for small arrays where you can confidently assume that the default values of that array won't change. Otherwise, it makes A LOT more sense to use the first (more common) method.

//This is one way of declaring and initializing an array with a pre-defined size first

int nums[] = new int[10];

//This is initializing the array with a value at index 0

nums[0] = 99;

//This is initializing the array with a value at index 1 and likewise allocating rest of array index values

nums[1] = -622;

//This is another way of declaring and initializing an array directly with pre-defined values. Here if you see instead of declaring array size first, directly the values are initialized for it

int nums[] = {99, - 10, 100123, 18, - 972 ......}

It depends on the way you prefer to use the arrays, but you must remember that whenever you use "new" keyword, there is a new space or resource created every time in memory.

Imagine you want to generate a series of random integers at runtime and want to store in the array:

int[] array = new int[1000000];
Random r = new Random();
for (int i = 0; i < array.length; i++)
    array[i] = r.nextInt();

When you don't know the items of array at the time of array declaration, then prefer method-1 ,

and,

when you know the all the values of array at the time of array declaration, then go for method-2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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