๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

One Dimensional Arrays in Java  


Introduction
  • A one-dimensional array is the simplest form of an array in Java.
  • It stores a collection of elements of the same data type in a linear sequence.
  • Each element in the array can be accessed using a single index.
  • It is often used to represent lists, such as marks of students, prices of products, or names in a list.
  • For example :
    • int[] marks = {88, 74, 91, 82, 68, 94};
    • Arrays in Java

Working with 1D Arrays:

  • To work with 1D arrays in Java, we need to learn the following steps:
    1. Declare an array - Define the reference of the array with a specific data type.
    2. Create an array - Allocate memory for the array using the new keyword.
    3. Initialize an array - Assign values to the elements of the array.
    4. Retrieve elements of an array - Access array elements using their index.
  • We will go through each step with examples below.
1. Declare an array
  • Array declaration is the process of defining a variable that can hold multiple values of the same data type.
  • It tells the compiler what type of data the array will store (int, float, String, etc.).
  • At this stage, memory is not allocated; only the reference for the array is declared.
  • How to Declare an Array (Commonly Used Syntax):
    • Syntax:
      • dataType[] arrayName;
    • Examples:
      • int[] marks;
        String[] names;
  • Java also allows some alternate syntaxes to declare a 1D array:
    • Syntax:
      • dataType arrayName[];
        dataType []arrayName;
    • Examples:
      • int marks[];
        int []marks;
2. Create an Array
  • Array creation is the process of allocating memory for the array after it has been declared.
  • This is done using the new keyword in Java.
  • At this stage, a fixed block of memory is reserved to store multiple elements of the specified data type.
  • The size of the array must be defined during creation (it cannot be changed later).
  • How to Create an Array:
    • Syntax:
      • arrayName = new dataType[size];
    • Examples:
      • marks = new int[6];
        names = new String[6];
3. Initialize an Array
  • Array initialization is the process of assigning values to the elements of an array.
  • After an array is created, its elements have default values (e.g., 0 for int, 0.0 for float, null for objects).
  • How to Initialize an Array:
    • Syntax:
      • arrayName[index] = value;
    • Examples:
      • marks[0] = 88;
        marks[1] = 74;
        marks[2] = 91;
        marks[3] = 82;
        marks[4] = 68;
        marks[5] = 94;
4. Retrieve elements of an array
  • Retrieving array elements means accessing the values stored in the array using their index.
  • Array elements in Java are zero-indexed, so the first element is at index 0 and the last element is at index array.length - 1.
  • Different Ways to Retrieve Elements of an Array
    1. Using Index:
      • System.out.println(marks[0]);  // Output : 88
        System.out.println(marks[3]);  // Output : 82
    2. Using For Loop:
      • for(int i = 0; i < numbers.length; i++) {
            System.out.println(marks[i]);
        }
    3. Using Enhanced For Loop (For-Each):
      • for(int num : marks) {
            System.out.println(num);
        }
    4. Using Arrays.toString() (for displaying entire array):
      • System.out.println(Arrays.toString(numbers)); // Output: [10, 20, 30, 40, 50]

Array Programs:-
  • Program 1:
    public class MainApp1
    {
        public static void main(String[] args)
        {
    
            // 1. Declare and create an array of size 6
            int[] marks = new int[6];
    
            // 2. Initialize array elements
            marks[0] = 88;
            marks[1] = 74;
            marks[2] = 91;
            marks[3] = 82;
            marks[4] = 68;
            marks[5] = 94;
    
            // 3. Access and print array elements using normal for loop (Way 1)
            System.out.print("Way 1: ");
            for (int i = 0; i < marks.length; i++)
            {
                System.out.print(marks[i] + " ");
            }
            System.out.println(); // Move to next line
    
            // 4. Access and print array elements using for-each loop (Way 2)
            System.out.print("Way 2: ");
            for (int no : marks)
            {
                System.out.print(no + " ");
            }
            System.out.println(); // Move to next line
        }
    }
    • Points to note:
      • We have accessed the array elements in 2 ways, i.e., using normal for loop and for-each loop. But for-each loop is preferred as it is simple and easy to understand.
      • Declaring and creating array like above is lengthy. We can also declare, create, and initialize the array in a single line using shorthand notation as shown in Program 2 below.
  • Program 2:
    public class MainApp2
    {
        public static void main(String[] args)
        {
            // 1. Declare and create an array using shorthand notation
            // This automatically initializes the array with the given values
            int[] marks = {88, 74, 91, 82, 68, 94};
    
            // 2. Access and print array elements using a for-each loop
            // The variable 'no' takes the value of each element sequentially
            System.out.print("Marks are: ");
            for (int no : marks)
            {
                System.out.print(no + " "); // Print each element
            }
        }
    }
    • Points to note:
      • In this program, we have declared, created, and initialized the array in a single line using shorthand notation.
      • We have accessed the array elements using for-each loop, which is the more preferred way.