Java Program to Print Odd Elements at Odd Index

In programming, accessing specific elements based on certain conditions is a common task. One such task is printing elements that are both odd and located at odd indices in an array. This task can be broken down into two main steps: identifying elements at odd indices and checking if they are odd. In this section, we will explore how to achieve this in Java, covering key concepts, a step-by-step guide, and additional considerations for robust and efficient code.

Arrays are fundamental data structures in Java, allows storage of multiple items of the same type. Accessing elements based on their position (index) and their value is a common requirement. In this section, we will develop a Java program that prints elements that are both odd and located at odd indices in an array.

Before diving into the implementation, let's review some key concepts:

  • Array Indexing: In Java, arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.
  • Odd Numbers: A number is considered odd if it is not divisible by 2. It can be checked using the modulus operator (%).
  • Odd Indices: For an element to be at an odd index, the index itself must be an odd number.

Let's start by creating a new Java program. Open your preferred text editor or Integrated Development Environment (IDE) and create a new file named OddElementsAtOddIndices.java.

File Name: OddElementsAtOddIndices.java

Output:

 
Element at index 3 is odd: 3
Element at index 5 is odd: 5   

Explanation

The program is encapsulated in a class named OddElementsAtOddIndices. The main() method serves as the entry point of the program. Here, we define an example array and call the printOddElementsAtOddIndices method.

Method to Print Odd Elements at Odd Indices:

We start iterating from index 1 and increment by 2 (i += 2), ensuring we only access odd indices. For each odd index, we check if the element is odd using the modulus operator (array[i] % 2 != 0). If true, we print the element.

Additional Considerations

Input Handling

For a more dynamic program, you might want to allow the user to input the array. This can be achieved using the Scanner class.

File Name: OddElementsAtOddIndices.java

Output:

 
Enter the number of elements in the array:
5
Enter the elements of the array:
2 1 4 7 9
Element at index 1 is odd: 1
Element at index 3 is odd: 7   

This modification prompts the user to enter the number of elements and the elements themselves, making the program more flexible.

Edge Cases

Consider edge cases, such as:

  • Empty arrays.
  • Arrays with only even indices.
  • Arrays with no odd elements at odd indices.

Here's how you might handle these cases:

Output:

 
Enter the number of elements in the array:
5
Enter the elements of the array:
121 3 4 5 6
Element at index 3 is odd: 5   

This version introduces a found flag to track if any qualifying elements are found. If none are found, a message is printed.

Optimizing Performance

While the task at hand is straightforward and doesn't typically require optimization for small arrays, consider the following tips for large arrays:

Early Exit: If the array has a length of 1, you can skip processing entirely as there are no odd indices to check.

Efficient Data Structures: For extremely large datasets, consider using data structures that allow more efficient traversal and manipulation, such as lists or streams.

Printing odd elements at odd indices in an array is a simple yet illustrative task that reinforces fundamental programming concepts such as array indexing and condition checking. By following the steps outlined in this article, you can create a flexible, efficient, and user-friendly Java program to achieve this goal. Remember to consider edge cases and optimize for performance as needed.