Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3.  
  4. public class LinearSearch {
  5. public static void main(String[] args) {
  6. int num = Integer.parseInt(args[0]);
  7. Integer orderNum = getOrderNumByLinearSearch(num);
  8. System.out.println(orderNum != null? num + "は" + orderNum + "番目です。": num + "はありませんでした。");
  9. }
  10.  
  11. public static Integer getOrderNumByLinearSearch(Integer targetNumber) {
  12. List<Integer> array = Arrays.asList(4, 2, 3, 5, 1);
  13. int orderNum = 1;
  14. for (Integer num : array) {
  15. if (num.equals(targetNumber)) {
  16. return orderNum;
  17. }
  18. orderNum++;
  19. }
  20. return null;
  21. }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement