1. Overview
In this article, You'll learn how to initialize the array in java. Array creation can be done in different ways.
Typically, Arrays are a collection of the value of that of the same type. You can not store the different types of values inside the array.
This is the underlying structure that is widely used in the Collection API and problem solving interview questions.
All the values stored inside the array are called as elements and each element is stored at a unique index. Array index starts from ZERO.
But, Collection api is preferred over arrays if you are doing insertions in middle or resizing the array.
2. How To Declare An Array?
Declaring array is pretty straight forward. Look at the below syntax.
DataType[] arrayName ;
All the below three segments are mandatory in the array declaration.
package com.javaprogramto.arrays.initialize;
public class ArrayDeclaration {
public static void main(String[] args) {
// int array declaration
int[] intArray;
// boolean array declaration
boolean[] statusArray;
// float array declaration
float[] salaryArray;
}
}
3. Initializing An Array
DataType[] arrayname = new DataType[size];new keyword and size must be specified to create an array.
4. Initializing An Array Without Assigning Values
package com.javaprogramto.arrays.initialize; public class ArrayInitializationDefaultValues { public static void main(String[] args) { // int array initialization int[] intArray = new int[9]; System.out.println("Printing array values : "); for(int i = 0; i < intArray.length ; i++){ System.out.println(intArray[i]); } } }Output:
Printing array values : 0 0 0 0 0 0 0 0 0
5. Initializing An Array After Declaration
package com.javaprogramto.arrays.initialize; public class ArrayInitializationAfterDeclaration { public static void main(String[] args) { // Example 1 // int array declaration int[] intArray; // Array initialization intArray = new int[]{10, 20, 30, 40, 50}; System.out.println("Printing array values : "); for (int i = 0; i < intArray.length; i++) { System.out.println(intArray[i]); } // Example 2 // Boolean array declaration boolean[] booleanArray ; // boolean array initialization booleanArray = new boolean[]{true, false, true}; System.out.println("Printing boolean array : "); for(int index = 0;index < booleanArray.length; index++){ System.out.println(booleanArray[index]); } } }Output:
Printing array values : 10 20 30 40 50 Printing boolean array : true false true
6. Initializing An Array And Assigning Values Without Using new Keyword
int[] intArray = {2, 4, 6, 8, 20}; boolean[] booleanArray = {true, false, true}; double[] salaryArray = {15000, 35000, 50000};Here, a new keyword is not used and size is calculated based on the values added to the array at declaration.
package com.javaprogramto.arrays.initialize;
public class ArrayInitializationWithoutNewKeyword {
public static void main(String[] args) {
// Example 1
// int array declaration and intialization without new keyword
int[] intArray = {2, 4, 6, 8, 20};
System.out.println("Printing array values : ");
for (int i = 0; i < intArray.length; i++) {
System.out.println(intArray[i]);
}
// Example 2
// Boolean array declaration and intialization without new keyword
boolean[] booleanArray = {true, false, true};
System.out.println("Printing boolean array : ");
for (int index = 0; index < booleanArray.length; index++) {
System.out.println(booleanArray[index]);
}
// Example 3
// Double array declaration and intialization without new keyword
double[] salaryArray = {15000, 35000, 50000};
System.out.println("Printing salary double array : ");
for (int index = 0; index < salaryArray.length; index++) {
System.out.println(salaryArray[index]);
}
}
}
7. Initializing the Wrapper Arrays and Employee Array
package com.javaprogramto.arrays.initialize; import com.javaprogramto.models.Employee; public class ArrayInitializationForNonPrimitives { public static void main(String[] args) { // Array initialization without the "new" keyword String[] stringArray = {"hello", "welcome"}; // String Array initialization with "new" keyword String[] newStringArray = new String[]{"hello", "welcome"}; Employee e1 = new Employee(100, "Jhon", 30); Employee e2 = new Employee(101, "Amal", 25); Employee e3 = new Employee(102, "Paul", 35); // Employee Array initialization without "new" keyword Employee[] empArrayWithoutNewKeyword = {e1, e2, e3};
// Employee Array initialization without "new" keyword Employee[] empArrayWithNewKeyword = new Employee[]{e1, e2, e3}; } }
8. Create and Initialize An Array With Size Zero(0)
// Array initialization without the "new" keyword with size 0 String[] stringArray = {}; // String Array initialization with "new" keyword with size 0 String[] newStringArray = new String[0]; // Employee Array initialization without "new" keyword with size 0 Employee[] empArrayWithoutNewKeyword = {}; // Employee Array initialization with "new" keyword with size 0 Employee[] empArrayWithNewKeyword = new Employee[0];
No comments:
Post a Comment
Please do not add any spam links in the comments section.