Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.5 kB
0
Indexable
Never
package Week2;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;

public class Exercise1 {

	public static void main(String[] args) {
		int testTimes = 0;
		int[] arr = null;
		try {
//			System.out.println(System.getProperty("user.dir")); // return the current directory 
			Path file = Paths.get("./src/Week2/test-data-100.txt");
			List<String> stringData = Files.readAllLines(file);
			arr = new int[stringData.size()];
			for (int i = 0; i< arr.length; i++) {
				arr[i] = Integer.parseInt(stringData.get(i));    
			}
//			for (int num : arr) {
//				System.out.print(" " + num);
//			}
			System.out.println();
			testTimes = arr.length;
		}catch(IOException e){
			e.printStackTrace();
		}
		int[] array = {2,8,5,1,4,3};
		int num = array.length;
		int times = 0;
		
		while (times < testTimes) {
			int key = arr[times];
			System.out.println("Key: " + key);
			
			
			int index = seqSearch(array, num, key);
			
			if (index == -1 ) {
				System.out.println("Not found!");
			}else {
				System.out.println("Found in array[" + index + "]");
			}
			
			times++;
			
		}
	}
	
	public static int seqSearch(int a[], int length, int key) {
		int index = 0; 
		while (true) {
			if (index == length ) {
				return -1;
			}
			if (a[index] == key) {
				return index;
			}
			index++;
		}
	}
}