I wrote the algorithm of the Basic Information Technology Engineer Examination as a practice in my own way.
A class made so that it can be used for practice such as the binary search method.
MessagePrinter.java
/**
 *A class that prints a message of search results
 * 
 * @author hatena
 *
 */
import java.util.List;
public class MessagePrinter {
    public void printFoundMessages(int targetNumber, List<Integer> targetList) {
        System.out.println("Number of objects to search in the list" + targetNumber + "Existed.");
        System.out.println("The search target list is" + targetList + "was.");
        System.out.println("The index of the number of search targets is" + targetList.indexOf(targetNumber) + "is.");
    }
    public void printNotFoundMessages(int targetNumber, List<Integer> targetList) {
        System.out.println("Number of objects to search in the list" + targetNumber + "Did not exist.");
        System.out.println("The list to be searched is" + targetList + "was.");
    }
}
The java.util.Random class had an ints () method that returned a specified range of IntStream.
I feel that the random numbers are biased. .. ..
RandomListCreator.java
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
/**
 *A class that creates a list of random positive integers.
 *
 * @author hatena
 *
 */
@AllArgsConstructor
public class RandomListCreator {
    public int lengthOfList;
    public int maximumNumber;//ex:If you specify 50, the list generated will include 50.
    /**
     *A method that returns an unsorted list of integers of a specified length.
     *
     *
     */
    public List<Integer> createRandUnorderdList() {
        //Random number generation class
        Random rand = new Random();
      //@formatter:off
        List<Integer> unorderdRandlist =
                rand
                .ints(this.lengthOfList, 0, this.maximumNumber + 1)
                .boxed()
                .collect(Collectors.toList());
      //@formatter:on
        return unorderdRandlist;
    }
}
Simply look for matching numbers from the beginning.
SeqSearch.java
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
 *A class that performs a linear search method (because it is an extended for statement, there is no sentinel turn.)
 *
 * @author hatena
 */
@Data
@AllArgsConstructor
public class SeqSearch {
    private List<Integer> targetList;
    /**
     *Method of the linear search method body. Search in order from the beginning.
     *
     * @return Matched index. If they don't match-1 is returned.
     *
     */
    public int findTargetNum(int targetNumber) {
        //Check if they match in order from the top of the list.
        for (int num : this.targetList) {
            //If the target number is included in the list, return the index number.
            if (num == targetNumber) {
                return this.targetList.indexOf(targetNumber);
            }
        }
        //If they do not match to the end.
        return -1;
    }
}
MainSeq.java
import java.util.List;
import java.util.Scanner;
import common.MessagePrinter;
import common.RandomListCreator;
import seqsearch.SeqSearch;
/**
 *Binary search method execution main class
 *
 * @author hatena
 *
 */
public class MainSeq {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //Have the user enter the information (length, maximum number in the list) needed to create a random list
        System.out.print("Enter the length of the list as a positive integer:");
        int lengthOfList = scanner.nextInt();
        System.out.print("Enter the maximum number to include in the list:");
        int maximumNumber = scanner.nextInt();
        //Create a random list
        RandomListCreator listCreator = new RandomListCreator(lengthOfList, maximumNumber);
        List<Integer> targetList = listCreator.createRandUnorderdList();
        System.out.println("I created a list of random numbers.");
        //Enter the number you want to search as standard
        System.out.print("Enter the number you want to linearly search:");
        int targetNumber = scanner.nextInt();
        System.out.println("In this list" + targetNumber + "Linear search for any.");
        //Pass the list and the search target to the class of the linear search body.
        SeqSearch seqSearch = new SeqSearch(targetList);
        //Class instantiation for displaying result messages
        MessagePrinter messagePrinter = new MessagePrinter();
        //If it matches the list, an integer greater than or equal to 0 is returned and the result is printed.
        if (seqSearch.findTargetNum(targetNumber) >= 0) {
            messagePrinter.printFoundMessages(targetNumber, seqSearch.getTargetList());
        } else {
            messagePrinter.printNotFoundMessages(targetNumber, seqSearch.getTargetList());
        }
        scanner.close();
    }
}
■ When the target number exists in the list
Enter the length of the list as a positive integer: 10
Enter the maximum number to include in the list: 15
I created a list of random numbers.
Enter the number you want to linearly search:9
Linear search for 9s in this list.
There were 9 search targets in the list.
The search target list is[0, 14, 14, 9, 6, 0, 4, 7, 15, 7]was.
The index of the number of objects to be searched is 3.
■ When the target number does not exist in the list
Enter the length of the list as a positive integer: 10
Enter the maximum number to include in the list: 15
I created a list of random numbers.
Enter the number you want to perform a linear search.:9
Linear search for 9s in this list.
The number 9 to be searched was not found in the list.
The list to be searched is[11, 7, 10, 10, 14, 4, 11, 3, 11, 1]was.
        Recommended Posts