Home  >  Article  >  Java  >  Java program to return elements at odd positions in a list

Java program to return elements at odd positions in a list

PHPz
PHPzforward
2023-08-25 15:41:121089browse

Java program to return elements at odd positions in a list

What's that weird spot on the list?

In the Java environment, printing return values ​​at odd positions of elements can be performed through control flow statements in the array list. Control flow statements in Java check odd positions based on step size. A loop is a condition-checking control flow method that helps evaluate whether a specific condition is true or false. For this case, "odd positions" always appear as first, third, fifth in a particular array list. By using this syntax, the build code checks for this condition.

In the first step, the temporary value is initialized to 0 so that it can be checked directly in the list. Next sort the data index by even or odd number. When a string is the main path, it is easier to find the element of the corresponding returned list.

In this article, we will learn and study how to write Java code and implement possible methods to return elements that appear in odd positions in a specific data list.

What is an array and its elements?

Arrays are similar types of data elements that exist in a specific list. In an array, users can access elements using index numbers. The search process is very simple and can be used effectively.

  • Suppose, here is an array {2,12,23,7,6,15}, and we need to find the odd position elements that appear in this specific array. Here we can see that the result will be 2,23,6 appearing in the list.

  • An array is a collection of similar data sets with elements of the same type. They are all continuous fixed-size data.

  • The elements appearing here are represented by N and end with N-1 after applying iteration.

  • In java, there are many ways to find the position of an element from a specific array

    • For method

    • Stream API method

  • For is an old and classic way to iterate over an array and then print and write, but for streaming API use "Arrays.AsList() .stream().forEach(s.o::p) ” is enough to get detailed result elements for these odd positions.

Algorithm for finding elements at odd positions

This is a general algorithm for finding elements at odd positions in Java -

  • Step 1 - Get Started

  • Step 2 - Initialize the specific array that exists on odd

  • Step 3 - Repeat steps to print (Step 5)

  • Step 4 - Print Array

  • Step 5 - Print

Example

public class OddPosition {  
   public static void main(String[] args) {     
      int [] arr = new int [] {11, 12, 13, 14, 15};  
      System.out.println("Odd position elements present in the array: ");  
      for (int i = 0; i < arr.length; i = i+2) {  
         System.out.println(arr[i]);  
      }  
   }  
}  

Output

Odd position elements present in the array: 
11
13
15

This is a simple and general example of finding elements at odd positions using java. There are now several ways to return array elements at odd positions in a specific data list. Let’s dig a little deeper.

The following method can find the elements at odd positions in the list -

  • Method 1 - Find odd elements with value 1 by iterating

  • Method 2 - Receive odd positions by position increment 2.

  • Method 3 - Find odd elements through flag pointer maintenance.

  • Method 4: - By looking for odd elements that are divisible by 2.

Find odd elements by iterating with value 1

The process of obtaining odd elements with an iteration value of 1: Iterator is an object file that can be used to connect loops. To use iterators in java there is a package called java.util.

  • Temporary value is initialized to 0.

  • Apply traversal.

  • Each iteration will check the temporary value, if the value is 0 then you will get returned, otherwise just continue the process.

  • The temporary value is increased by 1 after each processing.

Example

import java.io.*;
import java.util.*;
public class TP {
   public static void main(String[] args){
      List<Integer> tp_list1 = new ArrayList<Integer>();
      tp_list1.add(100);
      tp_list1.add(200);
      tp_list1.add(300);
      tp_list1.add(400);
      tp_list1.add(500);
      tp_list1.add(600);
      int temp_val = 0;
      System.out.print("Elements present at odd position are : ");
      for (Integer numbers : tp_list1) {
         if (temp_val % 2 != 0) {
            System.out.print(numbers + " ");
         }
         temp_val += 1;
      }
   }
}

Output

Elements present at odd position are: 200 400 600

Increment by position by 2 to get odd positions

Traverse the array elements and add 1 when the element position is an odd number.

Steps to follow in increments of 2 -

  • Traverse the list starting from the first position.

  • Apply an incremental process of 2 to each operation.

  • End the process after the iteration is completed.

  • First iteration - 1 2=3

  • Second iteration - 2 3=5

  • Third iteration - 5 2=7

  • Continue the process

  • return

Example

import java.io.*;
import java.util.*;
public class TP {
   public static void main(String[] args){
      List<Integer> tp_list2 = new ArrayList<>();
      tp_list2.add(1000);
      tp_list2.add(2000);
      tp_list2.add(3000);
      tp_list2.add(4000);
      tp_list2.add(5000);
      tp_list2.add(6000);
      System.out.print(
      "Elements at odd positions in that array are : ");
      for (int i = 1; i < 6; i = i + 2) {
         System.out.print(tp_list2.get(i) + " ");
      }
   }
}

Output

Elements at odd positions in that array are: 2000 4000 6000

Find odd elements through flag pointer maintenance

In a sorted array containing positive integers, the value of the first element should be the maximum value, the value of the second element should be the minimum value, and so on. During this process, the pointer will be initialized to 1 to start the iteration.

The steps to get the odd elements with an iteration value of 1 are -

  • start

  • The pointer is initialized to 1

  • Start iteration

  • If the flag is 1, print the data

  • Change flag to 0

  • Otherwise, if the flag throws 0, change it to 1

  • Finish

示例

import java.util.*;
public class PrintOddElementsInArray {
   public static void main(String[] args){
      int inputArray[] = new int[] { 1000, -5000, 4500, -2000,
                        10000, -2130, 7500 };
      System.out.println("Existing array elements.");
      for (int i = 0; i < inputArray.length; i++) {
         System.out.println(inputArray[i]);
      }
            
      System.out.println(
      "Array elements at odd position.");
      int flag = 1;
      for (int i = 0; i < inputArray.length; i++) {
         if (flag == 1) {
            System.out.print(inputArray[i] + " ");
            flag = 0;
         }
         else
         flag = 1;
      }
   }
}

输出

Existing array elements ..
1000
-5000
4500
-2000
10000
-2130
7500
Array elements at odd position.1000 4500 10000 7500

通过查找能否被 2 整除的元素

要检查奇数位置的元素,我们可以使用是否被2整除的方法。

示例

import java.util.*;
public class PrintOddElementsInArray {
   public static void main(String[] args){
      int inputArray[] = new int[] { 1000, -5000, 4500, -2000, 10000, -2130, 7500 };
      System.out.println("Existing array elements ..");
      for (int i = 0; i < inputArray.length; i++) {
         System.out.println(inputArray[i]);
      }
      System.out.println(
      "Array elements at odd position.");     
      for (int i = 0; i < inputArray.length; i++) {
         if (i % 2 == 0) {
            System.out.println(inputArray[i]);
         }
      }
   }
}

输出

Existing array elements.
1000
-5000
4500
-2000
10000
-2130
7500
Array elements at odd position.
1000
4500
10000
7500

结论

要查找出现在奇数位置的元素,应用排序方法或应用可被 2 整除的方法会更方便。它评估特定条件的过程是真还是假。

在本文中,我们学习了如何使用这些算法和示例编写 Java 程序来返回列表中奇数位置的元素。

The above is the detailed content of Java program to return elements at odd positions in a list. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete