Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.List;
- public class LinearSearch {
- public static void main(String[] args) {
- int num = Integer.parseInt(args[0]);
- Integer orderNum = getOrderNumByLinearSearch(num);
- System.out.println(orderNum != null? num + "は" + orderNum + "番目です。": num + "はありませんでした。");
- }
- public static Integer getOrderNumByLinearSearch(Integer targetNumber) {
- List<Integer> array = Arrays.asList(4, 2, 3, 5, 1);
- int orderNum = 1;
- for (Integer num : array) {
- if (num.equals(targetNumber)) {
- return orderNum;
- }
- orderNum++;
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement