简单的很,直接代码。
package com.ufida.practice.suanfa.search; import java.util.ArrayList; import java.util.List; import java.util.Random; public class SequenceSearch { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub List<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < 15; i++) list.add(new Random().nextInt(15)); for(int i : list) System.out.print(i+" "); System.out.println("~~~~~~~~~~~"); int num = new Random().nextInt(20); System.out.println("找到了"+num+",位置在:"+SequenceSearch(list,num)); } static int SequenceSearch(List<Integer> list, int key){ for (int i = 0; i < list.size(); i++){ //查找成功,返回序列号 if (key == list.get(i)) return i; } return -1; } }