Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to declare an empty array in java and then I want do update it but the code is not working

Tags:

java

arrays

I want to declare an empty array in java and then I want do update it but the code is not working...

public class JavaConversion
{
    public static void main(String args[])
    {
        int array[]={};
        int number = 5, i = 0,j = 0;
        while (i<4) {
            array[i]=number;
            i=i+1;
        }
        while (j<4) {
            System.out.println(array[j]);
        }
    }
}
like image 454
Saif Arsalan Avatar asked Jan 18 '16 16:01

Saif Arsalan


People also ask

How do you declare an empty array in Java?

Use List. clear() method to empty an array.

How do you handle an empty array in Java?

Example 1 – Check if Array is Empty using Null Check To check if an array is null, use equal to operator and check if array is equal to the value null. In the following example, we will initialize an integer array with null. And then use equal to comparison operator in an If Else statement to check if array is null.

How do you update an array in Java?

To update or set an element or object at a given index of Java ArrayList, use ArrayList. set() method. ArrayList. set(index, element) method updates the element of ArrayList at specified index with given element.

How do you initialize an empty array?

To create an empty array, you can use an array initializer. The length of the array is equal to the number of items enclosed within the braces of the array initializer. Java allows an empty array initializer, in which case the array is said to be empty.


3 Answers

You are creating an array of zero length (no slots to put anything in)

 int array[]={/*nothing in here = array with no elements*/};

and then trying to assign values to array elements (which you don't have, because there are no slots)

array[i] = number; //array[i] = element i in the array of length 0

You need to define a larger array to fit your needs

 int array[] = new int[4]; //Create an array with 4 elements [0],[1],[2] and [3] each containing an int value
like image 78
Ross Drew Avatar answered Oct 19 '22 17:10

Ross Drew


Your code compiles just fine. However, your array initialization line is wrong:

int array[]={};

What this does is declare an array with a size equal to the number of elements in the brackets. Since there is nothing in the brackets, you're saying the size of the array is 0 - this renders the array completely useless, since now it can't store anything.

Instead, you can either initialize the array right in your original line:

int array[] = { 5, 5, 5, 5 };

Or you can declare the size and then populate it:

int array[] = new int[4];
// ...while loop

If you don't know the size of the array ahead of time (for example, if you're reading a file and storing the contents), you should use an ArrayList instead, because that's an array that grows in size dynamically as more elements are added to it (in layman's terms).

like image 15
Mage Xy Avatar answered Oct 19 '22 16:10

Mage Xy


You need to give the array a size:

public static void main(String args[])
{
    int array[] = new int[4];
    int number = 5, i = 0,j = 0;
    while (i<4){
        array[i]=number;
        i=i+1;
    }
    while (j<4){
        System.out.println(array[j]);
        j++;
    }
}
like image 8
brso05 Avatar answered Oct 19 '22 16:10

brso05