Java Arrays - A Complete Guide To Single & Multi-Dimensional Arrays In Java
Java is a general-purpose, concurrent, object-oriented, class-based, and the runtime environment(JRE). Through this blog on Java Array, I will explain you the concepts of Arrays in Java and how single & multi-dimensional arrays work.
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.
Obtaining an array is a two-step process.
- 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
Example:- int month_days[];
General Form of Java Array Initialization
Example:-
Arrays can be initialized when they are declared. The array will automatically be created large enough to hold the number of elements you specify in the array initializer. There is no need to use new.Now, let us see how we can implement this.
General Form of Java Array Initialization
The following code creates an initialized array of integers:
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.
Conceptually, the array declared above would be represented as shown in the figure:-
Let us now Demonstrate Multidimensional Array.
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 end of the Java Array blog. I hope you have enjoyed this post on Java Array.With this, we come to an end to this blog. If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.
Do look out for other articles in this series which will explain the various other aspects of Java.
1. Object Oriented Programming
5. Java String
8. Java Threads
9. Introduction to Java Servlets
11. Exception Handling in Java
14. Java Programs
15. Kotlin vs Java
16. Dependency Injection Using Spring Boot
22. Socket Programming In Java
25. Library Management System Project in Java
26. Trees in Java
28. Top Data Structures & Algorithms in Java
30. Top 55 Servlet Interview Questions
34. Java Collections Interview Questions and Answers
35. How to Handle Deadlock in Java?
36. Top 50 Java Collections Interview Questions You Need to Know
37. What is the concept of String Pool in Java?
38. What is the difference between C, C++, and Java?
39. Palindrome in Java- How to check a number or string?
40. Top MVC Interview Questions and Answers You Need to Know
41. Top 10 Applications of Java Programming Language
42. Deadlock in Java
43. Square and Square Root in Java
Originally published at www.edureka.co on January 25, 2018.