JAVA: AN INTRODUCTION TO ARRAYS IN JAVA🍵
An Introduction to arrays in Java and why we need them?
INTRODUCTION
Arrays are one of the most important and fundamental concepts in Java. But what is an array? Why did programmers feel the need of introducing an array in programming? Why do we have to study them in the first place? Well, suppose you write a program, where you have to add 100 numbers. You may go on declaring variables for each value and make it work! But it is inefficient and requires a lot of hard work! In programming, all programmers try to follow a simple philosophy and that is called DRY philosophy.
DRY stands for Don't repeat yourself
"Don't repeat yourself" (DRY) is a principle of software development aimed at reducing the repetition of software patterns, replacing them with abstractions, or using data normalization to avoid redundancy. Declaring the same variable multiple times will make your code look unprofessional and large. Having said that, let's dive straight into arrays and understand their use and importance :D
What is an array?
Instead of declaring separate variables for each value, Arrays are used to store multiple values in a single variable. An array is a collection of similar types of data. Imagine it like this, If you take up an egg to be your variable, then an egg tray is an Array.
The above image shows two rows, which might make you think, can an array have one more array in it? The answer is YES! We will touch upon that at the end of this article.
📌Declaring the array
To declare an array, define the variable type with square brackets:
public class arrays{
public static void main(String[] args){
String [] cars;
}
}
You can make arrays of any primitive datatype and also of String which is not a primitive datatype, but String has special support in Java. The string is of utmost importance in programming and hence Java provides you with the option to make an array of Strings! For example:
public class arrays{
public static void main(String[] args){
String [] cars = {"Porsche", "Tesla", "Lamborgini", "Mazerati"};
}
}
An Array can be defined in more than one way. You can define an array in 3 ways. They are as follows:
In the third method, we directly initialize the array by using curly brackets. In the first two methods, we first declare and then allocate memory for them. Then they are initialized as follows:
public class arrays{
public static void main(String[] args){
int [] marks;
marks = new int[5];
marks[0] = 78;
marks[1] = 89;
marks[2] = 90;
marks[3] = 97;
marks[4] = 100;
System.out.println(marks[3]); // Output is 97
}
}
Array indices start from 0 and go up to (n-1), where n is the size of that Array. You access an array element by referring to the index number.
Change an Array Element
To change the value of a specific element, refer to the index number:
public class arrays{
public static void main(String[] args){
String[] cars = {"Porsche", "Tesla", "Lamborgini", "Maserati"};
cars[0] = "Ferrari";
System.out.println(cars[0]);
// Now outputs Ferrari instead of Porsche
}
}
Array Length
The array has a length property, to find out how many elements an array has, we use the length property:
public class arrays{
public static void main(String[] args){
String[] cars = {"Porsche", "Tesla", "Lamborgini", "Maserati"};
System.out.println(cars.length); // Output is 4
}
}
DISPLAYING AN ARRAY USING LOOPS
Using a for loop, we can display the elements inside an array. We use the length property to specify how many times the loop should run. See the example given below:
public class Main {
public static void main(String[] args) {
String[] cars = {"Porsche", "Tesla", "Lamborgini", "Maserati"};
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]); // View the output in your code editor:-D
}
}
}
There is also a "for-each" loop, which is used exclusively to display the elements of the array. See the example below:
String[] cars = {"Porsche", "Tesla", "Lamborgini", "Maserati"};
for (String elements : cars) {
System.out.println(elements); // you can use 'i' instead of elements
}
}
Multidimensional Arrays
A multidimensional array is an array of arrays. You can make n - D arrays. To create a 2-D array, we use "[]" 2 times to allocate the memory to 2 arrays. See the following example to have a better understanding:
public class Main {
public static void main(String[] args) {
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7, 8} };
}
}
In order to access the elements of this array, see the code below:
public class Main {
public static void main(String[] args) {
int [][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7, 8} };
int x = myNumbers[1][2];
System.out.print(x); // output is 7
}
}
If you are finding it difficult to visualize the 2-D array, then you can refer to the below hack!
That's a wrap!
Thank you for Reading my Article📄 Hope it adds value to the community💙
Follow me on Twitter vinayakstwt🕊️
Follow me on Hashnode VINAYAK PANCHAL✅