Ads Top

HOW TO INITIALIZE an ARRAY OF JAVA


Java is a general-purpose, mid-level backend programming language with which you can create websites, web applications, Android applications for mobile devices and tablets, and other types of software. Matrices are one of the most frequently used data structures in Java, so it is crucial to understand how they work. Java arrays are, in fact, variables that allow you to store more than one value of the same data type and call any of them when you need it.

CHARACTERISTICS OF AN ARRAY DE JAVA

Java fixes are case-sensitive and are based on zero (the first index is not 1 but 0). In addition, Java fixes can only contain elements of the same data type. For example, you can have an array of integers or an array of strings but not a mixed array that contains both strings and integers. Since Java is a compiled language (unlike interpreted languages), you must also define the size of the array before passing it to the compiler.
If the Java compiler does not know in advance how many elements its matrix will have, it will not be able to allocate the necessary memory to the matrix and will not be able to compile its code. This also means that you cannot change the size of a matrix at run time (you cannot add new elements to the array and you cannot remove superfluous ones). Therefore, Java arrays require a bit more planning than matrices of a language interpreted as JavaScript, where you can add or remove elements on the fly freely.

1. DECLARE A NEW ARRAY OF JAVA

Because Java arrays can only contain elements of the same type, you must define what type of data you will use when you declare a new array. You can use primitive data types, such as integers, floats, Booleans, or objects, such as strings and dates. Because the Array object is also one of the built-in Java objects, you can also store arrays within an array. This type of matrix is ​​called a multidimensional matrix.
When you declare a matrix, you specify the data type and the name of the array. At this point, the array will not contain any elements yet. In fact, a matrix declaration is a plane from which you will create an instance during the next step (called instance creation). For example, this is what the declaration of a string array called myArray looks like:
/ * Statement of matrix * /
public class MyClass {
public static void main (String [] args) {
String [] myArray;
}
}
I put the matrix in the main () method so that the Java compiler evaluates it immediately. However, you can also place array declarations elsewhere in your Java code. Do not forget the brackets after the name of the data type (String [] in the example), since this is how the compiler knows that the new variable will be an array.
Note that since my Array has not yet been created, the compiler cannot evaluate its value either.

2. CREATE A NEW ARRAY INSTANCE

After declaring the new array, you must create a new instance of your array declaration, or in other words, you must create an instance of the array. For the creation of instances, you must use the new keyword that creates a new instance of array in memory. You must also specify the number of elements your Java array will have.
Following our example, this is how you can create an instance of the Array variable that we declared in the previous step:
/* Array instantiation */
import java.util.Arrays;
public class MyClass {
   public static void main(String[] args) {
      String[] myArray = new String[6]
      System.out.println(Arrays.toString(myArray));
   }
}
As you can see in the above code fragment, myArray will contain 6 elements and each one will be a string. I have also added the System.out.println () method that generates the result of the compilation in the console. And, to make that method work, I've imported the Arrays class in the first line of the code (in this way, all its built-in methods are available for the MyClass class).
This is the first point when the Java compiler is able to make sense of the new matrix. However, it only returns 6 null elements, since we have not yet initialized the values ​​for any of the elements:
/ * Console output * /
[null, null, null, null, null, null]

3. INITIALIZE THE ARRAY

When you initialize a matrix, define a value for each of its elements. You can initialize a value for each element separately using the simple assignment operator (equal sign). In the example, I assigned the name of a famous writer to each element:
/ * Matrix initialization * /
import java.util.Arrays
public class MyClass {
public static void main (String [] args) {
String [] myArray = new String [6];
myArray [0] = "Mark Twain";
myArray [1] = "Virginia Woolf";
myArray [2] = "William Shakespeare";
myArray [3] = "Maya Angelou";
myArray [4] = "Charles Dickens";
myArray [5] = "Agatha Christie";
System.out.println (Arrays.toString (myArray));
}
}
Since Java arrays are zero-based, an array with six elements will have indexes from 0 to 5. If you run the program, you can verify in the console that after initialization, each element has a value, in fact:
/ * Console output * /
[Mark Twain, Virginia Woolf, William Shakespeare, Maya Angelou,
Charles Dickens, Agatha Christie]

4. USE THE INITIALIZATION SHORT

If you do not want to write as much, you can also perform the three steps mentioned above (declaration, creation of instances, initialization) in a single step using the following abbreviated syntax:
/ * Initialization shorthand * /
import java.util.Arrays;
public class MyClass {
public static void main (String [] args) {
String [] myArray = {"Mark Twain", "Virginia Woolf",
"William Shakespeare", "Maya Angelou",
"Charles Dickens", "Agatha Christie"};
System.out.println (Arrays.toString (myArray));
}
}
Note that when you use the abbreviated syntax to initialize your Java array, you do not need to specify the number of elements. This is because the compiler can simply count the number of elements it assigned to the array to know the size of the memory it needs to allocate to the new array. However, you still need to use the brackets [] after the String class type.
In the console, you can prove that the abbreviation returns the same values ​​as the longest syntax:
/ * Console output * /
[Mark Twain, Virginia Woolf, William Shakespeare, Maya Angelou,
Charles Dickens, Agatha Christie]

5. INITIALIZE AN ARRAY WITH THE LOOP

If your matrix stores elements of a numeric data type (integers, floats, etc.), you can also use a for loop to initialize the array:
/ * Initialization with loop * /
import java.util.Arrays;
public class MyClass {
public static void main (String [] args) {
int [] myArray = new int [15];
for (int i = 0; i <15; i ++) {
myArray [i] = i + 1;
}
System.out.println (Arrays.toString (myArray));
}
}
Here, myArray is an array of integers and the for loop generates integers from 1 to 15. During instantiation, you must define the number of elements that will return the for loop (int [15] in the example).
In the console, you can see that the array has been initialized with integers from 1 to 15:
/ * Console output * /
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

Powered by Blogger.