Array in Java With Example Program : – here’s an example program that demonstrates how to declare and use an array in Java:
- In Array We Store Collection of Value.
- When We Create An Array Special Object gets Created.
- In Array the Value get Store Starting At Index Zero.
Array in Java With Example Program
Array in Java Example
public class A {
public static void main(String[] args) {
int[] arr = new int[3];
System.out.println(arr);
}
}
Output : [I@2c7b84de
Example 2 :
public class A {
public static void main(String[] args) {
int[] arr = new int[3];
arr[0] =10;
arr[1] =20;
arr[2] =30;
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
}
}
Output :
10
20
30
Example 3:
public class ArrayExample {
public static void main(String[] args) {
// Declare and initialize an array of integers
int[] numbers = { 1, 2, 3, 4, 5 };
// Access and print elements of the array using a for loop
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element " + i + ": " + numbers[i]);
}
// Change the value of an element in the array
numbers[2] = 10;
// Access and print elements of the array again using a for loop
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element " + i + ": " + numbers[i]);
}
}
}
In this program, we declare and initialize an array of integers using the following line of code:
Array in java important Questions with Answers
Q: What is an array in Java?
Ans. An array is a data structure that stores a fixed-size sequence of elements of the same data type.
Q: How do you declare an array in Java?
Ans. To declare an array in Java, you use the following syntax:
data_type[] array_name;
More Article Related to Core Java –
- Top 10 Core Java Interview Question Answer
- 25 Java Interview Questions and Answer
- What is the Final Keyword in Java
- What is Wrapper Class in Java
- Break Keywords in Java with Example
- Types of access specifiers in java
- Spring Boot interview Questions with Answers
Q: How do you initialize an array in Java?
Ans.There are several ways to initialize an array in Java. One way is to use an array initializer, which is a comma-separated list of values enclosed in curly braces. For example, to initialize an array of integers with the values 1, 2, and 3, you could use:
int[] numbers = { 1, 2, 3 };
Q: How do you access elements of an array in Java?
Ans. You can access elements of an array in Java using the index of the element, which starts at 0. For example, to access the first element of an array named numbers
, you would use:
int firstElement = numbers[0];
Q: How do you change the value of an element in an array in Java?
Ans. To change the value of an element in an array, you can simply assign a new value to the element using its index. For example, to change the value of the third element in an array named numbers
to 10, you could use:
numbers[2] = 10;
Q: What is the length of an array in Java?
The length of an array in Java is the number of elements in the array. You can get the length of an array using the length
field. For example, to get the length of an array named numbers
, you would use:
int arrayLength = numbers.length;
Q: What is an array index out of bounds exception in Java?
Ans. An array index out of bounds exception is an error that occurs when you try to access an element of an array using an index that is outside the valid range of indexes for the array. This can happen if you use an index that is negative, or greater than or equal to the length of the array.
Where is microservice and web services
Thanks for feedback
Web services and micro services coming soon