0

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.

BradleyPeh
  • 35
  • 7
  • Method 2 requires you to have *all* the values when you declare the array, using method 1 you can populate it later on. – Jai Jul 02 '18 at 05:58
  • 1
    Sometimes, you don't know the values yet when initializing the array. Imagine a program where the user must enter 10 values, for example. – JB Nizet Jul 02 '18 at 05:58
  • Perhaps you should look here https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java – Balwinder Singh Jul 02 '18 at 05:58
  • So using either code depends on whether I was given values to work with ? Also is the second line of code an object as well because I was told arrays were all objects – BradleyPeh Jul 02 '18 at 06:03
  • Both methods will end up giving an array object. `int nums[] = new int[10]` will compile into an object similar to `int nums[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}`. Therefore, 2nd method is a short-hand for instantiating the array with that fixed size, and putting in the elements. Arrays instantiated with 2nd method can still be changed element by element later on. – Jai Jul 02 '18 at 06:04
  • Oh so its more of a shortcut if I have the values but both are stills objects, alright I get it thanks – BradleyPeh Jul 02 '18 at 06:08

4 Answers4

2

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.

Jabari Dash
  • 1,702
  • 1
  • 11
  • 22
0

//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.

JaySin
  • 13
  • 3
0

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();
Samuel Philipp
  • 9,552
  • 12
  • 27
  • 45
Debapriya Biswas
  • 907
  • 7
  • 18
0

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

Bikash Sahani
  • 142
  • 1
  • 1
  • 10