Sequential Search

Java 2012.01.26 14:18 |

샘플 코드 >

public class SequentialSearch {
	public static boolean contains(int[] a, int b){
		for (int i : a) {
			if (i==b){
				return true;
			}
		}
		return false; 
	}
}

 

jUnit 테스트 >

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class SequentialSearchTest {

	@Test
	public void testContains() {
		int[]a = {1, 2, 3, 4, 5, 19, 17, 7};
		assertTrue(SequentialSearch.contains(a, 17));
		assertTrue(SequentialSearch.contains(a, 1));
		assertTrue(SequentialSearch.contains(a, 2));
		assertTrue(SequentialSearch.contains(a, 3));
		assertTrue(SequentialSearch.contains(a, 4));
		assertFalse(SequentialSearch.contains(a, 10));
	}
 

신고

'Java' 카테고리의 다른 글

정렬 알고리즘  (0) 2012.02.04
Binary Search  (3) 2012.01.26
Sequential Search  (0) 2012.01.26
java에서 손쉽게 JSON을 사용한다. JSON.simple example – Read and write JSON  (0) 2012.01.20
Java Garbage Collection  (0) 2012.01.19
Difference between JDK1.4.2, JDK 1.5.0 and JDK1.6  (0) 2012.01.19
Posted by jeonguk