In the previous blog, you have learned about Java string. Through this blog on Java Array, I will explain you the concepts of Arrays in Java and how single & multi-dimensional arrays work. Learning about Java arrays is essential in earning your java certification.
In this Java Array blog, I would be covering the following topics:
- What are Java Arrays?
- Accessing a Specific Element in a Java Array
- Multidimensional Arrays in Java
- Passing Java Array to a Method
Before we proceed further, let’s see why exactly we need Java Array:
- Arrays are an important structure to hold data.
- Java allows us to hold many objects of the same type using arrays.
- It can be used with the help of a loop to access the elements by their index.
Now, let’s begin with this post on Java Array and understand what exactly are arrays.
What are Java Arrays?
Arrays in Java are homogeneous data structures implemented in Java as objects. Arrays store one or more values of a specific data type and provide indexed access to store the same. A specific element in an array is accessed by its index. Arrays offer a convenient means of grouping related information.
- First, you must declare a variable of the desired array type
- Second, you must allocate the memory that will hold the array, using new, and assign it to the array variable
So, let us see how can we declare arrays in different ways.
General Form of Java Array Initialization
General Form of Java Array Initialization
General Form of Java Array Initialization
class MyArray{ public static voide main(String args[]){ int month_days[ ] = {31,28,31,30,31,30,31,30,31,30,31}; System.out.println("April has " + month+days[3] + "days."); } }
It will only be fair if I explain how you can access elements in a Java Array.
Accessing a Specific Element in a Java Array
In arrays, we can access the specific element by its index within square brackets.
Example:-
Putting together all the pieces,
public static void main(String args[]) { int month_days[]; month_days = new int[12]; month_days[0] = 31; month_days[1] = 28; month_days[2] = 31; month_days[3] = 30; month_days[4] = 31; month_days[5] = 30; month_days[6] = 31; month_days[8] = 30; month_days[9] = 31; month_days[10] = 30; month_days[11] = 31; System.out.println("April has " + month_days[3] + " days."); } }
So, this was all about the arrays and its declaration and how single dimension arrays can be used.
What if I tell you, there can be an array inside an array. I know it sounds a bit complex, but don’t worry, I know how to make it easy for you.
Java Multidimensional Array
Multidimensional arrays are arrays of arrays.
Declaring Multidimensional Array
To declare it, we have to specify each additional index using another set of square brackets.
The following program, numbers each element in the array from left to right, top to bottom, and then displays these values:
class Mul2D{ public static void main(String args[]) { int mul2d[][]= new int[4][5]; int i, j, k = 0; for(i=0; i<4; i++) for(j=0; j<5; j++) { Mul2D[i][j] = k; k++; } for(i=0; i<4; i++) { for(j=0; j<5; j++); System.out.print(mul2d[i][j] + " "); System.out.println(); } } }
This program generates the following output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
These are other Multidimensional arrays representation of other data types.
So, this was all about the Multidimensional Arrays. Now, Let us see, how to pass an array to a method as a parameter like the other data types.
Passing Java Array to a Method
We can also pass arrays to methods just as we can pass primitive type values to methods.
Example:-
public class PMethods{ public static void display(int y[]) { System.out.println(y[0]); System.out.println(y[1]); System.out.println(y[2]); } public static void main(String args[]) { int x[] = { 1, 2, 3 }; display(x); } }
This will be the output of the program
1 2 3
This brings us to the end of Java Array blog. I hope you have enjoyed this post on Java Array. If you are looking for in-depth knowledge of Java, do read Java Tutorial blog where you will be explained in detail on below topics with examples.
- Data Types and Operations in Java
- Control Statements
- Classes & Objects
- Arrays
- Basic OOPS Concept
You can also learn Java through our YouTube Java Tutorial playlist. Happy Learning!!
If you found this blog on “Java Array” useful, check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Got a question for us? Please mention it in the comments section and we will get back to you.
Got a question for us? Please mention it in the comments section and we will get back to you.