One-dimensional arrays are widely used in programming for storing lists of values such as marks, salaries, prices, IDs, and scores. They also form the foundation for advanced data structures and algorithms.
Table of Contents
Structure of a One-Dimensional Array
A one-dimensional array stores elements in a linear sequence. Each element is accessed using its index value.Example Representation:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
Creating a One-Dimensional Array
Arrays are created using the new keyword followed by the data type and size.Syntax:
Example:dataType[] arrayName = new dataType[size];
int[] numbers = new int[5];
Declaring and Initializing Arrays
Arrays can be declared and initialized at the same time.Syntax:
Example:dataType[] arrayName = {value1, value2, value3};
int[] marks = {85, 90, 78, 88};
Memory Allocation in Arrays
Array elements are stored in contiguous memory locations, which allows faster access.Example Representation:
Index: 0 1 2 3
Value: 10 20 30 40
Accessing Elements Using Indexes
Array elements are accessed using index numbers. In Java, indexing starts from 0.Syntax:
Example:arrayName[index];
// Java program to access array elements
// using indexes
public class AccessArray
{
public static void main(String[] args)
{
int[] arr = {10, 20, 30, 40};
System.out.println(arr[0]);
System.out.println(arr[2]);
}
}
Output:
10
30
Array Traversal Using for Loop
Traversal means accessing all array elements one by one using loops.Syntax:
Example:for(initialization; condition; increment/decrement) {
// code
}
// Java program to traverse an array
// using for loop
public class TraverseArray
{
public static void main(String[] args)
{
int[] arr = {5, 10, 15, 20};
for(int i = 0; i arr.length; i++)
{
System.out.println(arr[i]);
}
}
}
Output:
5
10
15
20
Array Traversal Using Enhanced for Loop
The enhanced for loop simplifies array traversal.Syntax:
Example:for(dataType variable : arrayName)
// Java program to traverse an array
// using enhanced for loop
public class EnhancedLoop
{
public static void main(String[] args)
{
int[] arr = {100, 200, 300};
for(int value : arr)
{
System.out.println(value);
}
}
}
Output:
100
200
300
User Input in Arrays
User input can be stored in arrays using loops and the Scanner class.Example:
// Java program to take array elements
// user input
import java.util.Scanner;
public class ArrayInput
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int[] arr = new int[5];
System.out.println("Enter 5 numbers:");
for(int i = 0; i arr.length; i++)
{
arr[i] = sc.nextInt();
}
System.out.println("Array Elements:");
for(int value : arr)
{
System.out.println(value);
}
sc.close();
}
}
Output:
Enter 5 numbers:
12
13
14
15
16
Array Elements:
12
13
14
15
Conclusion
One-dimensional arrays provide an efficient way to store and process multiple values in Java. They simplify operations such as searching, sorting, and traversal, making them an essential concept for every Java programmer.Frequently Asked Questions
1. What is a one-dimensional array?2. What is the starting index of an array?A one-dimensional array stores elements in a single sequence.
3. Can array size be changed after creation?The starting index is 0.
4. How do we find the array length? ExampleNo, the size of an array is fixed after creation.
5. Which loop is commonly used for array traversal?System.out.println(arr.length);
Both the for loop and the enhanced for loop are commonly used.
0 Comments