How to initialize an array in Java?
data[10] = {10,20,30,40,50,60,71,80,90,91};
The above is not correct (syntax error). It means you are assigning an array to data[10]
which can hold just an element.
If you want to initialize an array, try using Array Initializer:
int[] data = {10,20,30,40,50,60,71,80,90,91};
// or
int[] data;
data = new int[] {10,20,30,40,50,60,71,80,90,91};
Notice the difference between the two declarations. When assigning a new array to a declared variable, new
must be used.
Even if you correct the syntax, accessing data[10]
is still incorrect (You can only access data[0]
to data[9]
because index of arrays in Java is 0-based). Accessing data[10]
will throw an ArrayIndexOutOfBoundsException.
How do I declare and initialize an array in Java?
You can either use array declaration or array literal (but only when you declare and affect the variable right away, array literals cannot be used for re-assigning an array).
For primitive types:
int[] myIntArray = new int[3];
int[] myIntArray = {1, 2, 3};
int[] myIntArray = new int[]{1, 2, 3};
// Since Java 8. Doc of IntStream: https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html
int [] myIntArray = IntStream.range(0, 100).toArray(); // From 0 to 99
int [] myIntArray = IntStream.rangeClosed(0, 100).toArray(); // From 0 to 100
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).toArray(); // The order is preserved.
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).sorted().toArray(); // Sort
For classes, for example String
, it's the same:
String[] myStringArray = new String[3];
String[] myStringArray = {"a", "b", "c"};
String[] myStringArray = new String[]{"a", "b", "c"};
The third way of initializing is useful when you declare an array first and then initialize it, pass an array as a function argument, or return an array. The explicit type is required.
String[] myStringArray;
myStringArray = new String[]{"a", "b", "c"};
Initialize part of an array in java
You can do something like this, it will create array with new size based on provided.
String[] temp = new String[] {"water", "shovel", "berries", "stick", "stone", "seed", "axe"};
String[] val = Arrays.copyOf(temp, 20);
System.out.println(val.length);
System.out.println(Arrays.toString(val));
The output will be:
20
[water, shovel, berries, stick, stone, seed, axe, null, null, null, null, null, null, null, null, null, null, null, null, null]
How to initialize all the elements of an array to any specific value in java
If it's a primitive type, you can use Arrays.fill()
:
Arrays.fill(array, -1);
[Incidentally, memset
in C or C++ is only of any real use for arrays of char
.]
Do we need to initialize an array in Java?
In Java, all array elements are automatically initialized to the default value. For primitive numerical types, that's 0
or 0.0
. For booleans, that's false
. For objects, that's null
.
In other languages such as C++, the values in an uninitialized array are undefined. Some compilers may initialize to 0/null similarly for security, and it's very bad practice to rely on this. However, this behavior is well defined in Java and so it's perfectly okay to create a primitive array and trust that the values are 0.
Related Topics
Using Threads to Make Database Requests
How to Capitalize the First Character of Each Word in a String
Java Hashmap: How to Get Key from Value
How to Use Wait and Notify in Java Without Illegalmonitorstateexception
How to Iterate Over a Jsonobject
How to Create a Windows Service from Java App
Where Does the @Transactional Annotation Belong
Equals VS Arrays.Equals in Java
Why Can't I Do Assignment Outside a Method
How to Update a Jfreechart'S Appearance After It's Been Made Visible
How to Pretty Print Xml from Java
How to Establish a Connection Pool in Jdbc