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.

6
  • Method 2 requires you to have all the values when you declare the array, using method 1 you can populate it later on. Commented Jul 2, 2018 at 5: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. Commented Jul 2, 2018 at 5:58
  • Perhaps you should look here stackoverflow.com/questions/1200621/… Commented Jul 2, 2018 at 5: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 Commented Jul 2, 2018 at 6: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. Commented Jul 2, 2018 at 6:04

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This explanation was on point, thanks man it help clarified doubts.
No problem, glad I could help!
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.

Comments

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

Comments

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.